1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Resta\Router;
|
4
|
|
|
|
5
|
|
|
use Resta\Support\Utils;
|
6
|
|
|
use Resta\Foundation\PathManager\StaticPathList;
|
7
|
|
|
|
8
|
|
|
class Route extends RouteHttpManager
|
9
|
|
|
{
|
10
|
|
|
// get route acccessible property
|
11
|
|
|
use RouteAccessiblePropertyTrait;
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* @var array $endpoints
|
15
|
|
|
*/
|
16
|
|
|
protected static $endpoints = [];
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* @var array $routes
|
20
|
|
|
*/
|
21
|
|
|
protected static $routes = [];
|
22
|
|
|
|
23
|
|
|
/**
|
24
|
|
|
* @var array $paths
|
25
|
|
|
*/
|
26
|
|
|
protected static $paths = [];
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* @var array $mappers
|
30
|
|
|
*/
|
31
|
|
|
protected static $mappers = [];
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* @var null|string $namespace
|
35
|
|
|
*/
|
36
|
|
|
protected static $namespace;
|
37
|
|
|
|
38
|
|
|
/**
|
39
|
|
|
* get route checkArrayEqual
|
40
|
|
|
*
|
41
|
|
|
* @param $patterns
|
42
|
|
|
* @param $urlRoute
|
43
|
|
|
* @return int|string
|
44
|
|
|
*/
|
45
|
|
|
public static function checkArrayEqual($patterns,$urlRoute)
|
46
|
|
|
{
|
47
|
|
|
// calculates the equality difference between
|
48
|
|
|
// the route pattern and the urlRoute value.
|
49
|
|
|
foreach ($patterns as $key=>$pattern){
|
50
|
|
|
|
51
|
|
|
if(Utils::isArrayEqual($pattern,$urlRoute)){
|
52
|
|
|
return $key;
|
53
|
|
|
}
|
54
|
|
|
}
|
55
|
|
|
|
56
|
|
|
// if the difference is not equal,
|
57
|
|
|
// null is returned.
|
58
|
|
|
return null;
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
/**
|
62
|
|
|
* get route getRouteResolve
|
63
|
|
|
*
|
64
|
|
|
* @return array
|
65
|
|
|
*/
|
66
|
|
|
public static function getRouteResolve()
|
67
|
|
|
{
|
68
|
|
|
// get routes data and the resolving pattern
|
69
|
|
|
// Both are interrelated.
|
70
|
|
|
$routes = self::getRoutes();
|
71
|
|
|
$patternResolve = app()->resolve(RouteMatching::class,['route'=>new self()])->getPatternResolve();
|
72
|
|
|
|
73
|
|
|
//if routes data is available in pattern resolve.
|
74
|
|
|
if(isset($routes['data'][$patternResolve])){
|
75
|
|
|
|
76
|
|
|
// if the incoming http value is
|
77
|
|
|
// the same as the real request method, the data is processed.
|
78
|
|
|
if($routes['data'][$patternResolve]['http'] == strtolower(httpMethod)){
|
79
|
|
|
|
80
|
|
|
// we are set the solved pattern to a variable.
|
81
|
|
|
$resolve = $routes['data'][$patternResolve];
|
82
|
|
|
|
83
|
|
|
return [
|
84
|
|
|
'class' => $resolve['class'],
|
85
|
|
|
'method' => $resolve['method'],
|
86
|
|
|
'controller' => $resolve['controller'],
|
87
|
|
|
'namespace' => $resolve['namespace'],
|
88
|
|
|
];
|
89
|
|
|
}
|
90
|
|
|
}
|
91
|
|
|
return [];
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
/**
|
95
|
|
|
* route handle for route application
|
96
|
|
|
*
|
97
|
|
|
* @return void|mixed
|
98
|
|
|
*/
|
99
|
|
|
public function handle()
|
100
|
|
|
{
|
101
|
|
|
// we will record the path data for the route.
|
102
|
|
|
// We set the routeMapper variables and the route path.
|
103
|
|
|
self::setPath(function(){
|
104
|
|
|
|
105
|
|
|
// we are sending
|
106
|
|
|
// the controller and routes.php path.
|
107
|
|
|
return [
|
108
|
|
|
'controllerPath' => path()->controller(),
|
109
|
|
|
'routePath' => path()->route(),
|
110
|
|
|
];
|
111
|
|
|
});
|
112
|
|
|
|
113
|
|
|
// in the paths data,
|
114
|
|
|
// we run the route mapper values and the route files one by one.
|
115
|
|
|
foreach (self::$paths as $mapper=>$controller){
|
116
|
|
|
core()->fileSystem->callFile($mapper);
|
117
|
|
|
}
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* get route setPath method
|
122
|
|
|
*
|
123
|
|
|
* @param $path
|
124
|
|
|
*/
|
125
|
|
|
public static function setPath(callable $callback)
|
126
|
|
|
{
|
127
|
|
|
$routeDefinitor = call_user_func($callback);
|
128
|
|
|
|
129
|
|
|
if(isset($routeDefinitor['controllerPath']) && isset($routeDefinitor['routePath'])){
|
130
|
|
|
|
131
|
|
|
//the route paths to be saved to the mappers static property.
|
132
|
|
|
static::$mappers['routePaths'][] = $routeDefinitor['routePath'];
|
133
|
|
|
static::$mappers['controllerNamespaces'][] = Utils::getNamespace($routeDefinitor['controllerPath']);
|
134
|
|
|
|
135
|
|
|
// if there is endpoint,
|
136
|
|
|
// then only that endpoint is transferred into the path
|
137
|
|
|
if(defined('endpoint')){
|
138
|
|
|
|
139
|
|
|
$routeName = endpoint.'Route.php';
|
140
|
|
|
$routeMapper = $routeDefinitor['routePath'].''.DIRECTORY_SEPARATOR.''.$routeName;
|
141
|
|
|
|
142
|
|
|
if(file_exists($routeMapper) && !isset(static::$paths[$routeMapper])){
|
143
|
|
|
static::$paths[$routeMapper] = $routeDefinitor['controllerPath'];
|
144
|
|
|
}
|
145
|
|
|
}
|
146
|
|
|
else{
|
147
|
|
|
|
148
|
|
|
// if there is no endpoint,
|
149
|
|
|
// all files in the path of the route are transferred to path.
|
150
|
|
|
$allFilesInThatRoutePath = Utils::glob($routeDefinitor['routePath']);
|
151
|
|
|
|
152
|
|
|
foreach ($allFilesInThatRoutePath as $item){
|
153
|
|
|
static::$paths[$item] = $routeDefinitor['controllerPath'];
|
154
|
|
|
}
|
155
|
|
|
}
|
156
|
|
|
}
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
/**
|
160
|
|
|
* get route setRoute method
|
161
|
|
|
*
|
162
|
|
|
* @param $params
|
163
|
|
|
* @param $function
|
164
|
|
|
* @param null $controller
|
|
|
|
|
165
|
|
|
*/
|
166
|
|
|
public static function setRoute($params,$function,$controller=null)
|
167
|
|
|
{
|
168
|
|
|
[$pattern,$route] = $params;
|
169
|
|
|
[$class,$method] = explode("@",$route);
|
170
|
|
|
|
171
|
|
|
$patternList = array_values(
|
172
|
|
|
array_filter(explode("/",$pattern),'strlen')
|
173
|
|
|
);
|
174
|
|
|
|
175
|
|
|
static::$routes['pattern'][] = $patternList;
|
176
|
|
|
static::$routes['data'][] = [
|
177
|
|
|
'method' => $method,
|
178
|
|
|
'class' => $class,
|
179
|
|
|
'http' => $function,
|
180
|
|
|
'controller' => $controller,
|
181
|
|
|
'namespace' => static::$namespace,
|
182
|
|
|
'endpoint' => strtolower(str_replace(StaticPathList::$controllerBundleName,'',static::$namespace))
|
183
|
|
|
];
|
184
|
|
|
}
|
185
|
|
|
|
186
|
|
|
/**
|
187
|
|
|
* is route variable regex pattern
|
188
|
|
|
*
|
189
|
|
|
* @param null $value
|
|
|
|
|
190
|
|
|
* @return bool
|
191
|
|
|
*/
|
192
|
|
|
public static function isMatchVaribleRegexPattern($value=null)
|
193
|
|
|
{
|
194
|
|
|
// determines if the variable that can be used
|
195
|
|
|
// in the route file meets the regex rule.
|
196
|
|
|
return (preg_match('@\{(.*?)\}@is',$value)) ? true : false;
|
197
|
|
|
}
|
198
|
|
|
} |