-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.php
164 lines (145 loc) · 3.48 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
<?php
/**
* Contain helper functions which provides shortcuts for various classes.
*/
use Pecee\Application\Router;
use Pecee\Http\Request;
use Pecee\Http\Response;
use Pecee\Http\Url;
request()->app = new \Pecee\Application\Application();
/**
* Get url for a route by using either name/alias, class or method name.
*
* The name parameter supports the following values:
* - Route name
* - Controller/resource name (with or without method)
* - Controller class name
*
* When searching for controller/resource by name, you can use this syntax "route.name@method".
* You can also use the same syntax when searching for a specific controller-class "MyController@home".
* If no arguments is specified, it will return the url for the current loaded route.
*
* @param string|null $name
* @param string|array|null $parameters
* @param array|null $getParams
* @return \Pecee\Http\Url
* @throws \InvalidArgumentException
*/
function url(?string $name = null, $parameters = null, ?array $getParams = null): Url
{
return Router::getUrl($name, $parameters, $getParams);
}
/**
* @return \Pecee\Http\Response
*/
function response(): Response
{
return Router::response();
}
/**
* @return \Pecee\Http\Request
*/
function request(): Request
{
return Router::request();
}
/**
* Get input class
* @param string|null $index Parameter index name
* @param string|null $defaultValue Default return value
* @param array ...$methods Default methods
* @return \Pecee\Http\Input\InputHandler|array|string|null
*/
function input($index = null, $defaultValue = null, ...$methods)
{
if ($index !== null) {
return request()->getInputHandler()->value($index, $defaultValue, ...$methods);
}
return request()->getInputHandler();
}
/**
* @param string $url
* @param int|null $code
*/
function redirect(string $url, ?int $code = null): void
{
if ($code !== null) {
response()->httpCode($code);
}
response()->redirect($url);
}
/**
* Get main application class
*
* @return \Pecee\Application\Application
*/
function app()
{
return request()->app;
}
/**
* @param string $key
* @param string|array ...$args
* @return string
*/
function lang($key, ...$args): string
{
return app()->translation->translate($key, ...$args);
}
/**
* Add debug message.
* Requires DEBUG=1 to be present in your env file.
*
* @param string $group
* @param string $text
* @param ...$args
*/
function debug(string $group, string $text, ...$args): void
{
if (app()->getDebugEnabled() === true) {
app()->debug->add($group, $text, ...$args);
}
}
function add_module(string $name, string $path): void
{
app()->addModule($name, $path);
}
/**
* Get environment variable
* @param string $key
* @param string|null $default
*
* @return string|null
*/
if(function_exists('env') === false) {
function env(string $key, ?string $default = null): ?string
{
return $_ENV[$key] ?? $default;
}
}
/**
* Get current csrf-token
* @return string|null
*/
function csrf_token(): ?string
{
$baseVerifier = Router::router()->getCsrfVerifier();
if ($baseVerifier !== null) {
return $baseVerifier->getTokenProvider()->getToken();
}
return null;
}
/**
* Base basename for class
*
* @param string $class
* @return string
*/
function class_basename(string $class): string
{
$pos = strrpos($class, '\\');
if ($pos !== false) {
$class = substr($class, $pos + 1);
}
return strtolower(preg_replace('/(?<!^)([A-Z])/', '_\\1', $class));
}