-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
385 lines (349 loc) · 12.6 KB
/
helpers.php
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/*
|-----------------------------------------------------------------------------
| Application Functions
|-----------------------------------------------------------------------------
|
| This file contains commonly used functions in the application. These
| functions provide utility for our Framework.
|
|-----------------------------------------------------------------------------
*/
use Framework\Component\Application;
use Framework\Component\Config\ConfigRepository;
use Framework\Component\Container;
use Framework\Component\View;
use Framework\Http\HeaderBag;
use Framework\Http\Redirector;
use Framework\Http\RedirectResponse;
use Framework\Http\Request;
use Framework\Http\Response;
use Framework\Http\Server;
use Framework\Routing\Generator\UrlGenerator;
use Framework\Routing\Router;
use Framework\Session\Session;
if (!function_exists('redirect')) {
/**
* Redirect to a specified route.
*
* @param string|null $route [optional] The route to redirect to. If null, path of redirect should be set using 'route' method instead.
* @return RedirectResponse A RedirectResponse instance representing the redirection.
*/
function redirect(string $route = null): RedirectResponse
{
$redirector = Application::get_instance()->get(Redirector::class);
return is_null($route) ? $redirector->make() : $redirector->to($route);
}
}
if (!function_exists('response')) {
/**
* Helper function to create an instance of the Response class.
*
* @param mixed $content The content of the response.
* @param int $status_code [optional] The HTTP status code of the response. Default is 200 (OK).
* @param HeaderBag|null $headers [optional] The HeaderBag instance containing HTTP headers (default is an empty HeaderBag).
* @return Response The created Response instance.
*/
function response($content = null, int $status_code = Response::HTTP_OK, HeaderBag $headers = null): Response
{
return new Response($content, $status_code, $headers ?: new HeaderBag());
}
}
if (!function_exists('view')) {
/**
* Create a new View instance for rendering views.
*
* @param string $path The path to the view file.
* @param array $parameters [optional] Parameters to pass to the view.
* @return View
*/
function view(string $path, array $parameters = []): View
{
return new View($path, $parameters);
}
}
if (!function_exists('render_view')) {
/**
* Get and render view.
*
* @param string $path The path to the view file.
* @param array $parameters [optional] Parameters to pass to the view.
*/
function render_view(string $path, array $parameters = []): void
{
echo (new View($path, $parameters))->render();
}
}
if (!function_exists('resource_path')) {
/**
* Get the path to the 'resources' directory.
*
* @param string|null $path [optional] Additional path within the 'resources' directory.
* @return string The absolute path to the 'resources' directory or its subdirectory.
*/
function resource_path(string $path = null): string
{
return Application::get_instance()->base_path('resources/') . ltrim($path, '/');
}
}
if (!function_exists('public_path')) {
/**
* Get the path to the 'public' directory.
*
* @param string|null $path [optional] Additional path within the 'public' directory.
* @return string The absolute path to the 'public' directory or its subdirectory.
*/
function public_path(string $path = null): string
{
return Application::get_instance()->base_path('public/') . ltrim($path, '/');
}
}
if (!function_exists('storage_path')) {
/**
* Get the path to the 'storage' directory.
*
* @param string|null $path [optional] Additional path within the 'storage' directory.
* @return string The absolute path to the 'storage' directory or its subdirectory.
*/
function storage_path(string $path = null): string
{
return Application::get_instance()->base_path('storage/') . ltrim($path, '/');
}
}
if (!function_exists('session')) {
/**
* Get or set a session value.
*
* If both $key and $value are provided, it sets the session value.
* If only $key is provided, it retrieves the session value.
*
* @template T
* @param string|array|null $key [optional] The key of the session value.
* @param T|null $value [optional] The value to set for the session key.
* @return Session|T|string
*/
function session($key = null, $value = null)
{
$session = Application::get_instance()->get(Session::class);
if (is_array($key) && is_null($value)) {
foreach ($key as $k => $v) {
$session->put($k, $v);
}
return $session;
}
if (!is_null($key) && !is_null($value)) {
return $session->put($key, $value);
}
if (!is_null($key) && is_null($value)) {
return $session->get($key);
}
return $session;
}
}
if (!function_exists('error')) {
/**
* Get the error associated with the given key from the session.
*
* @param string $key The key to retrieve the error.
* @return string|null The error message or null if not found.
*/
function error(string $key): ?string
{
$errors = Application::get_instance()->get(Session::class)->get('errors.form.' . $key, []);
return !empty($errors) ? $errors[0] : null;
}
}
if (!function_exists('route')) {
/**
* Get URL for a named route.
*
* @param string $name The name of the route.
* @param array $parameters [optional] Associative array of route parameters.
* @return string|null The URL for the named route with parameters applied.
*/
function route(string $name, array $parameters = []): ?string
{
return Application::get_instance()->get(Router::class)->route($name, $parameters);
}
}
if (!function_exists('server')) {
/**
* Get server value by key or retrieve the entire Server instance.
*
* @param string|null $key The key to retrieve from the server. If null, the entire Server instance is returned.
* @return mixed|Server If $key is provided, the value associated with that key from the server is returned. If $key is null, the entire Server instance is returned.
*/
function server(string $key = null)
{
$server = Application::get_instance()->get(Server::class);
return $key ? $server->get($key) : $server;
}
}
if (!function_exists('request')) {
/**
* Get the current request instance.
*
* This function provides a convenient way to obtain the current request object
* throughout the application. It ensures that only a single instance of the
* Request class is created and reused.
*
* @return Request The instance of the Request class.
*/
function request(): Request
{
return Application::get_instance()->get(Request::class);
}
}
if (!function_exists('old')) {
/**
* Retrieve the previous input value for a given key from the session.
*
* This function is commonly used in the context of form submissions
* where validation fails, and you need to repopulate form fields
* with the previously submitted values.
*
* @param string $key The key for which the previous input value should be retrieved.
* @param string|null $default [optional] The default value if the previous input value cannot be retrieved.
* @return mixed Returns the previous input value for the specified key or null if not found.
*/
function old(string $key, ?string $default = null)
{
return Application::get_instance()->get(Request::class)->old($key) ?? $default;
}
}
if (!function_exists('config')) {
/**
* Get the value of a configuration key or set a configuration value at runtime.
*
* If $key is null, it retrieves the entire configuration array. If $key
* is an array, it sets multiple configuration values at once. If $key is a
* string, it retrieves the value for the specified key.
*
* If $key is null and $default is provided, the default value will be
* returned if the configuration key is not found.
*
* If $key is an array, it sets multiple configuration values at runtime and
* returns the array of key-value pairs that were set.
*
* @template T
* @param string|array<T>|null $key [optional] The configuration key or an array of key-value pairs to set.
* @param T $default [optional] The default value to return if the key is not found.
* @return T|array The value of the configuration key, the entire configuration array, or the default value.
*/
function config($key = null, $default = null)
{
$config = Application::get_instance()->get(ConfigRepository::class);
if (is_null($key)) {
return $config->all();
}
if (is_array($key)) {
$config->set($key);
return $key;
}
return $config->get($key, $default);
}
}
if (!function_exists('dd')) {
/**
* Dump and die function for quick debugging.
*
* @param mixed|null $message The variable or message to be dumped.
* @return void
*/
function dd(...$message)
{
if (!empty($message)) {
var_dump($message);
}
die();
}
}
if (!function_exists('get')) {
/**
* Get an instance of the specified class from the Container.
*
* This function acts as a convenient entry point to retrieve instances of
* classes from the service container. The container serves as a collection of instances of
* classes used in this application, allowing for reuse and reference to instances without declaring
* anything on a global scope.
*
* If no class name is provided, the function returns the service container itself.
*
* @template T
* @param class-string<T> $abstract The fully qualified class name to resolve.
* @return T An instance of the specified class.
*
* @see Container
*/
function get(string $abstract)
{
return Application::get_instance()->get($abstract);
}
}
if (!function_exists('base_path')) {
/**
* Get the absolute path to the Base directory of the application.
*
* @param string|null $path [optional] The relative path to append to the Base path.
* @return string The absolute path to the Base directory of the application.
*/
function base_path(string $path = null): string
{
return Application::get_instance()->base_path($path);
}
}
if (!function_exists('normalize_path')) {
/**
* Normalizes a file path by converting backslashes to slashes.
*
* This function replaces all occurrences of backslashes with forward slashes (/)
* in the given file path string.
*
* @param string $input The file path string to normalize.
* @return string The normalized file path with backslashes converted to slashes.
*/
function normalize_path(string $input): string
{
return str_replace(DIRECTORY_SEPARATOR, '/', $input);
}
}
if (!function_exists('back')) {
/**
* Generate a redirect response back to the previous page.
*
* This function creates a redirect response to the URL specified in the 'Referer' header
* or defaults to the home URL if the 'Referer' header is not present.
*
* @return RedirectResponse
*/
function back(): RedirectResponse
{
return Application::get_instance()->get(Redirector::class)->back();
}
}
if (!function_exists('url')) {
/**
* Generate a URL based on the given route.
*
* @param string|null $path [optional] The path for the URL.
* @param array $parameters [optional] Query parameters for the URL.
* @return UrlGenerator|string The generated URL or UrlGenerator if $path is not specified.
*/
function url(string $path = null, array $parameters = [], bool $absolute = true)
{
$url = Application::get_instance()->get(UrlGenerator::class);
return $path ? $url->to($path, $parameters, $absolute) : $url;
}
}
if (!function_exists('asset')) {
/**
* Generates the URL for an asset based on the provided relative path.
*
* @param string $path The relative path to the asset.
* @return string The full URL for the asset.
*/
function asset(string $path): string
{
return Application::get_instance()->get(UrlGenerator::class)->to('public/') . trim($path, '/');
}
}