Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DevRouterCacheTrait.php
Go to the documentation of this file.
1<?php
2
4
9use Ubiquity\utils\base\UArray;
11
21
22 abstract public static function getAnnotationsEngineInstance();
23
24 private static function addControllerCache(string $classname): array {
25 $parser = new ControllerParser ( self::getAnnotationsEngineInstance () );
26 try {
27 $parser->parse ( $classname );
28 return $parser->asArray ();
29 } catch ( \Exception $e ) {
30 // Nothing to do
31 }
32 return [ ];
33 }
34
35 private static function parseControllerFiles(array &$config, bool $silent = false,bool $activeDomain=false): array {
36 $routes = [ 'rest' => [ ],'default' => [ ] ];
37 if($activeDomain){
38 $files=self::getControllersFiles($config,$silent);
39 }else {
40 $files = self::getAllControllersFiles($config, $silent);
41 }
42 $annotsEngine = self::getAnnotationsEngineInstance ();
43 foreach ( $files as $file ) {
44 if (is_file ( $file )) {
45 $controller = ClassUtils::getClassFullNameFromFile ( $file );
46 $parser = new ControllerParser ( $annotsEngine );
47 $parser->setSilent($silent);
48 try {
49 $parser->parse ( $controller);
50 $ret = $parser->asArray ();
51 $key = ($parser->isRest ()) ? 'rest' : 'default';
52 $routes [$key] = \array_merge ( $routes [$key], $ret );
53 } catch ( \Exception $e ) {
54 if (!$silent && $e instanceof ParserException) {
55 throw $e;
56 }
57 // Nothing to do
58 }
59 }
60 }
61 self::sortByPriority ( $routes ['default'] );
62 self::sortByPriority ( $routes ['rest'] );
63 $routes ['rest-index'] = self::createIndex ( $routes ['rest'] );
64 $routes ['default-index'] = self::createIndex ( $routes ['default'] );
65 return $routes;
66 }
67
68 private static function hasCapturingGroup(string $expression): bool {
69 return \preg_match ( "~\\\\.(*SKIP)(?!)|\‍((?(?=\?)\?(P?['<]\w+['>]))~", $expression )===1;
70 }
71
72 public static function getFirstPartIndex(string $element): string {
73 return \strtok ( \trim ( $element, '/' ), '/' );
74 }
75
76 protected static function createIndex(array $routes): array {
77 $res = [ ];
78 foreach ( $routes as $path => $route ) {
79 if (self::hasCapturingGroup ( $path )) {
80 $part = self::getFirstPartIndex ( $path );
81 if ($part != null) {
82 if($part===\trim($path,'/')){
83 $part='*';
84 }
85 $res [$part] [] = $path;
86 }
87 }
88 }
89 return $res;
90 }
91
92 protected static function sortByPriority(array &$array): void {
93 \uasort ( $array, function ($item1, $item2) {
94 return UArray::getRecursive ( $item2, 'priority', 0 ) <=> UArray::getRecursive ( $item1, 'priority', 0 );
95 } );
96 UArray::removeRecursive ( $array, 'priority' );
97 }
98
99 protected static function initRouterCache(array &$config, bool $silent = false): void {
100 $routes = self::parseControllerFiles ( $config, $silent );
101 self::$cache->store ( 'controllers/routes.default', $routes ['default'], 'controllers' );
102 self::$cache->store ( 'controllers/routes.rest', $routes ['rest'], 'controllers' );
103 self::$cache->store ( 'controllers/routes.default-index', $routes ['default-index'], 'controllers' );
104 self::$cache->store ( 'controllers/routes.rest-index', $routes ['rest-index'], 'controllers' );
105 DiManager::init ( $config );
106 if (! $silent) {
107 echo "Router cache reset\n";
108 }
109 }
110
111 public static function getControllersFiles(array &$config, bool $silent = false): array {
112 return self::_getFiles ( $config, 'controllers', $silent );
113 }
114
115 public static function getAllControllersFiles(array &$config, bool $silent = false): array {
116 return self::_getAllFiles ( $config, 'controllers', $silent );
117 }
118
119 public static function getControllers(string $subClass = "\\Ubiquity\\controllers\\Controller", bool $backslash = false, bool $includeSubclass = false, bool $includeAbstract = false): array {
120 $result = [ ];
121 if ($includeSubclass) {
122 $result [] = $subClass;
123 }
124 $config = Startup::getConfig ();
125 $files = self::getControllersFiles ( $config, true );
126 try {
127 $restCtrls = self::getRestCache ();
128 } catch ( \Exception $e ) {
129 $restCtrls = [ ];
130 }
131 foreach ( $files as $file ) {
132 if (\is_file ( $file )) {
133 $controllerClass = ClassUtils::getClassFullNameFromFile ( $file, $backslash );
134 if (\class_exists ( $controllerClass ) && isset ( $restCtrls [$controllerClass] ) === false) {
135 $r = new \ReflectionClass ( $controllerClass );
136 if ($r->isSubclassOf ( $subClass ) && ($includeAbstract || ! $r->isAbstract ())) {
137 $result [] = $controllerClass;
138 }
139 }
140 }
141 }
142 return $result;
143 }
144}
145
146
Manipulates class and namespace names Ubiquity\cache$ClassUtils This class is part of Ubiquity.
static getClassFullNameFromFile($filePathName, $backSlash=false)
get the full name (name \ namespace) of a class from its file path result example: (string) "I\Am\The...
Scans a controller to detect routes defined by annotations or attributes.
Ubiquity\cache\traits$DevRouterCacheTrait This class is part of Ubiquity.
static initRouterCache(array &$config, bool $silent=false)
static parseControllerFiles(array &$config, bool $silent=false, bool $activeDomain=false)
static getControllers(string $subClass="\\Ubiquity\\controllers\\Controller", bool $backslash=false, bool $includeSubclass=false, bool $includeAbstract=false)
static getAllControllersFiles(array &$config, bool $silent=false)
static getControllersFiles(array &$config, bool $silent=false)
Starts the framework.
Definition Startup.php:19
Manage dependency injection.
Definition DiManager.php:17