forked from the-benchmarker/web-frameworks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.php
More file actions
326 lines (309 loc) · 8.1 KB
/
Copy pathhelpers.php
File metadata and controls
326 lines (309 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php declare(strict_types=1);
/*
* This file is part of App Project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package app
*/
use Framework\Helpers\ArraySimple;
use Framework\HTTP\Response;
use Framework\MVC\App;
use Framework\MVC\Model;
use Framework\Routing\Route;
use Framework\Session\Session;
use JetBrains\PhpStorm\Pure;
/**
* Load helper files.
*
* @param array<int,string>|string $helper A list of helper names as array
* or a helper name as string
*
* @return array<int,string> A list of all loaded files
*/
function helpers(array | string $helper) : array
{
if (is_array($helper)) {
$files = [];
foreach ($helper as $item) {
$files[] = helpers($item);
}
return array_merge(...$files);
}
$files = App::locator()->findFiles('Helpers/' . $helper);
foreach ($files as $file) {
require_once $file;
}
return $files;
}
/**
* Escape special characters to HTML entities.
*
* @param string|null $text The text to be escaped
* @param string $encoding The escaped text encoding
*
* @return string The escaped text
*/
#[Pure]
function esc(?string $text, string $encoding = 'UTF-8') : string
{
$text = (string) $text;
return empty($text)
? $text
: htmlspecialchars($text, \ENT_QUOTES | \ENT_HTML5, $encoding);
}
/**
* Renders a view.
*
* @param string $path View path
* @param array<string,mixed> $variables Variables passed to the view
* @param string $instance The View instance name
*
* @return string The rendered view contents
*/
function view(string $path, array $variables = [], string $instance = 'default') : string
{
return App::view($instance)->render($path, $variables);
}
/**
* Get the current URL.
*
* @return string
*/
function current_url() : string
{
return App::request()->getUrl()->toString();
}
/**
* Get the current Route.
*
* @return Framework\Routing\Route
*/
function current_route() : Route
{
return App::router()->getMatchedRoute();
}
/**
* Get a URL based in a Route name.
*
* @param string $name Route name
* @param array<mixed> $pathArgs Route path arguments
* @param array<mixed> $originArgs Route origin arguments
*
* @return string The Route URL
*/
function route_url(string $name, array $pathArgs = [], array $originArgs = []) : string
{
$route = App::router()->getNamedRoute($name);
$matched = App::router()->getMatchedRoute();
if (empty($originArgs)
&& $matched
&& $route->getOrigin() === $matched->getOrigin()
) {
$originArgs = App::router()->getMatchedOriginArguments();
}
return $route->getUrl($originArgs, $pathArgs);
}
/**
* Renders a language file line with dot notation format.
*
* e.g. home.hello matches 'home' for file and 'hello' for line.
*
* @param string $line The dot notation file line
* @param array<int|string,string> $args The arguments to be used in the
* formatted text
* @param string|null $locale A custom locale or null to use the current
*
* @return string|null The rendered text or null if not found
*/
function lang(string $line, array $args = [], string $locale = null) : ?string
{
return App::language()->lang($line, $args, $locale);
}
/**
* Get the Session instance.
*
* @return Framework\Session\Session
*/
function session() : Session
{
return App::session();
}
/**
* Get data from old redirect.
*
* @param string|null $key Set null to return all data
* @param bool $escape
*
* @see Framework\HTTP\Request::getRedirectData()
* @see Framework\HTTP\Response::redirect()
* @see redirect()
*
* @return mixed The old value. If $escape is true and the value is not
* stringable, an empty string will return
*/
function old(?string $key, bool $escape = true) : mixed
{
App::session()->activate();
$data = App::request()->getRedirectData($key);
if ($data !== null && $escape) {
$data = is_scalar($data) || (is_object($data) && method_exists($data, '__toString'))
? esc((string) $data)
: '';
}
return $data;
}
/**
* Tells if session has old data.
*
* @param string|null $key null to check all data or a specific key in the
* array simple format
*
* @see old()
*
* @return bool
*/
function has_old(string $key = null) : bool
{
App::session()->activate();
return App::request()->getRedirectData($key) !== null;
}
/**
* Renders the AntiCSRF input.
*
* @param string $instance The antiCsrf service instance name
*
* @return string An HTML hidden input if antiCsrf service is enabled or an
* empty string if it is disabled
*/
function csrf_input(string $instance = 'default') : string
{
return App::antiCsrf($instance)->input();
}
/**
* Set Response status as "404 Not Found" and auto set body as
* JSON or HTML page based on Request Content-Type header.
*
* @param array<string,mixed> $variables
*
* @return Framework\HTTP\Response
*/
function respond_not_found(array $variables = []) : Response
{
$request = App::request();
$response = App::response();
$response->setStatus(404);
if ($request->isJson() || $request->negotiateAccept([
'text/html',
'application/json',
]) === 'application/json') {
return $response->setJson([
'error' => [
'code' => 404,
'reason' => 'Not Found',
],
]);
}
$variables['title'] ??= lang('routing.error404');
$variables['message'] ??= lang('routing.pageNotFound');
return $response->setBody(
view('errors/404', $variables)
);
}
/**
* Sets the HTTP Redirect Response with data accessible in the next HTTP
* Request.
*
* @param string $location Location Header value
* @param array<int|string,mixed> $data Session data available on next
* Request
* @param int|null $code HTTP Redirect status code. Leave null to determine
* based on the current HTTP method.
*
* @see http://en.wikipedia.org/wiki/Post/Redirect/Get
* @see Framework\HTTP\Request::getRedirectData()
* @see old()
*
* @throws InvalidArgumentException for invalid Redirection code
*
* @return Framework\HTTP\Response
*/
function redirect(string $location, array $data = [], int $code = null) : Response
{
if ($data) {
App::session()->activate();
}
return App::response()->redirect($location, $data, $code);
}
/**
* Redirect to a named route.
*
* @param array<mixed>|string $route route name as string or an array with the
* route name, an array with path args and other array with origin args
* @param array<mixed> $data Session data available on next
* Request
* @param int|null $code HTTP Redirect status code. Leave null to determine
* based on the current HTTP method.
*
* @see http://en.wikipedia.org/wiki/Post/Redirect/Get
* @see Framework\HTTP\Request::getRedirectData()
* @see old()
* @see redirect()
*
* @throws InvalidArgumentException for invalid Redirection code
*
* @return Framework\HTTP\Response
*/
function redirect_to(
array | string $route,
array $data = [],
int $code = null
) : Response {
$route = (array) $route;
$route = route_url(...$route);
return redirect($route, $data, $code);
}
/**
* Get configs from a service.
*
* @param string $name The service name
* @param string $key The instance name and, optionally, with keys in the
* ArraySimple keys format
*
* @return mixed The key value
*/
function config(string $name, string $key = 'default') : mixed
{
[$instance, $keys] = array_pad(explode('[', $key, 2), 2, null);
$config = App::config()->get($name, $instance);
if ($keys === null) {
return $config;
}
$pos = strpos($keys, ']');
if ($pos === false) {
$pos = strlen($key);
}
$parent = substr($keys, 0, $pos);
$keys = substr($keys, $pos + 1);
$key = $parent . $keys;
return ArraySimple::value($key, $config);
}
/**
* Get same Model instance.
*
* @template T of Model
*
* @param class-string<T> $class
*
* @return T
*/
function model(string $class) : Model
{
static $models;
if ( ! isset($models[$class])) {
$models[$class] = new $class();
}
return $models[$class];
}