15 private static $excludeds=[
"__construct",
"isValid",
"initialize",
"finalize",
"onInvalidControl",
"loadView",
"forward",
"redirectToRoute" ];
22 if (!$reflect->isAbstract() && $reflect->isSubclassOf(
"Ubiquity\controllers\Controller")) {
26 $this->rest=\sizeof($restAnnotsClass) > 0;
27 if (\
sizeof($annotsClass) > 0) {
28 $this->mainRouteClass=$annotsClass[0];
29 $inherited=$this->mainRouteClass->inherited;
30 $automated=$this->mainRouteClass->automated;
33 foreach ( $methods as $method ) {
34 if ($method->getDeclaringClass()->getName() ===
$controllerClass || $inherited) {
36 if ($annots !==
false) {
37 foreach ( $annots as $annot ) {
40 $annot->path=$newAnnot[0]->path;
43 $this->routesMethods[$method->name]=[
"annotations" => $annots,
"method" => $method ];
46 if ($method->class !==
'Ubiquity\\controllers\\Controller' && \array_search($method->name, self::$excludeds) ===
false && !
UString::startswith($method->name,
"_"))
57 $annot->path=self::getPathFromMethod($method);
62 $methodName=$method->getName();
63 if ($methodName ===
"index") {
64 $pathParts=[
"(index/)?" ];
66 $pathParts=[ $methodName ];
68 $parameters=$method->getParameters();
69 foreach ( $parameters as $parameter ) {
70 if ($parameter->isVariadic()) {
71 $pathParts[]=
'{...' . $parameter->getName() .
'}';
72 return "/" . \implode(
"/", $pathParts);
74 if (!$parameter->isOptional()) {
75 $pathParts[]=
'{' . $parameter->getName() .
'}';
77 $pathParts[\sizeof($pathParts) - 1].=
'{~' . $parameter->getName() .
'}';
80 return "/" . \implode(
"/", $pathParts);
83 private static function cleanpath($prefix, $path=
"") {
85 $prefix=$prefix .
"/";
87 $path=\substr($path, 1);
88 $path=$prefix . $path;
98 if ($this->mainRouteClass) {
99 if (isset($this->mainRouteClass->path))
100 $prefix=$this->mainRouteClass->path;
101 if (isset($this->mainRouteClass->methods)) {
102 $httpMethods=$this->mainRouteClass->methods;
103 if ($httpMethods !== null) {
104 if (\is_string($httpMethods))
105 $httpMethods=[ $httpMethods ];
109 foreach ( $this->routesMethods as $method => $arrayAnnotsMethod ) {
110 $routeAnnotations=$arrayAnnotsMethod[
"annotations"];
112 foreach ( $routeAnnotations as $routeAnnotation ) {
113 $params=[
"path" => $routeAnnotation->path,
"methods" => $routeAnnotation->methods,
"name" => $routeAnnotation->name,
"cache" => $routeAnnotation->cache,
"duration" => $routeAnnotation->duration,
"requirements" => $routeAnnotation->requirements ];
114 self::parseRouteArray($result, $this->controllerClass, $params, $arrayAnnotsMethod[
"method"], $method, $prefix, $httpMethods);
121 if (!isset($routeArray[
"path"])) {
122 $routeArray[
"path"]=self::getPathFromMethod($method);
124 $pathParameters=self::addParamsPath($routeArray[
"path"], $method, $routeArray[
"requirements"]);
125 $name=$routeArray[
"name"];
129 $cache=$routeArray[
"cache"];
130 $duration=$routeArray[
"duration"];
131 $path=$pathParameters[
"path"];
132 $parameters=$pathParameters[
"parameters"];
133 $path=self::cleanpath($prefix, $path);
135 if (isset($routeArray[
"methods"]) && \is_array($routeArray[
"methods"])) {
136 self::createRouteMethod($result,
$controllerClass, $path, $routeArray[
"methods"], $methodName, $parameters, $name, $cache, $duration);
137 } elseif (\is_array($httpMethods)) {
138 self::createRouteMethod($result,
$controllerClass, $path, $httpMethods, $methodName, $parameters, $name, $cache, $duration);
140 $result[$path]=[
"controller" =>
$controllerClass,
"action" => $methodName,
"parameters" => $parameters,
"name" => $name,
"cache" => $cache,
"duration" => $duration];
144 public static function addParamsPath($path, \ReflectionMethod $method, $requirements) {
147 preg_match_all(
'@\{(\.\.\.|\~)?(.+?)\}@s', $path, $matches);
148 if (isset($matches[2]) && \
sizeof($matches[2]) > 0) {
149 $path=\preg_quote($path);
152 foreach ( $matches[2] as $paramMatch ) {
153 $find=\array_search($paramMatch, $params);
154 if ($find !==
false) {
156 if (isset($requirements[$paramMatch])) {
157 $requirement=$requirements[$paramMatch];
159 self::scanParam($parameters, $hasOptional, $matches, $index, $paramMatch, $find, $path, $requirement);
161 throw new \Exception(
"{$paramMatch} is not a parameter of the method " . $method->name);
168 return [
"path" => $path,
"parameters" => $parameters ];
171 private static function scanParam(&$parameters, &$hasOptional, $matches, $index, $paramMatch, $find, &$path, $requirement) {
172 if (isset($matches[1][$index])) {
173 if ($matches[1][$index] ===
"...") {
175 $path=\str_replace(
"\{\.\.\." . $paramMatch .
"\}",
"(.*?)", $path);
176 } elseif ($matches[1][$index] ===
"~") {
177 $parameters[]=
"~" . $find;
178 $path=\str_replace(
"\{~" . $paramMatch .
"\}",
"", $path);
182 $path=\str_replace(
"\{" . $paramMatch .
"\}",
"({$requirement})", $path);
186 $path=\str_replace(
"\{" . $paramMatch .
"\}",
"({$requirement})", $path);
191 foreach ( $httpMethods as $httpMethod ) {
192 $result[$path][$httpMethod]=[
"controller" =>
$controllerClass,
"action" => $method,
"parameters" => $parameters,
"name" => $name,
"cache" => $cache,
"duration" => $duration ];
static addParamsPath($path, \ReflectionMethod $method, $requirements)
static getAnnotationsMethod($class, $method, $annotation)
static cleanpath($prefix, $path="")
static getPathFromMethod(\ReflectionMethod $method)
static getMethodParameters(\ReflectionMethod $method)
('method'=>true,'class'=>true,'multiple'=>true, 'inherited'=>true)
static createRouteMethod(&$result, $controllerClass, $path, $httpMethods, $method, $parameters, $name, $cache, $duration)
static scanParam(&$parameters, &$hasOptional, $matches, $index, $paramMatch, $find, &$path, $requirement)
generateRouteAnnotationFromMethod(\ReflectionMethod $method)
static getAnnotationClass($class, $annotation)
static cleanAttribute($attr, $replacement="_")
static getMethods($instance, $filter=null)
static parseRouteArray(&$result, $controllerClass, $routeArray, \ReflectionMethod $method, $methodName, $prefix="", $httpMethods=NULL)
static cleanClassname($classname)
static endswith($hay, $needle)
static getClassSimpleName($classnameWithNamespace)
static startswith($hay, $needle)