9use Ubiquity\utils\base\UArray;
27 $parser->
parse ( $classname );
28 return $parser->asArray ();
29 }
catch ( \Exception $e ) {
35 private static function parseControllerFiles(array &$config,
bool $silent =
false,
bool $activeDomain=
false): array {
36 $routes = [
'rest' => [ ],
'default' => [ ] ];
38 $files=self::getControllersFiles($config,$silent);
40 $files = self::getAllControllersFiles($config, $silent);
42 $annotsEngine = self::getAnnotationsEngineInstance ();
43 foreach ( $files as $file ) {
44 if (is_file ( $file )) {
46 $parser =
new ControllerParser ( $annotsEngine );
47 $parser->setSilent($silent);
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) {
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'] );
69 return \preg_match (
"~\\\\.(*SKIP)(?!)|\((?(?=\?)\?(P?['<]\w+['>]))~", $expression )===1;
73 return \strtok ( \trim ( $element,
'/' ),
'/' );
78 foreach ( $routes as $path => $route ) {
79 if (self::hasCapturingGroup ( $path )) {
80 $part = self::getFirstPartIndex ( $path );
82 if($part===\trim($path,
'/')){
85 $res [$part] [] = $path;
93 \uasort ( $array, function ($item1, $item2) {
94 return UArray::getRecursive ( $item2,
'priority', 0 ) <=> UArray::getRecursive ( $item1,
'priority', 0 );
96 UArray::removeRecursive ( $array,
'priority' );
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 );
107 echo
"Router cache reset\n";
112 return self::_getFiles ( $config,
'controllers', $silent );
116 return self::_getAllFiles ( $config,
'controllers', $silent );
119 public static function getControllers(
string $subClass =
"\\Ubiquity\\controllers\\Controller",
bool $backslash =
false,
bool $includeSubclass =
false,
bool $includeAbstract =
false): array {
121 if ($includeSubclass) {
122 $result [] = $subClass;
124 $config = Startup::getConfig ();
125 $files = self::getControllersFiles ( $config,
true );
127 $restCtrls = self::getRestCache ();
128 }
catch ( \Exception $e ) {
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;
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 createIndex(array $routes)
static parseControllerFiles(array &$config, bool $silent=false, bool $activeDomain=false)
static getAnnotationsEngineInstance()
static hasCapturingGroup(string $expression)
static getControllers(string $subClass="\\Ubiquity\\controllers\\Controller", bool $backslash=false, bool $includeSubclass=false, bool $includeAbstract=false)
static sortByPriority(array &$array)
static getFirstPartIndex(string $element)
static getAllControllersFiles(array &$config, bool $silent=false)
static addControllerCache(string $classname)
static getControllersFiles(array &$config, bool $silent=false)
Manage dependency injection.