engelsystem/src/helpers.php

203 lines
4.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2017-09-19 14:50:20 +02:00
use Engelsystem\Application;
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;
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
* @return mixed|Application
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation
2017-08-31 17:30:54 +02: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);
}
function auth(): Authenticator
2018-10-08 21:15:56 +02:00
{
return app('authenticator');
2017-08-31 17:30:54 +02:00
}
function base_path(string $path = ''): string
{
return app('path') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path);
}
function back(int $status = 302, array $headers = []): Response
{
/** @var Redirector $redirect */
$redirect = app('redirect');
return $redirect->back($status, $headers);
}
/**
* Get or set config values
* @return mixed|Config
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation
*/
function config(string|array $key = null, mixed $default = null): mixed
{
/** @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);
}
function config_path(string $path = ''): string
{
return app('path.config') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path);
}
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;
}
function redirect(string $path, int $status = 302, array $headers = []): Response
{
/** @var Redirector $redirect */
$redirect = app('redirect');
return $redirect->to($path, $status, $headers);
}
/**
* @return mixed|Request
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation
*/
function request(string $key = null, mixed $default = null): mixed
{
2020-04-06 19:35:59 +02:00
/** @var Request $request */
2017-08-31 17:30:54 +02:00
$request = app('request');
if (is_null($key)) {
return $request;
}
return $request->input($key, $default);
}
function response(mixed $content = '', int $status = 200, array $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;
}
/**
* @return mixed|SessionInterface
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation
*/
function session(string $key = null, mixed $default = null): mixed
2017-08-30 19:57:01 +02: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);
}
/**
* Translate the given message
*/
function trans(string $key = null, array $replace = []): string|Translator
{
/** @var Translator $translator */
$translator = app('translator');
if (is_null($key)) {
return $translator;
}
return $translator->translate($key, $replace);
}
/**
* Translate the given message
*/
function __(string $key, array $replace = []): string
{
/** @var Translator $translator */
$translator = app('translator');
return $translator->translate($key, $replace);
}
/**
* Translate the given message
*/
function _e(string $key, string $keyPlural, int $number, array $replace = []): string
{
/** @var Translator $translator */
$translator = app('translator');
return $translator->translatePlural($key, $keyPlural, $number, $replace);
}
function url(string $path = null, array $parameters = []): UrlGeneratorInterface|string
{
2020-04-06 19:35:59 +02:00
/** @var UrlGeneratorInterface $urlGenerator */
$urlGenerator = app('http.urlGenerator');
2017-09-20 01:09:11 +02:00
if (is_null($path)) {
return $urlGenerator;
}
return $urlGenerator->to($path, $parameters);
}
function view(string $template = null, array $data = []): Renderer|string
{
2020-04-06 19:35:59 +02:00
/** @var Renderer $renderer */
$renderer = app('renderer');
if (is_null($template)) {
return $renderer;
}
return $renderer->render($template, $data);
}