26 abstract public static function getControllers(
string $subClass =
"\\Ubiquity\\controllers\\Controller",
bool $backslash =
false,
bool $includeSubclass =
false,
bool $includeAbstract =
false): array;
30 $domain = DDDManager::getActiveDomain();
31 $newRoutes = self::parseControllerFiles($config,
true, $domain !=
'');
33 if ($newRoutes [
'default'] != $ctrls && !(
false)) {
34 $result [
'default'] =
true;
37 if ($newRoutes [
'rest'] != $ctrls) {
38 $result [
'rest'] =
true;
44 $routes =
Router::getRoutes();
45 $part = ($isRest) ?
'rest' :
'default';
46 self::$cache->store(
'controllers/routes.' . $part, $routes,
'controllers');
50 $cache = [
'content-type' =>
UResponse::$headers [
'Content-Type'] ??
'text/html',
'content' => $response];
51 self::$cache->store(
'controllers/' . $key, $cache,
'controllers');
55 private static function getRouteKey(
string $routePath): string {
56 return
'path' . \md5(
Router::slashPath($routePath));
65 $key = ($isRest) ?
'rest' :
'default';
66 if (self::$cache->exists(
'controllers/routes.' . $key)) {
67 return self::$cache->fetch(
'controllers/routes.' . $key);
79 $key = ($isRest) ?
'rest' :
'default';
80 if (self::$cache->exists(
'controllers/routes.' . $key)) {
82 return self::$cache->fetch(
'controllers/routes.' . $key);
84 $ns = Startup::getNS();
85 $routes = self::$cache->fetch(
'controllers/routes.' . $key);
87 foreach ($routes as $k => $route) {
88 if (isset($route[
'controller'])) {
89 if (UString::startswith($route[
'controller'], $ns)) {
93 foreach ($route as $method => $routePart) {
94 if (UString::startswith($routePart[
'controller'], $ns)) {
95 $result[$k][$method] = $routePart;
112 $key = ($isRest) ?
'rest-index' :
'default-index';
113 if (self::$cache->exists(
'controllers/routes.' . $key)) {
114 return self::$cache->fetch(
'controllers/routes.' . $key);
119 public static function getRouteCache(
string $routePath, array $routeArray,
int $duration) {
120 $key = self::getRouteKey($routePath);
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);
127 return $response [
'content'] ??
'';
129 $response = Startup::runAsString($routeArray);
130 return self::storeRouteResponse($key, $response);
134 protected static function expired(
string $key,
int $duration): bool {
135 return self::$cache->expired(
"controllers/" . $key, $duration) === true;
138 public static function isExpired(
string $routePath,
int $duration): bool {
139 return self::expired(self::getRouteKey($routePath), $duration);
143 $key = self::getRouteKey($routePath);
144 if (self::$cache->exists(
'controllers/' . $key)) {
145 self::$cache->remove(
'controllers/' . $key);
155 $key = self::getRouteKey($routePath);
157 Startup::forward($routePath);
158 $response = \ob_get_clean();
159 return self::storeRouteResponse($key, $response);
163 self::addControllerCache(
'Ubiquity\controllers\Admin');
167 return self::getControllerCache();
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;
179 $firstValue = current($routeAttributes);
180 if (isset ($firstValue) && isset ($firstValue [
'controller'])) {
181 if ($firstValue [
'controller'] === $controllerClass) {
182 $result [$path] = $routeAttributes;
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');
196 public static function addRoutes($pathArray, $controller, $action =
'index', $methods =
null, $name =
'') {
197 self::addRoutes_($pathArray, $controller, $action, $methods, $name,
false);
200 public static function addRestRoutes($pathArray, $controller, $action =
'index', $methods =
null, $name =
'') {
201 self::addRoutes_($pathArray, $controller, $action, $methods, $name,
true);
204 private static function addRoutes_($pathArray, $controller, $action =
'index', $methods =
null, $name =
'', $isRest =
false) {
205 $controllerCache = self::getControllerCache($isRest);
206 $postfix =
'default';
210 Router::addRoutesToRoutes($controllerCache, $pathArray, $controller, $action, $methods, $name);
211 self::$cache->store(
'controllers/routes.' . $postfix, $controllerCache,
'controllers');
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;
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 getRouteKey(string $routePath)
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 setExpired(string $routePath)
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...
Manager for a Domain Driven Design approach.
Ubiquity\utils\base$UIntrospection This class is part of Ubiquity.