Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
RouterCacheTrait.php
Go to the documentation of this file.
1<?php
2
4
13
25
26 abstract public static function getControllers(string $subClass = "\\Ubiquity\\controllers\\Controller", bool $backslash = false, bool $includeSubclass = false, bool $includeAbstract = false): array;
27
28 public static function controllerCacheUpdated(array &$config): array {
29 $result = [];
30 $domain = DDDManager::getActiveDomain();
31 $newRoutes = self::parseControllerFiles($config, true, $domain != '');
32 $ctrls = self::getControllerCacheByDomain(false, $domain);
33 if ($newRoutes ['default'] != $ctrls && !(false)) {
34 $result ['default'] = true;
35 }
36 $ctrls = self::getControllerCacheByDomain(true, $domain);
37 if ($newRoutes ['rest'] != $ctrls) {
38 $result ['rest'] = true;
39 }
40 return $result;
41 }
42
43 public static function storeDynamicRoutes(bool $isRest = false): void {
44 $routes = Router::getRoutes();
45 $part = ($isRest) ? 'rest' : 'default';
46 self::$cache->store('controllers/routes.' . $part, $routes, 'controllers');
47 }
48
49 private static function storeRouteResponse(string $key, ?string $response): ?string {
50 $cache = ['content-type' => UResponse::$headers ['Content-Type'] ?? 'text/html', 'content' => $response];
51 self::$cache->store('controllers/' . $key, $cache, 'controllers');
52 return $response;
53 }
54
55 private static function getRouteKey(string $routePath): string {
56 return 'path' . \md5(Router::slashPath($routePath));
57 }
58
64 public static function getControllerCache(bool $isRest = false): array {
65 $key = ($isRest) ? 'rest' : 'default';
66 if (self::$cache->exists('controllers/routes.' . $key)) {
67 return self::$cache->fetch('controllers/routes.' . $key);
68 }
69 return [];
70 }
71
78 public static function getControllerCacheByDomain(bool $isRest = false, string $domain = ''): array {
79 $key = ($isRest) ? 'rest' : 'default';
80 if (self::$cache->exists('controllers/routes.' . $key)) {
81 if ($domain == '') {
82 return self::$cache->fetch('controllers/routes.' . $key);
83 } else {
84 $ns = Startup::getNS();
85 $routes = self::$cache->fetch('controllers/routes.' . $key);
86 $result = [];
87 foreach ($routes as $k => $route) {
88 if (isset($route['controller'])) {
89 if (UString::startswith($route['controller'], $ns)) {
90 $result[$k] = $route;
91 }
92 } else {
93 foreach ($route as $method => $routePart) {
94 if (UString::startswith($routePart['controller'], $ns)) {
95 $result[$k][$method] = $routePart;
96 }
97 }
98 }
99 }
100 return $result;
101 }
102 }
103 return [];
104 }
105
111 public static function getControllerCacheIndex(bool $isRest = false): array {
112 $key = ($isRest) ? 'rest-index' : 'default-index';
113 if (self::$cache->exists('controllers/routes.' . $key)) {
114 return self::$cache->fetch('controllers/routes.' . $key);
115 }
116 return [];
117 }
118
119 public static function getRouteCache(string $routePath, array $routeArray, int $duration) {
120 $key = self::getRouteKey($routePath);
121
122 if (self::$cache->exists('controllers/' . $key) && !self::expired($key, $duration)) {
123 $response = self::$cache->fetch('controllers/' . $key);
124 if ($ct = $response ['content-type'] ?? false) {
125 UResponse::setContentType($ct);
126 }
127 return $response ['content'] ?? '';
128 } else {
129 $response = Startup::runAsString($routeArray);
130 return self::storeRouteResponse($key, $response);
131 }
132 }
133
134 protected static function expired(string $key, int $duration): bool {
135 return self::$cache->expired("controllers/" . $key, $duration) === true;
136 }
137
138 public static function isExpired(string $routePath, int $duration): bool {
139 return self::expired(self::getRouteKey($routePath), $duration);
140 }
141
142 public static function setExpired(string $routePath): void {
143 $key = self::getRouteKey($routePath);
144 if (self::$cache->exists('controllers/' . $key)) {
145 self::$cache->remove('controllers/' . $key);
146 }
147 }
148
154 public static function setRouteCache(string $routePath): ?string {
155 $key = self::getRouteKey($routePath);
156 \ob_start();
157 Startup::forward($routePath);
158 $response = \ob_get_clean();
159 return self::storeRouteResponse($key, $response);
160 }
161
162 public static function addAdminRoutes(): void {
163 self::addControllerCache('Ubiquity\controllers\Admin');
164 }
165
166 public static function getRoutes(): array {
167 return self::getControllerCache();
168 }
169
170 public static function getControllerRoutes(string $controllerClass, bool $isRest = false): array {
171 $result = [];
172 $ctrlCache = self::getControllerCache($isRest);
173 foreach ($ctrlCache as $path => $routeAttributes) {
174 if (isset ($routeAttributes ['controller'])) {
175 if ($routeAttributes ['controller'] === $controllerClass) {
176 $result [$path] = $routeAttributes;
177 }
178 } else {
179 $firstValue = current($routeAttributes);
180 if (isset ($firstValue) && isset ($firstValue ['controller'])) {
181 if ($firstValue ['controller'] === $controllerClass) {
182 $result [$path] = $routeAttributes;
183 }
184 }
185 }
186 }
187 return $result;
188 }
189
190 public static function addRoute(string $path, string $controller, string $action = 'index', ?array $methods = null, string $name = '', bool $isRest = false, int $priority = 0, $callback = null) {
191 $controllerCache = self::getControllerCache($isRest);
192 Router::addRouteToRoutes($controllerCache, $path, $controller, $action, $methods, $name, false, null, [], $priority, $callback);
193 self::$cache->store('controllers/routes.' . ($isRest ? 'rest' : 'default'), $controllerCache, 'controllers');
194 }
195
196 public static function addRoutes($pathArray, $controller, $action = 'index', $methods = null, $name = '') {
197 self::addRoutes_($pathArray, $controller, $action, $methods, $name, false);
198 }
199
200 public static function addRestRoutes($pathArray, $controller, $action = 'index', $methods = null, $name = '') {
201 self::addRoutes_($pathArray, $controller, $action, $methods, $name, true);
202 }
203
204 private static function addRoutes_($pathArray, $controller, $action = 'index', $methods = null, $name = '', $isRest = false) {
205 $controllerCache = self::getControllerCache($isRest);
206 $postfix = 'default';
207 if ($isRest) {
208 $postfix = 'rest';
209 }
210 Router::addRoutesToRoutes($controllerCache, $pathArray, $controller, $action, $methods, $name);
211 self::$cache->store('controllers/routes.' . $postfix, $controllerCache, 'controllers');
212 }
213
220 public static function warmUpControllers(?array $controllers = null) {
221 $controllers ??= self::getControllers();
222 foreach ($controllers as $ctrl) {
223 $controller = StartupAsync::getControllerInstance($ctrl);
224 $binary = UIntrospection::implementsMethod($controller, 'isValid', Controller::class) ? 1 : 0;
225 $binary += UIntrospection::implementsMethod($controller, 'initialize', Controller::class) ? 2 : 0;
226 $binary += UIntrospection::implementsMethod($controller, 'finalize', Controller::class) ? 4 : 0;
227 $controller->_binaryCalls = $binary;
228 }
229 }
230}
static expired(string $key, int $duration)
static getRouteCache(string $routePath, array $routeArray, int $duration)
static getControllerCacheIndex(bool $isRest=false)
static warmUpControllers(?array $controllers=null)
Preloads controllers.
static storeRouteResponse(string $key, ?string $response)
static addRoutes_($pathArray, $controller, $action='index', $methods=null, $name='', $isRest=false)
static controllerCacheUpdated(array &$config)
static addRoute(string $path, string $controller, string $action='index', ?array $methods=null, string $name='', bool $isRest=false, int $priority=0, $callback=null)
static getControllerRoutes(string $controllerClass, bool $isRest=false)
static addRoutes($pathArray, $controller, $action='index', $methods=null, $name='')
static getControllers(string $subClass="\\Ubiquity\\controllers\\Controller", bool $backslash=false, bool $includeSubclass=false, bool $includeAbstract=false)
static setRouteCache(string $routePath)
Stores a route response in cache.
static getControllerCache(bool $isRest=false)
static storeDynamicRoutes(bool $isRest=false)
static isExpired(string $routePath, int $duration)
static getControllerCacheByDomain(bool $isRest=false, string $domain='')
static addRestRoutes($pathArray, $controller, $action='index', $methods=null, $name='')
Base class for controllers.
Startup for async platforms (Swoole, Workerman, Roadrunner, php-pm...) Ubiquity\controllers$StartupAs...
Starts the framework.
Definition Startup.php:19
Manager for a Domain Driven Design approach.
Ubiquity\utils\base$UIntrospection This class is part of Ubiquity.
String utilities.
Definition UString.php:15
Http Response utilities.
Definition UResponse.php:17