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