21 $methodName = $method->getName ();
22 if ($methodName ===
"index") {
23 $pathParts = [
"(index/)?" ];
25 $pathParts = [ $methodName ];
27 $parameters = $method->getParameters ();
28 foreach ( $parameters as $parameter ) {
29 if ($parameter->isVariadic ()) {
30 $pathParts [] =
'{...' . $parameter->getName () .
'}';
31 return "/" . \implode (
"/", $pathParts );
33 if (! $parameter->isOptional ()) {
34 $pathParts [] =
'{' . $parameter->getName () .
'}';
36 $pathParts [\count ( $pathParts ) - 1] .=
'{~' . $parameter->getName () .
'}';
39 return "/" . \implode (
"/", $pathParts );
52 foreach ( $method->getParameters () as $param ) {
53 if($param->hasType()){
54 $type=$param->getType();
55 if($type instanceof \ReflectionNamedType){
56 switch ($type->getName()){
58 $requirements[$param->getName()]=
'\d+';
61 $requirements[$param->getName()]=
'[0-1]{1}';
64 $requirements[$param->getName()]=
'[+-]?([0-9]*[.])?[0-9]+';
73 public static function parseMethodPath(\ReflectionFunctionAbstract $method, $path) {
74 if (! isset ( $path ) || $path ===
'') {
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 );
89 public static function cleanpath($prefix, $path =
"", &$isRoot=
false) {
90 $path = \str_replace (
'//',
'/', $path );
91 if ($prefix !==
'' && ! UString::startswith ( $prefix,
'/' )) {
92 $prefix =
'/' . $prefix;
94 if (! UString::endswith ( $prefix,
'/' )) {
95 $prefix = $prefix .
'/';
97 if ($path !==
'' && UString::startswith ( $path,
'/' )) {
98 $path = \substr ( $path, 1 );
100 if(UString::startswith($path,
'#/')){
101 $path=\substr($path,1);
104 $path = $prefix . $path;
106 if (! UString::endswith ( $path,
'/' ) && ! UString::endswith ( $path,
'(.*?)' ) && ! UString::endswith ( $path,
'(index/)?' )) {
109 return \str_replace (
'//',
'/', $path );
112 public static function addParamsPath($path, \ReflectionFunctionAbstract $method, $requirements) {
114 $hasOptional =
false;
115 \preg_match_all (
'@\{(\.\.\.|\~)?(.+?)\}@s', $path, $matches );
117 if (isset ( $matches [2] ) && \count ( $matches [2] ) > 0) {
118 $path = \preg_quote ( $path );
119 $params = Reflexion::getMethodParameters ( $method );
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];
132 self::scanParam ( $parameters, $hasOptional, $matches, $index, $paramMatch, $find, $path, $requirement );
134 throw new ParserException (
"{$paramMatch} is not a parameter of the method " . $method->name );
142 $path=\str_replace(
'\\#',
'#',$path);
143 return [
'path' => $path,
'parameters' => $parameters ];
146 public static function scanParam(&$parameters, &$hasOptional, $matches, $index, $paramMatch, $find, &$path, $requirement) {
148 if (isset ( $matches [1] [$index] )) {
149 if ($matches [1] [$index] ===
'...') {
150 $parameters [] =
'*';
151 $path = \str_replace (
'\{\.\.\.' . $paramMatch .
'\}',
'(.*?)', $path );
153 } elseif ($matches [1] [$index] ===
'~') {
154 $parameters [] =
'~' . $find;
155 $path = \str_replace (
'\{~' . $paramMatch .
'\}',
'', $path );
161 $parameters [] = $find;
162 $path = \str_replace (
'\{' . $paramMatch .
'\}',
"({$requirement})", $path );
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);
177 throw new ParserException(
"Method $method does not exist on $controllerClass");
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;
186 throw new ParserException(
"Property $paramMatch must be public $controllerClass");
189 throw new ParserException(
"Property $paramMatch does not exist on $controllerClass");