|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\cache\parser; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UString; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Ubiquity\cache\parser$CallableParser |
|
9
|
|
|
* This class is part of Ubiquity |
|
10
|
|
|
* |
|
11
|
|
|
* @author jcheron <[email protected]> |
|
12
|
|
|
* @version 1.0.0 |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
class CallableParser { |
|
16
|
|
|
|
|
17
|
|
|
public static function parseRouteArray(&$result, $callable, $routeArray, \ReflectionFunction $function, $prefix = "", $httpMethods = NULL) { |
|
18
|
|
|
$pathParameters = self::addParamsPath ( $routeArray ["path"], $function, $routeArray ["requirements"] ); |
|
19
|
|
|
$name = $routeArray ["name"]; |
|
20
|
|
|
if (! isset ( $name )) { |
|
21
|
|
|
$name = UString::cleanAttribute ( $pathParameters ); |
|
22
|
|
|
} |
|
23
|
|
|
$cache = $routeArray ["cache"]; |
|
24
|
|
|
$duration = $routeArray ["duration"]; |
|
25
|
|
|
$path = $pathParameters ["path"]; |
|
26
|
|
|
$parameters = $pathParameters ["parameters"]; |
|
27
|
|
|
$priority = $routeArray ["priority"]; |
|
28
|
|
|
$path = self::cleanpath ( $prefix, $path ); |
|
|
|
|
|
|
29
|
|
|
if (isset ( $routeArray ["methods"] ) && \is_array ( $routeArray ["methods"] )) { |
|
30
|
|
|
self::createRouteMethod ( $result, $callable, $path, $routeArray ["methods"], $parameters, $name, $cache, $duration, $priority ); |
|
31
|
|
|
} elseif (\is_array ( $httpMethods )) { |
|
32
|
|
|
self::createRouteMethod ( $result, $callable, $path, $httpMethods, $parameters, $name, $cache, $duration, $priority ); |
|
33
|
|
|
} else { |
|
34
|
|
|
$result [$path] = [ "controller" => $callable,"action" => "","parameters" => $parameters,"name" => $name,"cache" => $cache,"duration" => $duration,"priority" => $priority ]; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private static function createRouteMethod(&$result, $callable, $path, $httpMethods, $parameters, $name, $cache, $duration, $priority) { |
|
39
|
|
|
foreach ( $httpMethods as $httpMethod ) { |
|
40
|
|
|
$result [$path] [$httpMethod] = [ "controller" => $callable,"action" => "","parameters" => $parameters,"name" => $name,"cache" => $cache,"duration" => $duration,"priority" => $priority ]; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function addParamsPath($path, \ReflectionFunction $function, $requirements) { |
|
45
|
|
|
$parameters = [ ]; |
|
46
|
|
|
$hasOptional = false; |
|
47
|
|
|
preg_match_all ( '@\{(\.\.\.|\~)?(.+?)\}@s', $path, $matches ); |
|
48
|
|
|
if (isset ( $matches [2] ) && \sizeof ( $matches [2] ) > 0) { |
|
49
|
|
|
$path = \preg_quote ( $path ); |
|
50
|
|
|
$params = $function->getParameters (); |
|
51
|
|
|
$index = 0; |
|
52
|
|
|
foreach ( $matches [2] as $paramMatch ) { |
|
53
|
|
|
$find = \array_search ( $paramMatch, $params ); |
|
54
|
|
|
if ($find !== false) { |
|
55
|
|
|
$requirement = '.+?'; |
|
56
|
|
|
if (isset ( $requirements [$paramMatch] )) { |
|
57
|
|
|
$requirement = $requirements [$paramMatch]; |
|
58
|
|
|
} |
|
59
|
|
|
ControllerParser::scanParam ( $parameters, $hasOptional, $matches, $index, $paramMatch, $find, $path, $requirement ); |
|
60
|
|
|
} else { |
|
61
|
|
|
throw new \Exception ( "{$paramMatch} is not a parameter of the function " . $function->name ); |
|
62
|
|
|
} |
|
63
|
|
|
$index ++; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
if ($hasOptional) |
|
67
|
|
|
$path .= "/(.*?)"; |
|
68
|
|
|
return [ "path" => $path,"parameters" => $parameters ]; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.