1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\cache\traits; |
4
|
|
|
|
5
|
|
|
use Ubiquity\controllers\Startup; |
6
|
|
|
use Ubiquity\controllers\Router; |
7
|
|
|
use Ubiquity\cache\parser\ControllerParser; |
8
|
|
|
use Ubiquity\cache\ClassUtils; |
9
|
|
|
use Ubiquity\utils\base\UArray; |
10
|
|
|
use Ubiquity\cache\CacheManager; |
11
|
|
|
use Ubiquity\controllers\di\DiManager; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* Ubiquity\cache\traits$RouterCacheTrait |
16
|
|
|
* This class is part of Ubiquity |
17
|
|
|
* |
18
|
|
|
* @author jcheron <[email protected]> |
19
|
|
|
* @version 1.0.7 |
20
|
|
|
* @property \Ubiquity\cache\system\AbstractDataCache $cache |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
trait RouterCacheTrait { |
24
|
|
|
|
25
|
|
|
abstract protected static function _getFiles(&$config, $type, $silent = false); |
26
|
|
|
|
27
|
|
|
private static function addControllerCache($classname) { |
28
|
|
|
$parser = new ControllerParser (); |
29
|
|
|
try { |
30
|
|
|
$parser->parse ( $classname ); |
31
|
|
|
return $parser->asArray (); |
32
|
|
|
} catch ( \Exception $e ) { |
33
|
|
|
// Nothing to do |
34
|
|
|
} |
35
|
|
|
return [ ]; |
36
|
|
|
} |
37
|
|
|
|
38
|
17 |
|
private static function parseControllerFiles(&$config, $silent = false) { |
39
|
17 |
|
$routes = [ 'rest' => [ ],'default' => [ ] ]; |
40
|
17 |
|
$files = self::getControllersFiles ( $config, $silent ); |
41
|
17 |
|
foreach ( $files as $file ) { |
42
|
17 |
|
if (is_file ( $file )) { |
43
|
17 |
|
$controller = ClassUtils::getClassFullNameFromFile ( $file ); |
44
|
17 |
|
$parser = new ControllerParser (); |
45
|
|
|
try { |
46
|
17 |
|
$parser->parse ( $controller ); |
47
|
17 |
|
$ret = $parser->asArray (); |
48
|
17 |
|
$key = ($parser->isRest ()) ? 'rest' : 'default'; |
49
|
17 |
|
$routes [$key] = \array_merge ( $routes [$key], $ret ); |
50
|
1 |
|
} catch ( \Exception $e ) { |
51
|
|
|
// Nothing to do |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
17 |
|
self::sortByPriority ( $routes ['default'] ); |
56
|
17 |
|
self::sortByPriority ( $routes ['rest'] ); |
57
|
17 |
|
return $routes; |
58
|
|
|
} |
59
|
|
|
|
60
|
17 |
|
protected static function sortByPriority(&$array) { |
61
|
|
|
uasort ( $array, function ($item1, $item2) { |
62
|
17 |
|
return UArray::getRecursive ( $item2, 'priority', 0 ) <=> UArray::getRecursive ( $item1, 'priority', 0 ); |
63
|
17 |
|
} ); |
64
|
17 |
|
UArray::removeRecursive ( $array, 'priority' ); |
65
|
17 |
|
} |
66
|
|
|
|
67
|
8 |
|
private static function initRouterCache(&$config, $silent = false) { |
68
|
8 |
|
$routes = self::parseControllerFiles ( $config, $silent ); |
69
|
8 |
|
self::$cache->store ( 'controllers/routes.default', 'return ' . UArray::asPhpArray ( $routes ['default'], 'array' ) . ';', 'controllers' ); |
70
|
8 |
|
self::$cache->store ( 'controllers/routes.rest', 'return ' . UArray::asPhpArray ( $routes ['rest'], 'array' ) . ';', 'controllers' ); |
71
|
8 |
|
DiManager::init ( $config ); |
72
|
8 |
|
if (! $silent) { |
73
|
8 |
|
echo "Router cache reset\n"; |
74
|
|
|
} |
75
|
8 |
|
} |
76
|
|
|
|
77
|
11 |
|
public static function controllerCacheUpdated(&$config) { |
78
|
11 |
|
$result = false; |
79
|
11 |
|
$newRoutes = self::parseControllerFiles ( $config, true ); |
80
|
11 |
|
$ctrls = self::getControllerCache (); |
81
|
11 |
|
if ($newRoutes ['default'] != $ctrls) { |
82
|
6 |
|
$result ['default'] = true; |
83
|
|
|
} |
84
|
11 |
|
$ctrls = self::getControllerCache ( true ); |
85
|
11 |
|
if ($newRoutes ['rest'] != $ctrls) { |
86
|
3 |
|
$result ['rest'] = true; |
87
|
|
|
} |
88
|
11 |
|
return $result; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public static function storeDynamicRoutes($isRest = false) { |
92
|
|
|
$routes = Router::getRoutes (); |
93
|
|
|
$part = ($isRest) ? 'rest' : 'default'; |
94
|
|
|
self::$cache->store ( 'controllers/routes.' . $part, 'return ' . UArray::asPhpArray ( $routes, 'array' ) . ';', 'controllers' ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private static function storeRouteResponse($key, $response) { |
98
|
|
|
self::$cache->store ( 'controllers/' . $key, $response, 'controllers', false ); |
99
|
|
|
return $response; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private static function getRouteKey($routePath) { |
103
|
|
|
if (is_array ( $routePath )) { |
104
|
|
|
return 'path' . \md5 ( \implode ( '', $routePath ) ); |
105
|
|
|
} |
106
|
|
|
return 'path' . \md5 ( Router::slashPath ( $routePath ) ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
* @param boolean $isRest |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
56 |
|
public static function getControllerCache($isRest = false) { |
115
|
56 |
|
$key = ($isRest) ? 'rest' : 'default'; |
116
|
56 |
|
if (self::$cache->exists ( 'controllers/routes.' . $key )) |
117
|
53 |
|
return self::$cache->fetch ( 'controllers/routes.' . $key ); |
118
|
4 |
|
return [ ]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function getRouteCache($routePath, $routeArray, $duration) { |
122
|
|
|
$key = self::getRouteKey ( $routePath ); |
123
|
|
|
|
124
|
|
|
if (self::$cache->exists ( 'controllers/' . $key ) && ! self::expired ( $key, $duration )) { |
125
|
|
|
$response = self::$cache->file_get_contents ( 'controllers/' . $key ); |
126
|
|
|
return $response; |
127
|
|
|
} else { |
128
|
|
|
$response = Startup::runAsString ( $routeArray ); |
129
|
|
|
return self::storeRouteResponse ( $key, $response ); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected static function expired($key, $duration) { |
134
|
|
|
return self::$cache->expired ( "controllers/" . $key, $duration ) === true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function isExpired($routePath, $duration) { |
138
|
|
|
return self::expired ( self::getRouteKey ( $routePath ), $duration ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public static function setExpired($routePath) { |
142
|
|
|
$key = self::getRouteKey ( $routePath ); |
143
|
|
|
if (self::$cache->exists ( 'controllers/' . $key )) { |
144
|
|
|
self::$cache->remove ( 'controllers/' . $key ); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public static function setRouteCache($routePath) { |
149
|
|
|
$key = self::getRouteKey ( $routePath ); |
150
|
|
|
$response = Startup::runAsString ( $routePath ); |
151
|
|
|
return self::storeRouteResponse ( $key, $response ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public static function addAdminRoutes() { |
155
|
|
|
self::addControllerCache ( 'Ubiquity\controllers\Admin' ); |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
public static function getRoutes() { |
159
|
2 |
|
$result = self::getControllerCache (); |
160
|
2 |
|
return $result; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
public static function getControllerRoutes($controllerClass, $isRest = false) { |
164
|
1 |
|
$result = [ ]; |
165
|
1 |
|
$ctrlCache = self::getControllerCache ( $isRest ); |
166
|
1 |
|
foreach ( $ctrlCache as $path => $routeAttributes ) { |
167
|
1 |
|
if (isset ( $routeAttributes ['controller'] )) { |
168
|
1 |
|
if ($routeAttributes ['controller'] === $controllerClass) { |
169
|
1 |
|
$result [$path] = $routeAttributes; |
170
|
|
|
} |
171
|
|
|
} else { |
172
|
1 |
|
$firstValue = current ( $routeAttributes ); |
173
|
1 |
|
if (isset ( $firstValue ) && isset ( $firstValue ['controller'] )) { |
174
|
1 |
|
if ($firstValue ['controller'] === $controllerClass) { |
175
|
1 |
|
$result [$path] = $routeAttributes; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
1 |
|
return $result; |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
public static function addRoute($path, $controller, $action = 'index', $methods = null, $name = '', $isRest = false, $priority = 0, $callback = null) { |
184
|
1 |
|
$controllerCache = self::getControllerCache ( $isRest ); |
185
|
1 |
|
Router::addRouteToRoutes ( $controllerCache, $path, $controller, $action, $methods, $name, false, null, [ ], $priority, $callback ); |
186
|
1 |
|
self::$cache->store ( 'controllers/routes.' . ($isRest ? 'rest' : 'default'), "return " . UArray::asPhpArray ( $controllerCache, 'array' ) . ';', 'controllers' ); |
187
|
1 |
|
} |
188
|
|
|
|
189
|
|
|
public static function addRoutes($pathArray, $controller, $action = 'index', $methods = null, $name = '') { |
190
|
|
|
self::addRoutes_ ( $pathArray, $controller, $action, $methods, $name, false ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public static function addRestRoutes($pathArray, $controller, $action = 'index', $methods = null, $name = '') { |
194
|
|
|
self::addRoutes_ ( $pathArray, $controller, $action, $methods, $name, true ); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
private static function addRoutes_($pathArray, $controller, $action = 'index', $methods = null, $name = '', $isRest = false) { |
198
|
|
|
$controllerCache = self::getControllerCache ( $isRest ); |
199
|
|
|
$postfix = 'default'; |
200
|
|
|
if ($isRest) { |
201
|
|
|
$postfix = 'rest'; |
202
|
|
|
} |
203
|
|
|
Router::addRoutesToRoutes ( $controllerCache, $pathArray, $controller, $action, $methods, $name ); |
204
|
|
|
self::$cache->store ( 'controllers/routes.' . $postfix, 'return ' . UArray::asPhpArray ( $controllerCache, 'array' ) . ';', 'controllers' ); |
205
|
|
|
} |
206
|
|
|
|
207
|
17 |
|
public static function getControllersFiles(&$config, $silent = false) { |
208
|
17 |
|
return self::_getFiles ( $config, 'controllers', $silent ); |
209
|
|
|
} |
210
|
|
|
|
211
|
10 |
|
public static function getControllers($subClass = "\\Ubiquity\\controllers\\Controller", $backslash = false, $includeSubclass = false, $includeAbstract = false) { |
212
|
10 |
|
$result = [ ]; |
213
|
10 |
|
if ($includeSubclass) { |
214
|
1 |
|
$result [] = $subClass; |
215
|
|
|
} |
216
|
10 |
|
$config = Startup::getConfig (); |
217
|
10 |
|
$files = self::getControllersFiles ( $config, true ); |
218
|
|
|
try { |
219
|
10 |
|
$restCtrls = CacheManager::getRestCache (); |
220
|
3 |
|
} catch ( \Exception $e ) { |
221
|
3 |
|
$restCtrls = [ ]; |
222
|
|
|
} |
223
|
10 |
|
foreach ( $files as $file ) { |
224
|
10 |
|
if (is_file ( $file )) { |
225
|
10 |
|
$controllerClass = ClassUtils::getClassFullNameFromFile ( $file, $backslash ); |
226
|
10 |
|
if (isset ( $restCtrls [$controllerClass] ) === false) { |
227
|
10 |
|
$r = new \ReflectionClass ( $controllerClass ); |
228
|
10 |
|
if ($r->isSubclassOf ( $subClass ) && ($includeAbstract || ! $r->isAbstract ())) { |
229
|
10 |
|
$result [] = $controllerClass; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} |
234
|
10 |
|
return $result; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Preloads controllers. |
239
|
|
|
* To use only with async servers (Swoole, Workerman) |
240
|
|
|
* |
241
|
|
|
* @param ?array $controllers |
242
|
|
|
*/ |
243
|
|
|
public static function warmUpControllers($controllers = null) { |
244
|
|
|
$controllers = $controllers ?? self::getControllers (); |
245
|
|
|
foreach ( $controllers as $ctrl ) { |
246
|
|
|
Startup::getControllerInstance ( $ctrl ); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|