47 $restAnnotsClass = [ ];
49 if (! $reflect->isAbstract () && $reflect->isSubclassOf ( \
Ubiquity\
controllers\Controller::class )) {
52 $restAnnotsClass = Reflexion::getAnnotationClass (
$controllerClass,
'rest' );
53 }
catch ( \Exception $e ) {
56 $this->rest = \count ( $restAnnotsClass ) > 0;
57 if (isset ( $annotsClass ) && \count ( $annotsClass ) > 0) {
58 $this->mainRouteClass = $annotsClass [0];
59 $inherited = $this->mainRouteClass->inherited;
60 $automated = $this->mainRouteClass->automated;
62 $methods = Reflexion::getMethods (
$controllerClass, \ReflectionMethod::IS_PUBLIC );
68 foreach ( $methods as $method ) {
69 if ($method->getDeclaringClass ()->getName () ===
$controllerClass || $inherited) {
71 $annots = Reflexion::getAnnotationsMethod (
$controllerClass, $method->name, [
'route',
'get',
'post',
'patch',
'put',
'delete',
'options' ] );
72 if (\count ( $annots ) > 0) {
73 foreach ( $annots as $annot ) {
76 $this->routesMethods [$method->name] = [
'annotations' => $annots,
'method' => $method ];
82 }
catch ( \Exception $e ) {
92 private function isRoutable(\ReflectionMethod $method):bool{
93 return $method->class !==
'Ubiquity\\controllers\\Controller'
94 && \array_search ( $method->name, self::
$excludeds ) === false
95 && !
UString::startswith ( $method->name,
'_' )
96 &&
Reflexion::getAnnotationsMethod($method->class,$method->name,
'noRoute')===false;
100 if (UString::isNull ( $annot->path )) {
101 $newAnnot = $this->generateRouteAnnotationFromMethod ( $method );
102 $annot->path = $newAnnot [0]->path;
104 $annot->path = $this->parseMethodPath ( $method, $annot->path );
120 $httpMethods =
false;
121 if ($this->mainRouteClass) {
122 if (isset ( $this->mainRouteClass->path )) {
123 $this->mainRouteClass->path=self::parseMainPath($this->mainRouteClass->path,$this->controllerClass);
124 $prefix = $this->mainRouteClass->path;
126 if (isset ( $this->mainRouteClass->methods )) {
127 $httpMethods = $this->mainRouteClass->methods;
128 if ($httpMethods !==
null) {
129 if (\is_string ( $httpMethods ))
130 $httpMethods = [ $httpMethods ];
134 foreach ( $this->routesMethods as $method => $arrayAnnotsMethod ) {
135 $routeAnnotations = $arrayAnnotsMethod [
'annotations'];
137 foreach ( $routeAnnotations as $routeAnnotation ) {
138 $params = [
'path' => $routeAnnotation->path,
'methods' => $routeAnnotation->methods,
'name' => $routeAnnotation->name,
'cache' => $routeAnnotation->cache,
'duration' => $routeAnnotation->duration,
'requirements' => $routeAnnotation->requirements,
'priority' => $routeAnnotation->priority ];
139 self::parseRouteArray ( $result, $this->controllerClass, $params, $arrayAnnotsMethod [
'method'], $method, $prefix, $httpMethods );
142 self::$mainParams=
null;
146 public static function parseRouteArray(&$result, $controllerClass, $routeArray, \ReflectionMethod $method, $methodName, $prefix =
'', $httpMethods = NULL) {
147 if (! isset ( $routeArray [
'path'] )) {
148 $routeArray [
'path'] = self::getPathFromMethod ( $method );
150 $pathParameters = self::addParamsPath ( $routeArray [
'path'], $method, $routeArray [
'requirements'] );
151 $name = $routeArray [
'name'];
152 if (! isset ( $name )) {
153 $name = self::generateRouteName($controllerClass,$methodName);
155 $cache = $routeArray [
'cache'];
156 $duration = $routeArray [
'duration'];
157 $path = $pathParameters [
'path'];
158 $parameters = $pathParameters [
'parameters'];
159 $priority = $routeArray [
'priority'];
160 $callback = $routeArray [
'callback'] ??
null;
162 $path = self::cleanpath ( $prefix, $path ,$isRoot);
163 if (isset ( $routeArray [
'methods'] ) && \is_array ( $routeArray [
'methods'] )) {
164 self::createRouteMethod ( $result, $controllerClass, $path, $routeArray [
'methods'], $methodName, $parameters, $name, $cache, $duration, $priority, $callback , $isRoot);
165 } elseif (\is_array ( $httpMethods )) {
166 self::createRouteMethod ( $result, $controllerClass, $path, $httpMethods, $methodName, $parameters, $name, $cache, $duration, $priority, $callback , $isRoot);
168 $v = [
'controller' => $controllerClass,
'action' => $methodName,
'parameters' => $parameters,
'name' => $name,
'cache' => $cache,
'duration' => $duration,
'priority' => $priority];
169 if (isset ( $callback )) {
170 $v [
'callback'] = $callback;
172 if(!$isRoot && isset(self::$mainParams) && \count(self::$mainParams)>0){
173 $v[
'main.params']=self::$mainParams;
175 $result [$path] = $v;
179 private static function createRouteMethod(&$result, $controllerClass, $path, $httpMethods, $method, $parameters, $name, $cache, $duration, $priority, $callback =
null,$isRoot=
false) {
180 foreach ( $httpMethods as $httpMethod ) {
181 $httpMethod=\strtolower($httpMethod);
182 if(\array_search($httpMethod, self::HTTP_METHODS)===
false){
185 $v = [
'controller' => $controllerClass,
'action' => $method,
'parameters' => $parameters,
'name' => $name,
'cache' => $cache,
'duration' => $duration,
'priority' => $priority ];
186 if (isset ( $callback )) {
187 $v [
'callback'] = $callback;
189 if(!$isRoot && isset(self::$mainParams) && \count(self::$mainParams)>0){
190 $v[
'main.params']=self::$mainParams;
192 $result [$path] [$httpMethod] = $v;