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