Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
60 / 60
100.00% covered (success)
100.00%
18 / 18
CRAP
n/a
0 / 0
app
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
auth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
base_path
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
back
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
cache
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
config
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
config_path
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
env_secret
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
event
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
redirect
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
request
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
response
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
session
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
trans
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
__
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
_e
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
url
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
view
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5use Engelsystem\Application;
6use Engelsystem\Config\Config;
7use Engelsystem\Events\EventDispatcher;
8use Engelsystem\Helpers\Authenticator;
9use Engelsystem\Helpers\Cache;
10use Engelsystem\Helpers\Translation\Translator;
11use Engelsystem\Http\Redirector;
12use Engelsystem\Http\Request;
13use Engelsystem\Http\Response;
14use Engelsystem\Http\UrlGeneratorInterface;
15use Engelsystem\Renderer\Renderer;
16use Symfony\Component\HttpFoundation\Session\SessionInterface;
17
18/**
19 * Get the global app instance
20 * @return mixed|Application
21 * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation
22 */
23function 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
32function auth(): Authenticator
33{
34    return app('authenticator');
35}
36
37function base_path(string $path = ''): string
38{
39    return app('path') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path);
40}
41
42function 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
50function 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 */
67function 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
84function 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 */
94function 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
104function 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
116function 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 */
128function 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
140function 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 */
159function 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 */
174function 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 */
189function __(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 */
200function _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
208function 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
220function 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}