|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.7 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
use Quantum\Environment\Environment; |
|
16
|
|
|
use Quantum\Router\RouteController; |
|
17
|
|
|
use Quantum\Router\Router; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Gets current middlewares |
|
21
|
|
|
* @return array|null |
|
22
|
|
|
*/ |
|
23
|
|
|
function current_middlewares(): ?array |
|
24
|
|
|
{ |
|
25
|
|
|
return RouteController::getCurrentRoute()['middlewares'] ?? null; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Gets current module |
|
30
|
|
|
* @return string|null |
|
31
|
|
|
*/ |
|
32
|
|
|
function current_module(): ?string |
|
33
|
|
|
{ |
|
34
|
|
|
return RouteController::getCurrentRoute()['module'] ?? null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get current controller |
|
39
|
|
|
* @return string|null |
|
40
|
|
|
*/ |
|
41
|
|
|
function current_controller(): ?string |
|
42
|
|
|
{ |
|
43
|
|
|
return RouteController::getCurrentRoute()['controller'] ?? null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Gets current action |
|
48
|
|
|
* @return string|null |
|
49
|
|
|
*/ |
|
50
|
|
|
function current_action(): ?string |
|
51
|
|
|
{ |
|
52
|
|
|
return RouteController::getCurrentRoute()['action'] ?? null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get current callback |
|
57
|
|
|
* @return Closure $callback|null |
|
58
|
|
|
*/ |
|
59
|
|
|
function route_callback(): ?Closure |
|
60
|
|
|
{ |
|
61
|
|
|
return RouteController::getCurrentRoute()['callback'] ?? null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Gets current route |
|
66
|
|
|
* @return string|null |
|
67
|
|
|
*/ |
|
68
|
|
|
function current_route(): ?string |
|
69
|
|
|
{ |
|
70
|
|
|
return RouteController::getCurrentRoute()['route'] ?? null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Gets current route parameters |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
function route_params(): array |
|
78
|
|
|
{ |
|
79
|
|
|
return RouteController::getCurrentRoute()['params'] ?? []; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Gets route parameter by name |
|
84
|
|
|
* @param string $name |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
function route_param(string $name) |
|
88
|
|
|
{ |
|
89
|
|
|
$params = RouteController::getCurrentRoute()['params']; |
|
90
|
|
|
|
|
91
|
|
|
if ($params) { |
|
92
|
|
|
foreach ($params as $param) { |
|
93
|
|
|
if ($param['name'] == $name) { |
|
94
|
|
|
return $param['value']; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Gets current route pattern |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
function route_pattern(): string |
|
107
|
|
|
{ |
|
108
|
|
|
return RouteController::getCurrentRoute()['pattern'] ?? ''; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Gets current route method |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
function route_method(): string |
|
116
|
|
|
{ |
|
117
|
|
|
return RouteController::getCurrentRoute()['method'] ?? ''; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Gets the current route uri |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
function route_uri(): string |
|
125
|
|
|
{ |
|
126
|
|
|
return RouteController::getCurrentRoute()['uri'] ?? ''; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Gets the current route cache settings |
|
131
|
|
|
* @return array|null |
|
132
|
|
|
*/ |
|
133
|
|
|
function route_cache_settings(): ?array |
|
134
|
|
|
{ |
|
135
|
|
|
return RouteController::getCurrentRoute()['cache_settings'] ?? null; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Gets the current route name |
|
140
|
|
|
* @return string|null |
|
141
|
|
|
*/ |
|
142
|
|
|
function route_name(): ?string |
|
143
|
|
|
{ |
|
144
|
|
|
return RouteController::getCurrentRoute()['name'] ?? null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Gets the current route name |
|
149
|
|
|
* @return string|null |
|
150
|
|
|
*/ |
|
151
|
|
|
function route_prefix(): ?string |
|
152
|
|
|
{ |
|
153
|
|
|
return RouteController::getCurrentRoute()['prefix'] ?? null; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Finds the route by name in given module scope |
|
158
|
|
|
* @param string $name |
|
159
|
|
|
* @param string $module |
|
160
|
|
|
* @return array|null |
|
161
|
|
|
*/ |
|
162
|
|
|
function find_route_by_name(string $name, string $module): ?array |
|
163
|
|
|
{ |
|
164
|
|
|
foreach (Router::getRoutes() as $route) { |
|
165
|
|
|
if (isset($route['name']) && |
|
166
|
|
|
strtolower($route['name']) == strtolower($name) && |
|
167
|
|
|
strtolower($route['module']) == strtolower($module)) { |
|
168
|
|
|
|
|
169
|
|
|
return $route; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return null; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Checks the existence of the route group by name in given module scope |
|
178
|
|
|
* @param string $name |
|
179
|
|
|
* @param string $module |
|
180
|
|
|
* @return bool |
|
181
|
|
|
*/ |
|
182
|
|
|
function route_group_exists(string $name, string $module): bool |
|
183
|
|
|
{ |
|
184
|
|
|
foreach (Router::getRoutes() as $route) { |
|
185
|
|
|
if (isset($route['group']) && |
|
186
|
|
|
strtolower($route['group']) == strtolower($name) && |
|
187
|
|
|
strtolower($route['module']) == strtolower($module)) { |
|
188
|
|
|
|
|
189
|
|
|
return true; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return false; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Gets the module base namespace depending on env |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
|
|
function module_base_namespace(): string |
|
201
|
|
|
{ |
|
202
|
|
|
return Environment::getInstance()->getAppEnv() === 'testing' |
|
203
|
|
|
? "Quantum\\Tests\\_root\\modules" |
|
204
|
|
|
: "Modules"; |
|
205
|
|
|
} |