1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Resta\Router;
|
4
|
|
|
|
5
|
|
|
use Resta\Support\Str;
|
6
|
|
|
use Resta\Support\Utils;
|
7
|
|
|
use Resta\Foundation\PathManager\StaticPathList;
|
8
|
|
|
|
9
|
|
|
class Route extends RouteHttpManager
|
10
|
|
|
{
|
11
|
|
|
// get route acccessible property
|
12
|
|
|
use RouteAccessiblePropertyTrait;
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* @var array $endpoints
|
16
|
|
|
*/
|
17
|
|
|
protected static $endpoints = [];
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @var array $routes
|
21
|
|
|
*/
|
22
|
|
|
protected static $routes = [];
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* @var array $paths
|
26
|
|
|
*/
|
27
|
|
|
protected static $paths = [];
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @var array $mappers
|
31
|
|
|
*/
|
32
|
|
|
protected static $mappers = [];
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* @var null|string $namespace
|
36
|
|
|
*/
|
37
|
|
|
protected static $namespace;
|
38
|
|
|
|
39
|
|
|
/**
|
40
|
|
|
* get route checkArrayEqual
|
41
|
|
|
*
|
42
|
|
|
* @param $patterns
|
43
|
|
|
* @param $urlRoute
|
44
|
|
|
* @return int|string
|
45
|
|
|
*/
|
46
|
|
|
public static function checkArrayEqual($patterns,$urlRoute)
|
47
|
|
|
{
|
48
|
|
|
// calculates the equality difference between
|
49
|
|
|
// the route pattern and the urlRoute value.
|
50
|
|
|
foreach ($patterns as $key=>$pattern){
|
51
|
|
|
|
52
|
|
|
if(Utils::isArrayEqual($pattern,$urlRoute)){
|
53
|
|
|
return $key;
|
54
|
|
|
}
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
// if the difference is not equal,
|
58
|
|
|
// null is returned.
|
59
|
|
|
return null;
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* get route calling for http
|
64
|
|
|
*
|
65
|
|
|
* @return array|mixed
|
66
|
|
|
*/
|
67
|
|
|
public static function getCallingRoute()
|
68
|
|
|
{
|
69
|
|
|
if(app()->has('routeResolvedData')){
|
70
|
|
|
return app()->get('routeResolvedData');
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
return [];
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
/**
|
77
|
|
|
* get route controller class
|
78
|
|
|
*
|
79
|
|
|
* @return string
|
80
|
|
|
*/
|
81
|
|
|
public static function getRouteControllerClass()
|
82
|
|
|
{
|
83
|
|
|
$route = self::getRouteResolve();
|
84
|
|
|
|
85
|
|
|
if(isset($route['class'])){
|
86
|
|
|
return $route['class'];
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
return null;
|
90
|
|
|
}
|
91
|
|
|
|
92
|
|
|
/**
|
93
|
|
|
* get route controller class
|
94
|
|
|
*
|
95
|
|
|
* @return string
|
96
|
|
|
*/
|
97
|
|
|
public static function getRouteControllerMethod()
|
98
|
|
|
{
|
99
|
|
|
$route = self::getRouteResolve();
|
100
|
|
|
|
101
|
|
|
if(isset($route['method'])){
|
102
|
|
|
return $route['method'];
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
return null;
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
/**
|
109
|
|
|
* get route controller namespace
|
110
|
|
|
*
|
111
|
|
|
* @return string
|
112
|
|
|
*/
|
113
|
|
|
public static function getRouteControllerNamespace()
|
114
|
|
|
{
|
115
|
|
|
$route = self::getRouteResolve();
|
116
|
|
|
|
117
|
|
|
if(isset($route['controller'],$route['namespace'])){
|
118
|
|
|
return $route['controller'].'/'.$route['namespace'];
|
119
|
|
|
}
|
120
|
|
|
|
121
|
|
|
return null;
|
122
|
|
|
}
|
123
|
|
|
|
124
|
|
|
/**
|
125
|
|
|
* get route getRouteResolve
|
126
|
|
|
*
|
127
|
|
|
* @return array
|
128
|
|
|
*/
|
129
|
|
|
public static function getRouteResolve()
|
130
|
|
|
{
|
131
|
|
|
// get routes data and the resolving pattern
|
132
|
|
|
// Both are interrelated.
|
133
|
|
|
$routes = self::getRoutes();
|
134
|
|
|
$patternResolve = app()->resolve(RouteMatching::class,['route'=>new self()])->getPatternResolve();
|
135
|
|
|
|
136
|
|
|
// we set the route variables for the route assistant.
|
137
|
|
|
self::updateRouteParameters($patternResolve);
|
138
|
|
|
|
139
|
|
|
//if routes data is available in pattern resolve.
|
140
|
|
|
if(isset($routes['data'][$patternResolve])){
|
141
|
|
|
|
142
|
|
|
// if the incoming http value is
|
143
|
|
|
// the same as the real request method, the data is processed.
|
144
|
|
|
if($routes['data'][$patternResolve]['http'] == strtolower(httpMethod)){
|
145
|
|
|
|
146
|
|
|
// we are set the solved pattern to a variable.
|
147
|
|
|
$resolve = $routes['data'][$patternResolve];
|
148
|
|
|
|
149
|
|
|
$routeResolvedData = [
|
150
|
|
|
'class' => $resolve['class'],
|
151
|
|
|
'method' => $resolve['method'],
|
152
|
|
|
'controller' => $resolve['controller'],
|
153
|
|
|
'namespace' => $resolve['namespace'],
|
154
|
|
|
'http' => httpMethod(),
|
155
|
|
|
];
|
156
|
|
|
|
157
|
|
|
app()->register('routeResolvedData',$routeResolvedData);
|
158
|
|
|
|
159
|
|
|
return $routeResolvedData;
|
160
|
|
|
}
|
161
|
|
|
}
|
162
|
|
|
|
163
|
|
|
return [];
|
164
|
|
|
}
|
165
|
|
|
|
166
|
|
|
/**
|
167
|
|
|
* update route parameters
|
168
|
|
|
*
|
169
|
|
|
* @param $patternResolvedKey
|
170
|
|
|
* @return mixed|void
|
171
|
|
|
*/
|
172
|
|
|
private static function updateRouteParameters($patternResolvedKey)
|
173
|
|
|
{
|
174
|
|
|
$list = [];
|
175
|
|
|
|
176
|
|
|
if(isset(static::$routes['pattern'][$patternResolvedKey])){
|
177
|
|
|
|
178
|
|
|
$routeParameters = static::$routes['pattern'][$patternResolvedKey];
|
179
|
|
|
$route = route();
|
180
|
|
|
|
181
|
|
|
foreach($routeParameters as $key=>$param){
|
182
|
|
|
|
183
|
|
|
$param = Str::replaceWordArray(['{','}','?'],'',$param);
|
184
|
|
|
|
185
|
|
|
if(isset($route[$key])){
|
186
|
|
|
$list[$param] = $route[$key];
|
187
|
|
|
}
|
188
|
|
|
}
|
189
|
|
|
}
|
190
|
|
|
|
191
|
|
|
app()->register('routeParams',$list);
|
192
|
|
|
}
|
193
|
|
|
|
194
|
|
|
/**
|
195
|
|
|
* route handle for route application
|
196
|
|
|
*
|
197
|
|
|
* @param bool $endpoint
|
198
|
|
|
*/
|
199
|
|
|
public function handle($endpoint=true)
|
200
|
|
|
{
|
201
|
|
|
// we will record the path data for the route.
|
202
|
|
|
// We set the routeMapper variables and the route path.
|
203
|
|
|
self::setPath(function(){
|
204
|
|
|
|
205
|
|
|
// we are sending
|
206
|
|
|
// the controller and routes.php path.
|
207
|
|
|
return [
|
208
|
|
|
'controllerPath' => path()->controller(),
|
209
|
|
|
'routePath' => path()->route(),
|
210
|
|
|
];
|
211
|
|
|
},$endpoint);
|
212
|
|
|
|
213
|
|
|
// in the paths data,
|
214
|
|
|
// we run the route mapper values and the route files one by one.
|
215
|
|
|
foreach (self::$paths as $mapper=>$controller){
|
216
|
|
|
core()->fileSystem->callFile($mapper);
|
217
|
|
|
}
|
218
|
|
|
}
|
219
|
|
|
|
220
|
|
|
/**
|
221
|
|
|
* get route set path
|
222
|
|
|
*
|
223
|
|
|
* @param callable $callback
|
224
|
|
|
* @param bool $endpoint
|
225
|
|
|
*/
|
226
|
|
|
public static function setPath(callable $callback,$endpoint=true)
|
227
|
|
|
{
|
228
|
|
|
$routeDefinitor = call_user_func($callback);
|
229
|
|
|
|
230
|
|
|
if(isset($routeDefinitor['controllerPath']) && isset($routeDefinitor['routePath'])){
|
231
|
|
|
|
232
|
|
|
//the route paths to be saved to the mappers static property.
|
233
|
|
|
static::$mappers['routePaths'][] = $routeDefinitor['routePath'];
|
234
|
|
|
static::$mappers['controllerNamespaces'][] = Utils::getNamespace($routeDefinitor['controllerPath']);
|
235
|
|
|
|
236
|
|
|
// if there is endpoint,
|
237
|
|
|
// then only that endpoint is transferred into the path
|
238
|
|
|
if(defined('endpoint') && $endpoint){
|
239
|
|
|
|
240
|
|
|
$routeName = endpoint.'Route.php';
|
241
|
|
|
$routeMapper = $routeDefinitor['routePath'].''.DIRECTORY_SEPARATOR.''.$routeName;
|
242
|
|
|
|
243
|
|
|
if(file_exists($routeMapper) && !isset(static::$paths[$routeMapper])){
|
244
|
|
|
static::$paths[$routeMapper] = $routeDefinitor['controllerPath'];
|
245
|
|
|
}
|
246
|
|
|
}
|
247
|
|
|
else{
|
248
|
|
|
|
249
|
|
|
// if there is no endpoint,
|
250
|
|
|
// all files in the path of the route are transferred to path.
|
251
|
|
|
$allFilesInThatRoutePath = Utils::glob($routeDefinitor['routePath']);
|
252
|
|
|
|
253
|
|
|
foreach ($allFilesInThatRoutePath as $item){
|
254
|
|
|
static::$paths[$item] = $routeDefinitor['controllerPath'];
|
255
|
|
|
}
|
256
|
|
|
}
|
257
|
|
|
}
|
258
|
|
|
}
|
259
|
|
|
|
260
|
|
|
/**
|
261
|
|
|
* get route setRoute method
|
262
|
|
|
*
|
263
|
|
|
* @param $params
|
264
|
|
|
* @param $function
|
265
|
|
|
* @param null $controller
|
|
|
|
|
266
|
|
|
*/
|
267
|
|
|
public static function setRoute($params,$function,$controller=null)
|
268
|
|
|
{
|
269
|
|
|
[$pattern,$route] = $params;
|
270
|
|
|
[$class,$method] = explode("@",$route);
|
271
|
|
|
|
272
|
|
|
$patternList = array_values(
|
273
|
|
|
array_filter(explode("/",$pattern),'strlen')
|
274
|
|
|
);
|
275
|
|
|
|
276
|
|
|
static::$routes['pattern'][] = $patternList;
|
277
|
|
|
static::$routes['data'][] = [
|
278
|
|
|
'method' => $method,
|
279
|
|
|
'class' => $class,
|
280
|
|
|
'http' => $function,
|
281
|
|
|
'controller' => $controller,
|
282
|
|
|
'namespace' => static::$namespace,
|
283
|
|
|
'endpoint' => strtolower(str_replace(StaticPathList::$controllerBundleName,'',static::$namespace))
|
284
|
|
|
];
|
285
|
|
|
}
|
286
|
|
|
|
287
|
|
|
/**
|
288
|
|
|
* is route variable regex pattern
|
289
|
|
|
*
|
290
|
|
|
* @param null $value
|
|
|
|
|
291
|
|
|
* @return bool
|
292
|
|
|
*/
|
293
|
|
|
public static function isMatchVaribleRegexPattern($value=null)
|
294
|
|
|
{
|
295
|
|
|
// determines if the variable that can be used
|
296
|
|
|
// in the route file meets the regex rule.
|
297
|
|
|
return (preg_match('@\{(.*?)\}@is',$value)) ? true : false;
|
298
|
|
|
}
|
299
|
|
|
|
300
|
|
|
|
301
|
|
|
/**
|
302
|
|
|
* is optional route variable regex pattern
|
303
|
|
|
*
|
304
|
|
|
* @param null $value
|
|
|
|
|
305
|
|
|
* @return bool
|
306
|
|
|
*/
|
307
|
|
|
public static function isOptionalVaribleRegexPattern($value=null)
|
308
|
|
|
{
|
309
|
|
|
// determines if the variable that can be used
|
310
|
|
|
// in the route file meets the regex rule.
|
311
|
|
|
return preg_match('@\{[a-z]+\?\}@is',$value) ? true : false;
|
312
|
|
|
}
|
313
|
|
|
|
314
|
|
|
/**
|
315
|
|
|
* get route mappings
|
316
|
|
|
*
|
317
|
|
|
* @return array
|
318
|
|
|
*/
|
319
|
|
|
public static function getRouteMappings()
|
320
|
|
|
{
|
321
|
|
|
(new self())->handle(false);
|
322
|
|
|
|
323
|
|
|
$mappings = [];
|
324
|
|
|
|
325
|
|
|
$routes = self::getRoutes();
|
326
|
|
|
$routeData = isset($routes['data']) ? $routes['data'] : [];
|
327
|
|
|
$routePattern = isset($routes['pattern']) ? $routes['pattern'] : [];
|
328
|
|
|
|
329
|
|
|
$counter = 0;
|
|
|
|
|
330
|
|
|
|
331
|
|
|
$application = app();
|
332
|
|
|
|
333
|
|
|
$application->loadIfNotExistBoot(['middleware']);
|
334
|
|
|
$middlewareProvider = $application['middleware'];
|
335
|
|
|
|
336
|
|
|
foreach($routeData as $key=>$data) {
|
337
|
|
|
|
338
|
|
|
$middlewareProvider->setKeyOdds('endpoint', $data['endpoint']);
|
339
|
|
|
$middlewareProvider->setKeyOdds('method', $data['method']);
|
340
|
|
|
$middlewareProvider->setKeyOdds('http', $data['http']);
|
341
|
|
|
|
342
|
|
|
$middlewareProvider->handleMiddlewareProcess();
|
343
|
|
|
$beforeMiddleware = $middlewareProvider->getShow();
|
344
|
|
|
|
345
|
|
|
$middlewareProvider->handleMiddlewareProcess('after');
|
346
|
|
|
$afterMiddleware = $middlewareProvider->getShow();
|
347
|
|
|
|
348
|
|
|
$endpoint = $data['endpoint'];
|
349
|
|
|
$controllerNamespace = Utils::getNamespace($data['controller'] . '/' . $data['namespace'] . '/' . $data['class']);
|
350
|
|
|
|
351
|
|
|
$methodDocument = app()['reflection']($controllerNamespace)->reflectionMethodParams($data['method'])->document;
|
352
|
|
|
|
353
|
|
|
$methodDefinition = '';
|
354
|
|
|
|
355
|
|
|
if (preg_match('@#define:(.*?)\n@is', $methodDocument, $definition)) {
|
356
|
|
|
if (isset($definition[1])) {
|
357
|
|
|
$methodDefinition = rtrim($definition[1]);
|
358
|
|
|
}
|
359
|
|
|
}
|
360
|
|
|
|
361
|
|
|
$mappings[$key]['endpoint'] = $endpoint . '/' . implode("/", $routePattern[$key]);
|
362
|
|
|
$mappings[$key]['http'] = $data['http'];
|
363
|
|
|
$mappings[$key]['definition'] = $methodDefinition;
|
364
|
|
|
$mappings[$key]['before'] = implode(",",$beforeMiddleware);
|
365
|
|
|
$mappings[$key]['after'] = implode(",",$afterMiddleware);
|
366
|
|
|
$mappings[$key]['doc'] = 'not available';
|
367
|
|
|
|
368
|
|
|
}
|
369
|
|
|
|
370
|
|
|
return $mappings;
|
371
|
|
|
}
|
372
|
|
|
|
373
|
|
|
|
374
|
|
|
} |