|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\cache\parser; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UString; |
|
6
|
|
|
use Ubiquity\orm\parser\Reflexion; |
|
7
|
|
|
|
|
8
|
|
|
trait ControllerParserPathTrait { |
|
9
|
|
|
|
|
10
|
1 |
|
protected static function getPathFromMethod(\ReflectionMethod $method) { |
|
11
|
1 |
|
$methodName = $method->getName (); |
|
12
|
1 |
|
if ($methodName === "index") { |
|
13
|
1 |
|
$pathParts = [ "(index/)?" ]; |
|
14
|
|
|
} else { |
|
15
|
1 |
|
$pathParts = [ $methodName ]; |
|
16
|
|
|
} |
|
17
|
1 |
|
$parameters = $method->getParameters (); |
|
18
|
1 |
|
foreach ( $parameters as $parameter ) { |
|
19
|
1 |
|
if ($parameter->isVariadic ()) { |
|
20
|
1 |
|
$pathParts [] = '{...' . $parameter->getName () . '}'; |
|
21
|
1 |
|
return "/" . \implode ( "/", $pathParts ); |
|
22
|
|
|
} |
|
23
|
1 |
|
if (! $parameter->isOptional ()) { |
|
24
|
1 |
|
$pathParts [] = '{' . $parameter->getName () . '}'; |
|
25
|
|
|
} else { |
|
26
|
1 |
|
$pathParts [\sizeof ( $pathParts ) - 1] .= '{~' . $parameter->getName () . '}'; |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
1 |
|
return "/" . \implode ( "/", $pathParts ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
2 |
|
protected static function parseMethodPath(\ReflectionMethod $method, $path) { |
|
33
|
2 |
|
if (! isset ( $path ) || $path === '') |
|
34
|
|
|
return; |
|
35
|
2 |
|
$parameters = $method->getParameters (); |
|
36
|
2 |
|
foreach ( $parameters as $parameter ) { |
|
37
|
2 |
|
$name = $parameter->getName (); |
|
38
|
2 |
|
if ($parameter->isVariadic ()) { |
|
39
|
|
|
$path = str_replace ( '{' . $name . '}', '{...' . $name . '}', $path ); |
|
40
|
2 |
|
} elseif ($parameter->isOptional ()) { |
|
41
|
2 |
|
$path = str_replace ( '{' . $name . '}', '{~' . $name . '}', $path ); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
2 |
|
return $path; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
12 |
|
public static function cleanpath($prefix, $path = "") { |
|
48
|
12 |
|
$path = str_replace ( "//", "/", $path ); |
|
49
|
12 |
|
if ($prefix !== "" && ! UString::startswith ( $prefix, "/" )) { |
|
50
|
|
|
$prefix = "/" . $prefix; |
|
51
|
|
|
} |
|
52
|
12 |
|
if (! UString::endswith ( $prefix, "/" )) { |
|
53
|
12 |
|
$prefix = $prefix . "/"; |
|
54
|
|
|
} |
|
55
|
12 |
|
if ($path !== "" && UString::startswith ( $path, "/" )) { |
|
56
|
2 |
|
$path = \substr ( $path, 1 ); |
|
57
|
|
|
} |
|
58
|
12 |
|
$path = $prefix . $path; |
|
59
|
12 |
|
if (! UString::endswith ( $path, "/" ) && ! UString::endswith ( $path, '(.*?)' ) && ! UString::endswith ( $path, "(index/)?" )) { |
|
60
|
12 |
|
$path = $path . "/"; |
|
61
|
|
|
} |
|
62
|
12 |
|
return $path; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
12 |
|
public static function addParamsPath($path, \ReflectionMethod $method, $requirements) { |
|
66
|
12 |
|
$parameters = [ ]; |
|
67
|
12 |
|
$hasOptional = false; |
|
68
|
12 |
|
preg_match_all ( '@\{(\.\.\.|\~)?(.+?)\}@s', $path, $matches ); |
|
69
|
12 |
|
if (isset ( $matches [2] ) && \sizeof ( $matches [2] ) > 0) { |
|
70
|
2 |
|
$path = \preg_quote ( $path ); |
|
71
|
2 |
|
$params = Reflexion::getMethodParameters ( $method ); |
|
72
|
2 |
|
$index = 0; |
|
73
|
2 |
|
foreach ( $matches [2] as $paramMatch ) { |
|
74
|
2 |
|
$find = \array_search ( $paramMatch, $params ); |
|
75
|
2 |
|
if ($find !== false) { |
|
76
|
2 |
|
$requirement = '.+?'; |
|
77
|
2 |
|
if (isset ( $requirements [$paramMatch] )) { |
|
78
|
1 |
|
$requirement = $requirements [$paramMatch]; |
|
79
|
|
|
} |
|
80
|
2 |
|
self::scanParam ( $parameters, $hasOptional, $matches, $index, $paramMatch, $find, $path, $requirement ); |
|
81
|
|
|
} else { |
|
82
|
|
|
throw new \Exception ( "{$paramMatch} is not a parameter of the method " . $method->name ); |
|
83
|
|
|
} |
|
84
|
2 |
|
$index ++; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
12 |
|
if ($hasOptional) |
|
88
|
2 |
|
$path .= "/(.*?)"; |
|
89
|
12 |
|
return [ "path" => $path,"parameters" => $parameters ]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
public static function scanParam(&$parameters, &$hasOptional, $matches, $index, $paramMatch, $find, &$path, $requirement) { |
|
93
|
2 |
|
$toReplace = true; |
|
94
|
2 |
|
if (isset ( $matches [1] [$index] )) { |
|
95
|
2 |
|
if ($matches [1] [$index] === "...") { |
|
96
|
1 |
|
$parameters [] = "*"; |
|
97
|
1 |
|
$path = \str_replace ( "\{\.\.\." . $paramMatch . "\}", "(.*?)", $path ); |
|
98
|
1 |
|
$toReplace = false; |
|
99
|
2 |
|
} elseif ($matches [1] [$index] === "~") { |
|
100
|
2 |
|
$parameters [] = "~" . $find; |
|
101
|
2 |
|
$path = \str_replace ( "\{~" . $paramMatch . "\}", "", $path ); |
|
102
|
2 |
|
$hasOptional = true; |
|
103
|
2 |
|
$toReplace = false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
2 |
|
if ($toReplace) { |
|
107
|
2 |
|
$parameters [] = $find; |
|
108
|
2 |
|
$path = \str_replace ( "\{" . $paramMatch . "\}", "({$requirement})", $path ); |
|
109
|
|
|
} |
|
110
|
2 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|