1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait for admin part of Router class. |
7
|
|
|
* Ubiquity\controllers\traits$RouterAdminTrait |
8
|
|
|
* This class is part of Ubiquity |
9
|
|
|
* |
10
|
|
|
* @author jcheron <[email protected]> |
11
|
|
|
* @version 1.0.3 |
12
|
|
|
* @property array $routes |
13
|
|
|
*/ |
14
|
|
|
trait RouterAdminTrait { |
15
|
|
|
|
16
|
|
|
abstract public static function slashPath($path): string; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @param string $controller |
21
|
|
|
* @param string $action |
22
|
|
|
* @return array|boolean |
23
|
|
|
*/ |
24
|
1 |
|
public static function getRouteInfoByControllerAction($controller, $action) { |
25
|
1 |
|
foreach ( self::$routes as $routePath => $routeDetails ) { |
26
|
1 |
|
if (! isset ( $routeDetails ['controller'] )) { |
27
|
1 |
|
$routeDetails = \current ( $routeDetails ); |
28
|
|
|
} |
29
|
1 |
|
if ($controller === $routeDetails ['controller'] && $action === $routeDetails ['action']) { |
30
|
|
|
$routeDetails ['path'] = $routePath; |
31
|
1 |
|
return $routeDetails; |
32
|
|
|
} |
33
|
|
|
} |
34
|
1 |
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public static function getRoutesPathByController($controller): array { |
38
|
1 |
|
$result = [ ]; |
39
|
1 |
|
foreach ( self::$routes as $routePath => $routeDetails ) { |
40
|
1 |
|
if (! isset ( $routeDetails ['controller'] )) { |
41
|
1 |
|
foreach ( $routeDetails as $method => $routeInfo ) { |
42
|
1 |
|
if ($routeInfo ['controller'] === $controller) { |
43
|
1 |
|
$result [] = [ 'method' => $method,'url' => self::getRoutePathInfos ( $controller, $routePath, $routeInfo ) ]; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} else { |
47
|
1 |
|
if ($routeDetails ['controller'] === $controller) { |
48
|
1 |
|
$result [] = [ 'method' => '*','url' => self::getRoutePathInfos ( $controller, $routePath, $routeDetails ) ]; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
1 |
|
return $result; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public static function getRoutePathInfos($controller, $routePath, $routeInfo) { |
56
|
1 |
|
$method = new \ReflectionMethod ( $controller, $routeInfo ['action'] ); |
57
|
1 |
|
$parameters = $method->getParameters (); |
58
|
1 |
|
$routeParams = $routeInfo ['parameters']; |
59
|
1 |
|
$pattern = "@\(.*?\)@"; |
60
|
1 |
|
$params = [ ]; |
61
|
1 |
|
foreach ( $routeParams as $param ) { |
62
|
1 |
|
if ($param === '*') { |
63
|
1 |
|
$params [] = $parameters [\sizeof ( $params )]->getName (); |
64
|
|
|
} else { |
65
|
1 |
|
$index = ( int ) \filter_var ( $param, FILTER_SANITIZE_NUMBER_INT ); |
66
|
1 |
|
$params [] = $parameters [$index]->getName (); |
67
|
|
|
} |
68
|
|
|
} |
69
|
1 |
|
$path = $routePath; |
70
|
1 |
|
foreach ( $params as $param ) { |
71
|
1 |
|
$path = \preg_replace ( $pattern, '{' . $param . '}', $path, 1 ); |
72
|
|
|
} |
73
|
1 |
|
return $path; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
* @param string $path |
79
|
|
|
* @return array|boolean |
80
|
|
|
*/ |
81
|
|
|
public static function getRouteInfo($path) { |
82
|
|
|
$path = self::slashPath ( $path ); |
83
|
|
|
foreach ( self::$routes as $routePath => $routeDetails ) { |
84
|
|
|
if (\preg_match ( "@^{$routePath}\$@s", $path, $matches ) || \stripslashes ( $routePath ) == $path) { |
85
|
|
|
if (! isset ( $routeDetails ['controller'] )) { |
86
|
|
|
return \current ( $routeDetails ); |
87
|
|
|
} else |
88
|
|
|
return $routeDetails; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public static function getAnnotations($controllerName, $actionName): array { |
95
|
1 |
|
$result = [ ]; |
96
|
1 |
|
foreach ( self::$routes as $routePath => $routeDetails ) { |
97
|
1 |
|
if (! isset ( $routeDetails ['controller'] )) { |
98
|
1 |
|
$routeDetails = \current ( $routeDetails ); |
99
|
|
|
} |
100
|
1 |
|
if ($routeDetails ['controller'] === $controllerName && $routeDetails ['action'] === $actionName) |
101
|
1 |
|
$result [$routePath] = $routeDetails; |
102
|
|
|
} |
103
|
1 |
|
return $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public static function filterRoutes($path) { |
107
|
|
|
$path = self::slashPath ( $path ); |
108
|
|
|
$result = [ ]; |
109
|
|
|
foreach ( self::$routes as $routePath => $routeDetails ) { |
110
|
|
|
if (\preg_match ( "@^{$routePath}.*?$@s", $path, $matches )) { |
111
|
|
|
$result [$routePath] = $routeDetails; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|