engelsystem/src/helpers.php

242 lines
4.6 KiB
PHP
Raw Normal View History

<?php
2017-09-19 14:50:20 +02:00
use Engelsystem\Application;
use Engelsystem\Config\Config;
2018-10-08 21:15:56 +02:00
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Translation\Translator;
use Engelsystem\Http\Redirector;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
2018-10-08 21:15:56 +02:00
use Engelsystem\Http\UrlGeneratorInterface;
use Engelsystem\Renderer\Renderer;
2017-08-30 19:57:01 +02:00
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2017-08-31 17:30:54 +02:00
/**
2017-09-19 14:50:20 +02:00
* Get the global app instance
2017-08-31 17:30:54 +02:00
*
* @param string $id
2018-08-07 03:18:22 +02:00
* @return mixed|Application
2017-08-31 17:30:54 +02:00
*/
2018-10-08 21:15:56 +02:00
function app($id = null)
2017-08-31 17:30:54 +02:00
{
2018-10-08 21:15:56 +02:00
if (is_null($id)) {
2017-09-19 14:50:20 +02:00
return Application::getInstance();
2017-08-31 17:30:54 +02:00
}
2018-10-08 21:15:56 +02:00
return Application::getInstance()->get($id);
}
/**
* @return Authenticator
*/
function auth(): Authenticator
2018-10-08 21:15:56 +02:00
{
return app('authenticator');
2017-08-31 17:30:54 +02:00
}
/**
* @param string $path
* @return string
*/
function base_path($path = ''): string
{
return app('path') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path);
}
/**
* @param int $status
* @param array $headers
* @return Response
*/
function back($status = 302, $headers = []): Response
{
/** @var Redirector $redirect */
$redirect = app('redirect');
return $redirect->back($status, $headers);
}
/**
* Get or set config values
*
* @param string|array $key
* @param mixed $default
* @return mixed|Config
*/
function config($key = null, $default = null)
{
/** @var Config $config */
2017-08-31 17:30:54 +02:00
$config = app('config');
if (empty($key)) {
2017-08-31 17:30:54 +02:00
return $config;
}
if (is_array($key)) {
2017-08-31 17:30:54 +02:00
$config->set($key);
return true;
}
2017-08-31 17:30:54 +02:00
return $config->get($key, $default);
}
/**
* @param string $path
* @return string
*/
function config_path($path = ''): string
{
return app('path.config') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path);
}
/**
* @param string $path
* @param int $status
* @param array $headers
* @return Response
*/
function redirect(string $path, $status = 302, $headers = []): Response
{
/** @var Redirector $redirect */
$redirect = app('redirect');
return $redirect->to($path, $status, $headers);
}
/**
* @param string $key
* @param mixed $default
* @return Request|mixed
*/
function request($key = null, $default = null)
{
2017-08-31 17:30:54 +02:00
$request = app('request');
if (is_null($key)) {
return $request;
}
return $request->input($key, $default);
}
2018-08-07 03:18:22 +02:00
/**
* @param string $content
* @param int $status
* @param array $headers
* @return Response
2018-08-07 03:18:22 +02:00
*/
function response($content = '', $status = 200, $headers = []): Response
2018-08-07 03:18:22 +02:00
{
/** @var Response $response */
2018-08-07 03:18:22 +02:00
$response = app('psr7.response');
$response = $response
->withContent($content)
2018-08-07 03:18:22 +02:00
->withStatus($status);
2018-08-07 03:18:22 +02:00
foreach ($headers as $key => $value) {
$response = $response->withAddedHeader($key, $value);
}
return $response;
}
2017-08-30 19:57:01 +02:00
/**
* @param string $key
* @param mixed $default
* @return SessionInterface|mixed
*/
function session($key = null, $default = null)
{
/** @var SessionInterface $session */
2017-09-20 01:09:11 +02:00
$session = app('session');
2017-08-30 19:57:01 +02:00
if (is_null($key)) {
return $session;
}
return $session->get($key, $default);
}
/**
* Translate the given message
*
* @param string $key
* @param array $replace
* @return string|Translator
*/
function trans($key = null, $replace = [])
{
/** @var Translator $translator */
$translator = app('translator');
if (is_null($key)) {
return $translator;
}
return $translator->translate($key, $replace);
}
/**
* Translate the given message
*
* @param string $key
* @param array $replace
* @return string
*/
function __($key, $replace = []): string
{
/** @var Translator $translator */
$translator = app('translator');
return $translator->translate($key, $replace);
}
/**
* Translate the given message
*
* @param string $key
* @param string $keyPlural
* @param int $number
* @param array $replace
* @return string
*/
function _e($key, $keyPlural, $number, $replace = []): string
{
/** @var Translator $translator */
$translator = app('translator');
return $translator->translatePlural($key, $keyPlural, $number, $replace);
}
/**
* @param string $path
* @param array $parameters
2018-03-31 05:19:49 +02:00
* @return UrlGeneratorInterface|string
*/
2017-09-20 01:09:11 +02:00
function url($path = null, $parameters = [])
{
$urlGenerator = app('http.urlGenerator');
2017-09-20 01:09:11 +02:00
if (is_null($path)) {
return $urlGenerator;
}
return $urlGenerator->to($path, $parameters);
}
/**
* @param string $template
* @param mixed[] $data
* @return Renderer|string
*/
function view($template = null, $data = [])
{
$renderer = app('renderer');
if (is_null($template)) {
return $renderer;
}
return $renderer->render($template, $data);
}