Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
60 / 60 |
|
100.00% |
18 / 18 |
CRAP | n/a |
0 / 0 |
|
| app | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| auth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| base_path | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| back | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| cache | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| config | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| config_path | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| env_secret | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| event | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| redirect | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| request | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| response | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| session | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| trans | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| __ | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| _e | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| url | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| view | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | use Engelsystem\Application; |
| 6 | use Engelsystem\Config\Config; |
| 7 | use Engelsystem\Events\EventDispatcher; |
| 8 | use Engelsystem\Helpers\Authenticator; |
| 9 | use Engelsystem\Helpers\Cache; |
| 10 | use Engelsystem\Helpers\Translation\Translator; |
| 11 | use Engelsystem\Http\Redirector; |
| 12 | use Engelsystem\Http\Request; |
| 13 | use Engelsystem\Http\Response; |
| 14 | use Engelsystem\Http\UrlGeneratorInterface; |
| 15 | use Engelsystem\Renderer\Renderer; |
| 16 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 17 | |
| 18 | /** |
| 19 | * Get the global app instance |
| 20 | * @return mixed|Application |
| 21 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation |
| 22 | */ |
| 23 | function app(?string $id = null): mixed |
| 24 | { |
| 25 | if (is_null($id)) { |
| 26 | return Application::getInstance(); |
| 27 | } |
| 28 | |
| 29 | return Application::getInstance()->get($id); |
| 30 | } |
| 31 | |
| 32 | function auth(): Authenticator |
| 33 | { |
| 34 | return app('authenticator'); |
| 35 | } |
| 36 | |
| 37 | function base_path(string $path = ''): string |
| 38 | { |
| 39 | return app('path') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); |
| 40 | } |
| 41 | |
| 42 | function back(int $status = 302, array $headers = []): Response |
| 43 | { |
| 44 | /** @var Redirector $redirect */ |
| 45 | $redirect = app('redirect'); |
| 46 | |
| 47 | return $redirect->back($status, $headers); |
| 48 | } |
| 49 | |
| 50 | function cache(string|null $key = null, mixed $default = null, int $seconds = 60 * 60): mixed |
| 51 | { |
| 52 | /** @var Cache $cache */ |
| 53 | $cache = app('cache'); |
| 54 | |
| 55 | if (empty($key)) { |
| 56 | return $cache; |
| 57 | } |
| 58 | |
| 59 | return $cache->get($key, $default, $seconds); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get or set config values |
| 64 | * @return mixed|Config |
| 65 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation |
| 66 | */ |
| 67 | function config(string|array|null $key = null, mixed $default = null): mixed |
| 68 | { |
| 69 | /** @var Config $config */ |
| 70 | $config = app('config'); |
| 71 | |
| 72 | if (empty($key)) { |
| 73 | return $config; |
| 74 | } |
| 75 | |
| 76 | if (is_array($key)) { |
| 77 | $config->set($key); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | return $config->get($key, $default); |
| 82 | } |
| 83 | |
| 84 | function config_path(string $path = ''): string |
| 85 | { |
| 86 | return app('path.config') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get a password from an environment variable. If an environment variable |
| 91 | * called `${var}_FILE` is set, read the password from that file. Otherwise |
| 92 | * returns the content of the `$var` environment variable. |
| 93 | */ |
| 94 | function env_secret(string $var, mixed $default = null): string | null |
| 95 | { |
| 96 | $filename = env($var . '_FILE'); |
| 97 | if ($filename && file_exists($filename)) { |
| 98 | return file_get_contents($filename); |
| 99 | } |
| 100 | |
| 101 | return env($var, $default); |
| 102 | } |
| 103 | |
| 104 | function event(string|object|null $event = null, array $payload = []): array|EventDispatcher |
| 105 | { |
| 106 | /** @var EventDispatcher $dispatcher */ |
| 107 | $dispatcher = app('events.dispatcher'); |
| 108 | |
| 109 | if (!is_null($event)) { |
| 110 | return $dispatcher->dispatch($event, $payload); |
| 111 | } |
| 112 | |
| 113 | return $dispatcher; |
| 114 | } |
| 115 | |
| 116 | function redirect(string $path, int $status = 302, array $headers = []): Response |
| 117 | { |
| 118 | /** @var Redirector $redirect */ |
| 119 | $redirect = app('redirect'); |
| 120 | |
| 121 | return $redirect->to($path, $status, $headers); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @return mixed|Request |
| 126 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation |
| 127 | */ |
| 128 | function request(?string $key = null, mixed $default = null): mixed |
| 129 | { |
| 130 | /** @var Request $request */ |
| 131 | $request = app('request'); |
| 132 | |
| 133 | if (is_null($key)) { |
| 134 | return $request; |
| 135 | } |
| 136 | |
| 137 | return $request->input($key, $default); |
| 138 | } |
| 139 | |
| 140 | function response(mixed $content = '', int $status = 200, array $headers = []): Response |
| 141 | { |
| 142 | /** @var Response $response */ |
| 143 | $response = app('psr7.response'); |
| 144 | $response = $response |
| 145 | ->withContent($content) |
| 146 | ->withStatus($status); |
| 147 | |
| 148 | foreach ($headers as $key => $value) { |
| 149 | $response = $response->withAddedHeader($key, $value); |
| 150 | } |
| 151 | |
| 152 | return $response; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @return mixed|SessionInterface |
| 157 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation |
| 158 | */ |
| 159 | function session(?string $key = null, mixed $default = null): mixed |
| 160 | { |
| 161 | /** @var SessionInterface $session */ |
| 162 | $session = app('session'); |
| 163 | |
| 164 | if (is_null($key)) { |
| 165 | return $session; |
| 166 | } |
| 167 | |
| 168 | return $session->get($key, $default); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Translate the given message |
| 173 | */ |
| 174 | function trans(?string $key = null, array $replace = []): string|Translator |
| 175 | { |
| 176 | /** @var Translator $translator */ |
| 177 | $translator = app('translator'); |
| 178 | |
| 179 | if (is_null($key)) { |
| 180 | return $translator; |
| 181 | } |
| 182 | |
| 183 | return $translator->translate($key, $replace); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Translate the given message |
| 188 | */ |
| 189 | function __(string $key, array $replace = []): string |
| 190 | { |
| 191 | /** @var Translator $translator */ |
| 192 | $translator = app('translator'); |
| 193 | |
| 194 | return $translator->translate($key, $replace); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Translate the given message |
| 199 | */ |
| 200 | function _e(string $key, string $keyPlural, int $number, array $replace = []): string |
| 201 | { |
| 202 | /** @var Translator $translator */ |
| 203 | $translator = app('translator'); |
| 204 | |
| 205 | return $translator->translatePlural($key, $keyPlural, $number, $replace); |
| 206 | } |
| 207 | |
| 208 | function url(?string $path = null, array $parameters = []): UrlGeneratorInterface|string |
| 209 | { |
| 210 | /** @var UrlGeneratorInterface $urlGenerator */ |
| 211 | $urlGenerator = app('http.urlGenerator'); |
| 212 | |
| 213 | if (is_null($path)) { |
| 214 | return $urlGenerator; |
| 215 | } |
| 216 | |
| 217 | return $urlGenerator->to($path, $parameters); |
| 218 | } |
| 219 | |
| 220 | function view(?string $template = null, array $data = []): Renderer|string |
| 221 | { |
| 222 | /** @var Renderer $renderer */ |
| 223 | $renderer = app('renderer'); |
| 224 | |
| 225 | if (is_null($template)) { |
| 226 | return $renderer; |
| 227 | } |
| 228 | |
| 229 | return $renderer->render($template, $data); |
| 230 | } |