2017-01-21 23:07:20 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-09-19 14:50:20 +02:00
|
|
|
use Engelsystem\Application;
|
2017-01-21 23:07:20 +01:00
|
|
|
use Engelsystem\Config\Config;
|
2020-12-28 16:04:05 +01:00
|
|
|
use Engelsystem\Events\EventDispatcher;
|
2018-10-08 21:15:56 +02:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
2019-07-08 01:31:59 +02:00
|
|
|
use Engelsystem\Helpers\Translation\Translator;
|
2019-11-27 19:11:37 +01:00
|
|
|
use Engelsystem\Http\Redirector;
|
2017-07-18 21:38:53 +02:00
|
|
|
use Engelsystem\Http\Request;
|
2018-08-11 15:05:55 +02:00
|
|
|
use Engelsystem\Http\Response;
|
2018-10-08 21:15:56 +02:00
|
|
|
use Engelsystem\Http\UrlGeneratorInterface;
|
2018-08-26 02:54:52 +02:00
|
|
|
use Engelsystem\Renderer\Renderer;
|
2017-08-30 19:57:01 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2017-01-21 23:07:20 +01:00
|
|
|
|
2017-08-31 17:30:54 +02:00
|
|
|
/**
|
2017-09-19 14:50:20 +02:00
|
|
|
* Get the global app instance
|
2023-01-28 00:41:29 +01:00
|
|
|
* @return mixed|Application
|
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation
|
2017-08-31 17:30:54 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
function app(string $id = null): mixed
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-27 19:11:37 +01:00
|
|
|
function auth(): Authenticator
|
2018-10-08 21:15:56 +02:00
|
|
|
{
|
|
|
|
return app('authenticator');
|
2017-08-31 17:30:54 +02:00
|
|
|
}
|
|
|
|
|
2022-12-14 00:28:34 +01:00
|
|
|
function base_path(string $path = ''): string
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
|
|
|
return app('path') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path);
|
|
|
|
}
|
|
|
|
|
2022-12-14 00:28:34 +01:00
|
|
|
function back(int $status = 302, array $headers = []): Response
|
2019-11-27 19:11:37 +01:00
|
|
|
{
|
|
|
|
/** @var Redirector $redirect */
|
|
|
|
$redirect = app('redirect');
|
|
|
|
|
|
|
|
return $redirect->back($status, $headers);
|
|
|
|
}
|
|
|
|
|
2017-01-21 23:07:20 +01:00
|
|
|
/**
|
|
|
|
* Get or set config values
|
2023-01-28 00:41:29 +01:00
|
|
|
* @return mixed|Config
|
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation
|
2017-01-21 23:07:20 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
function config(string|array $key = null, mixed $default = null): mixed
|
2017-01-21 23:07:20 +01:00
|
|
|
{
|
2019-11-06 13:43:57 +01:00
|
|
|
/** @var Config $config */
|
2017-08-31 17:30:54 +02:00
|
|
|
$config = app('config');
|
|
|
|
|
2017-01-21 23:07:20 +01:00
|
|
|
if (empty($key)) {
|
2017-08-31 17:30:54 +02:00
|
|
|
return $config;
|
2017-01-21 23:07:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($key)) {
|
2017-08-31 17:30:54 +02:00
|
|
|
$config->set($key);
|
|
|
|
return true;
|
2017-01-21 23:07:20 +01:00
|
|
|
}
|
|
|
|
|
2017-08-31 17:30:54 +02:00
|
|
|
return $config->get($key, $default);
|
2017-01-21 23:07:20 +01:00
|
|
|
}
|
2017-07-18 21:38:53 +02:00
|
|
|
|
2022-12-14 00:28:34 +01:00
|
|
|
function config_path(string $path = ''): string
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
|
|
|
return app('path.config') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path);
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
function event(string|object|null $event = null, array $payload = []): array|EventDispatcher
|
2020-12-28 16:04:05 +01:00
|
|
|
{
|
|
|
|
/** @var EventDispatcher $dispatcher */
|
|
|
|
$dispatcher = app('events.dispatcher');
|
|
|
|
|
|
|
|
if (!is_null($event)) {
|
|
|
|
return $dispatcher->dispatch($event, $payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dispatcher;
|
|
|
|
}
|
|
|
|
|
2022-12-14 00:28:34 +01:00
|
|
|
function redirect(string $path, int $status = 302, array $headers = []): Response
|
2019-11-27 19:11:37 +01:00
|
|
|
{
|
|
|
|
/** @var Redirector $redirect */
|
|
|
|
$redirect = app('redirect');
|
|
|
|
|
|
|
|
return $redirect->to($path, $status, $headers);
|
|
|
|
}
|
|
|
|
|
2023-01-28 00:41:29 +01:00
|
|
|
/**
|
|
|
|
* @return mixed|Request
|
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
function request(string $key = null, mixed $default = null): mixed
|
2017-07-18 21:38:53 +02:00
|
|
|
{
|
2020-04-06 19:35:59 +02:00
|
|
|
/** @var Request $request */
|
2017-08-31 17:30:54 +02:00
|
|
|
$request = app('request');
|
2017-07-18 21:38:53 +02:00
|
|
|
|
|
|
|
if (is_null($key)) {
|
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request->input($key, $default);
|
|
|
|
}
|
2017-07-20 02:22:18 +02:00
|
|
|
|
2022-12-14 00:28:34 +01:00
|
|
|
function response(mixed $content = '', int $status = 200, array $headers = []): Response
|
2018-08-07 03:18:22 +02:00
|
|
|
{
|
2018-08-11 15:05:55 +02:00
|
|
|
/** @var Response $response */
|
2018-08-07 03:18:22 +02:00
|
|
|
$response = app('psr7.response');
|
|
|
|
$response = $response
|
2018-08-11 15:05:55 +02:00
|
|
|
->withContent($content)
|
2018-08-07 03:18:22 +02:00
|
|
|
->withStatus($status);
|
2018-08-11 15:05:55 +02:00
|
|
|
|
2018-08-07 03:18:22 +02:00
|
|
|
foreach ($headers as $key => $value) {
|
|
|
|
$response = $response->withAddedHeader($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2023-01-28 00:41:29 +01:00
|
|
|
/**
|
|
|
|
* @return mixed|SessionInterface
|
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
function session(string $key = null, mixed $default = null): mixed
|
2017-08-30 19:57:01 +02:00
|
|
|
{
|
2019-11-06 13:43:57 +01:00
|
|
|
/** @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);
|
|
|
|
}
|
|
|
|
|
2018-08-28 22:23:59 +02:00
|
|
|
/**
|
|
|
|
* Translate the given message
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
function trans(string $key = null, array $replace = []): string|Translator
|
2018-08-28 22:23:59 +02:00
|
|
|
{
|
|
|
|
/** @var Translator $translator */
|
|
|
|
$translator = app('translator');
|
|
|
|
|
|
|
|
if (is_null($key)) {
|
|
|
|
return $translator;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $translator->translate($key, $replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate the given message
|
|
|
|
*/
|
2022-12-14 00:28:34 +01:00
|
|
|
function __(string $key, array $replace = []): string
|
2018-08-28 22:23:59 +02:00
|
|
|
{
|
|
|
|
/** @var Translator $translator */
|
|
|
|
$translator = app('translator');
|
|
|
|
|
|
|
|
return $translator->translate($key, $replace);
|
|
|
|
}
|
|
|
|
|
2018-10-18 23:34:18 +02:00
|
|
|
/**
|
|
|
|
* Translate the given message
|
|
|
|
*/
|
2022-12-14 00:28:34 +01:00
|
|
|
function _e(string $key, string $keyPlural, int $number, array $replace = []): string
|
2018-10-18 23:34:18 +02:00
|
|
|
{
|
|
|
|
/** @var Translator $translator */
|
|
|
|
$translator = app('translator');
|
|
|
|
|
|
|
|
return $translator->translatePlural($key, $keyPlural, $number, $replace);
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
function url(string $path = null, array $parameters = []): UrlGeneratorInterface|string
|
2017-08-29 16:21:25 +02:00
|
|
|
{
|
2020-04-06 19:35:59 +02:00
|
|
|
/** @var UrlGeneratorInterface $urlGenerator */
|
2018-08-22 03:10:08 +02:00
|
|
|
$urlGenerator = app('http.urlGenerator');
|
2017-09-20 01:09:11 +02:00
|
|
|
|
|
|
|
if (is_null($path)) {
|
|
|
|
return $urlGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $urlGenerator->to($path, $parameters);
|
2017-08-29 16:21:25 +02:00
|
|
|
}
|
2017-09-22 14:02:02 +02:00
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
function view(string $template = null, array $data = []): Renderer|string
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
2020-04-06 19:35:59 +02:00
|
|
|
/** @var Renderer $renderer */
|
2017-09-22 14:02:02 +02:00
|
|
|
$renderer = app('renderer');
|
|
|
|
|
|
|
|
if (is_null($template)) {
|
|
|
|
return $renderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $renderer->render($template, $data);
|
|
|
|
}
|