Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ControllerParserPathTrait.php
Go to the documentation of this file.
1<?php
2
4
9
19 protected static $mainParams;
20 protected static function getPathFromMethod(\ReflectionMethod $method) {
21 $methodName = $method->getName ();
22 if ($methodName === "index") {
23 $pathParts = [ "(index/)?" ];
24 } else {
25 $pathParts = [ $methodName ];
26 }
27 $parameters = $method->getParameters ();
28 foreach ( $parameters as $parameter ) {
29 if ($parameter->isVariadic ()) {
30 $pathParts [] = '{...' . $parameter->getName () . '}';
31 return "/" . \implode ( "/", $pathParts );
32 }
33 if (! $parameter->isOptional ()) {
34 $pathParts [] = '{' . $parameter->getName () . '}';
35 } else {
36 $pathParts [\count ( $pathParts ) - 1] .= '{~' . $parameter->getName () . '}';
37 }
38 }
39 return "/" . \implode ( "/", $pathParts );
40 }
41
42 private static function checkParams(\ReflectionFunctionAbstract $method,$actualParams){
43 foreach ( $method->getParameters () as $param ) {
44 if(!$param->isOptional() && \array_search($param->name,$actualParams)===false){
45 throw new ParserException(sprintf('The parameter %s is not present in the route path although it is mandatory.',$param->name));
46 }
47 }
48 }
49
50 private static function checkParamsTypesForRequirement(\ReflectionFunctionAbstract $method){
51 $requirements=[];
52 foreach ( $method->getParameters () as $param ) {
53 if($param->hasType()){
54 $type=$param->getType();
55 if($type instanceof \ReflectionNamedType){
56 switch ($type->getName()){
57 case 'int':
58 $requirements[$param->getName()]='\d+';
59 break;
60 case 'bool':
61 $requirements[$param->getName()]='[0-1]{1}';
62 break;
63 case 'float':
64 $requirements[$param->getName()]='[+-]?([0-9]*[.])?[0-9]+';
65 break;
66 }
67 }
68 }
69 }
70 return $requirements;
71 }
72
73 public static function parseMethodPath(\ReflectionFunctionAbstract $method, $path) {
74 if (! isset ( $path ) || $path === '') {
75 return;
76 }
77 $parameters = $method->getParameters ();
78 foreach ( $parameters as $parameter ) {
79 $name = $parameter->getName ();
80 if ($parameter->isVariadic ()) {
81 $path = \str_replace ( '{' . $name . '}', '{...' . $name . '}', $path );
82 } elseif ($parameter->isOptional ()) {
83 $path = \str_replace ( '{' . $name . '}', '{~' . $name . '}', $path );
84 }
85 }
86 return $path;
87 }
88
89 public static function cleanpath($prefix, $path = "", &$isRoot=false) {
90 $path = \str_replace ( '//', '/', $path );
91 if ($prefix !== '' && ! UString::startswith ( $prefix, '/' )) {
92 $prefix = '/' . $prefix;
93 }
94 if (! UString::endswith ( $prefix, '/' )) {
95 $prefix = $prefix . '/';
96 }
97 if ($path !== '' && UString::startswith ( $path, '/' )) {
98 $path = \substr ( $path, 1 );
99 }
100 if(UString::startswith($path,'#/')){
101 $path=\substr($path,1);
102 $isRoot=true;
103 }else {
104 $path = $prefix . $path;
105 }
106 if (! UString::endswith ( $path, '/' ) && ! UString::endswith ( $path, '(.*?)' ) && ! UString::endswith ( $path, '(index/)?' )) {
107 $path = $path . '/';
108 }
109 return \str_replace ( '//', '/', $path );
110 }
111
112 public static function addParamsPath($path, \ReflectionFunctionAbstract $method, $requirements) {
113 $parameters = [ ];
114 $hasOptional = false;
115 \preg_match_all ( '@\{(\.\.\.|\~)?(.+?)\}@s', $path, $matches );
116 self::checkParams($method,$matches[2]??[]);
117 if (isset ( $matches [2] ) && \count ( $matches [2] ) > 0) {
118 $path = \preg_quote ( $path );
119 $params = Reflexion::getMethodParameters ( $method );
120 $typeRequirements=self::checkParamsTypesForRequirement($method);
121 $index = 0;
122 foreach ( $matches [2] as $paramMatch ) {
123 $find = \array_search ( $paramMatch, $params );
124 if ($find !== false) {
125 unset($params[$find]);
126 $requirement = '.+?';
127 if (isset ( $requirements [$paramMatch] )) {
128 $requirement = $requirements [$paramMatch];
129 }elseif (isset($typeRequirements[$paramMatch])){
130 $requirement = $typeRequirements [$paramMatch];
131 }
132 self::scanParam ( $parameters, $hasOptional, $matches, $index, $paramMatch, $find, $path, $requirement );
133 } else {
134 throw new ParserException ( "{$paramMatch} is not a parameter of the method " . $method->name );
135 }
136 $index ++;
137 }
138 }
139 if ($hasOptional) {
140 $path .= '/(.*?)';
141 }
142 $path=\str_replace('\\#','#',$path);
143 return [ 'path' => $path,'parameters' => $parameters ];
144 }
145
146 public static function scanParam(&$parameters, &$hasOptional, $matches, $index, $paramMatch, $find, &$path, $requirement) {
147 $toReplace = true;
148 if (isset ( $matches [1] [$index] )) {
149 if ($matches [1] [$index] === '...') {
150 $parameters [] = '*';
151 $path = \str_replace ( '\{\.\.\.' . $paramMatch . '\}', '(.*?)', $path );
152 $toReplace = false;
153 } elseif ($matches [1] [$index] === '~') {
154 $parameters [] = '~' . $find;
155 $path = \str_replace ( '\{~' . $paramMatch . '\}', '', $path );
156 $hasOptional = true;
157 $toReplace = false;
158 }
159 }
160 if ($toReplace) {
161 $parameters [] = $find;
162 $path = \str_replace ( '\{' . $paramMatch . '\}', "({$requirement})", $path );
163 }
164 }
165
166 protected static function parseMainPath(string $path,string $controllerClass): string{
167 \preg_match_all ( '@\{(.+?)\}@s', $path, $matches );
168 self::$mainParams=[];
169 if (isset ( $matches [1] ) && \count ( $matches [1] ) > 0) {
170 foreach ( $matches [1] as $paramMatch ) {
171 if(\substr($paramMatch, -2) === '()'){
172 $method=\substr($paramMatch,0,\strlen($paramMatch)-2);
173 if(\method_exists($controllerClass,$method)){
174 self::$mainParams[]=$method;
175 $path = \str_replace('{' . $paramMatch . '}', '(.+?)', $path);
176 }else{
177 throw new ParserException("Method $method does not exist on $controllerClass");
178 }
179 }else{
180 if(\property_exists($controllerClass,$paramMatch)){
181 $rProp=new \ReflectionProperty($controllerClass,$paramMatch);
182 if($rProp->isPublic()){
183 $path = \str_replace('{' . $paramMatch . '}', '(.+?)', $path);
184 self::$mainParams[]=$paramMatch;
185 }else{
186 throw new ParserException("Property $paramMatch must be public $controllerClass");
187 }
188 }else{
189 throw new ParserException("Property $paramMatch does not exist on $controllerClass");
190 }
191 }
192
193 }
194 }
195 return $path;
196 }
197}
198
Manager for caches (Router, Rest, models).
Ubiquity\cache\parser$ControllerParserPathTrait This class is part of Ubiquity.
static checkParams(\ReflectionFunctionAbstract $method, $actualParams)
static addParamsPath($path, \ReflectionFunctionAbstract $method, $requirements)
static parseMainPath(string $path, string $controllerClass)
static cleanpath($prefix, $path="", &$isRoot=false)
static scanParam(&$parameters, &$hasOptional, $matches, $index, $paramMatch, $find, &$path, $requirement)
static parseMethodPath(\ReflectionFunctionAbstract $method, $path)
static checkParamsTypesForRequirement(\ReflectionFunctionAbstract $method)
Reflection utilities in dev environment only.
Definition Reflexion.php:17
String utilities.
Definition UString.php:15