Ubiquity  2.0.3
php rapid development framework
Router.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\controllers;
4 
9 
16 class Router {
17  private static $routes;
18 
19  public static function slashPath($path) {
20  if (UString::startswith ( $path, "/" ) === false)
21  $path = "/" . $path;
22  if (! UString::endswith ( $path, "/" ))
23  $path = $path . "/";
24  return $path;
25  }
26 
27  public static function start() {
28  self::$routes = CacheManager::getControllerCache ();
29  }
30 
31  public static function startRest() {
32  self::$routes = CacheManager::getControllerCache ( true );
33  }
34 
35  public static function getRoute($path, $cachedResponse = true) {
36  $path = self::slashPath ( $path );
37  foreach ( self::$routes as $routePath => $routeDetails ) {
38  if (preg_match ( "@^" . $routePath . "$@s", $path, $matches )) {
39  if (! isset ( $routeDetails ["controller"] )) {
40  $method = URequest::getMethod ();
41  if (isset ( $routeDetails [$method] ))
42  return self::getRouteUrlParts ( [ "path" => $routePath,"details" => $routeDetails [$method] ], $matches, $routeDetails [$method] ["cache"], $routeDetails [$method] ["duration"], $cachedResponse );
43  } else
44  return self::getRouteUrlParts ( [ "path" => $routePath,"details" => $routeDetails ], $matches, $routeDetails ["cache"], $routeDetails ["duration"], $cachedResponse );
45  }
46  }
47  return false;
48  }
49 
50  public static function getRouteInfoByControllerAction($controller, $action) {
51  foreach ( self::$routes as $routePath => $routeDetails ) {
52  if (! isset ( $routeDetails ["controller"] )) {
53  $routeDetails = \reset ( $routeDetails );
54  }
55  if ($controller === $routeDetails ["controller"] && $action === $routeDetails ["action"]) {
56  $routeDetails ["path"] = $routePath;
57  return $routeDetails;
58  }
59  }
60  return false;
61  }
62 
63  public static function filterRoutes($path) {
64  $path = self::slashPath ( $path );
65  $result = [ ];
66  foreach ( self::$routes as $routePath => $routeDetails ) {
67  if (preg_match ( "@^" . $routePath . ".*?$@s", $path, $matches )) {
68  $result [$routePath] = $routeDetails;
69  }
70  }
71  return $result;
72  }
73 
74  public static function getRouteInfo($path) {
75  $path = self::slashPath ( $path );
76  foreach ( self::$routes as $routePath => $routeDetails ) {
77  if (preg_match ( "@^" . $routePath . "$@s", $path, $matches ) || \stripslashes ( $routePath ) == $path) {
78  if (! isset ( $routeDetails ["controller"] )) {
79  return \reset ( $routeDetails );
80  } else
81  return $routeDetails;
82  }
83  }
84  return false;
85  }
86 
87  public static function getAnnotations($controllerName, $actionName) {
88  $result = [ ];
89  foreach ( self::$routes as $routePath => $routeDetails ) {
90  if (! isset ( $routeDetails ["controller"] )) {
91  $routeDetails = \reset ( $routeDetails );
92  }
93  if ($routeDetails ["controller"] === $controllerName && $routeDetails ["action"] === $actionName)
94  $result [$routePath] = $routeDetails;
95  }
96  return $result;
97  }
98 
108  public static function getRouteByName($name, $parameters = [], $absolute = true) {
109  foreach ( self::$routes as $routePath => $routeDetails ) {
110  if (self::checkRouteName ( $routeDetails, $name )) {
111  if (\sizeof ( $parameters ) > 0)
112  $routePath = self::_getURL ( $routePath, $parameters );
113  if (! $absolute)
114  return \ltrim ( $routePath, '/' );
115  else
116  return $routePath;
117  }
118  }
119  return false;
120  }
121 
133  public static function path($name, $parameters = [], $absolute = false) {
134  return self::getRouteByName ( $name, $parameters, $absolute );
135  }
136 
146  public static function url($name, $parameters = []) {
147  return URequest::getUrl ( self::getRouteByName ( $name, $parameters, false ) );
148  }
149 
150  protected static function _getURL($routePath, $params) {
151  $result = \preg_replace_callback ( '~\((.*?)\)~', function () use (&$params) {
152  return array_shift ( $params );
153  }, $routePath );
154  if (\sizeof ( $params ) > 0) {
155  $result = \rtrim ( $result, '/' ) . '/' . \implode ( '/', $params );
156  }
157  return $result;
158  }
159 
160  protected static function checkRouteName($routeDetails, $name) {
161  if (! isset ( $routeDetails ["name"] )) {
162  foreach ( $routeDetails as $methodRouteDetail ) {
163  if (isset ( $methodRouteDetail ["name"] ) && $methodRouteDetail == $name)
164  return true;
165  }
166  }
167  return isset ( $routeDetails ["name"] ) && $routeDetails ["name"] == $name;
168  }
169 
170  public static function getRouteUrlParts($routeArray, $params, $cached = false, $duration = NULL, $cachedResponse = true) {
171  $params = \array_slice ( $params, 1 );
172  $ctrl = str_replace ( "\\\\", "\\", $routeArray ["details"] ["controller"] );
173  $result = [ $ctrl,$routeArray ["details"] ["action"] ];
174  $paramsOrder = $routeArray ["details"] ["parameters"];
175  $index = 0;
176  foreach ( $paramsOrder as $order ) {
177  if ($order === "*") {
178  if (isset ( $params [$index] ))
179  $result = \array_merge ( $result, \array_diff ( \explode ( "/", $params [$index] ), [ "" ] ) );
180  break;
181  }
182  if (\substr ( $order, 0, 1 ) === "~") {
183  $order = \intval ( \substr ( $order, 1, 1 ) );
184  if (isset ( $params [$order] )) {
185  $result = \array_merge ( $result, \array_diff ( \explode ( "/", $params [$order] ), [ "" ] ) );
186  break;
187  }
188  }
189  $result [] = self::cleanParam ( $params [$order] );
190  unset ( $params [$order] );
191  $index ++;
192  }
193  if ($cached === true && $cachedResponse === true) {
194  return CacheManager::getRouteCache ( $result, $duration );
195  }
196  return $result;
197  }
198 
199  private static function cleanParam($param) {
200  if (UString::endswith ( $param, "/" ))
201  return \substr ( $param, 0, - 1 );
202  return $param;
203  }
204 
211  public static function setExpired($routePath, $expired = true) {
212  CacheManager::setExpired ( $routePath, $expired );
213  }
214 
226  public static function addRoute($path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = []) {
227  self::addRouteToRoutes ( self::$routes, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements );
228  }
229 
230  public static function addRouteToRoutes(&$routesArray, $path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = []) {
231  $result = [ ];
232  if (\class_exists ( $controller )) {
233  $method = new \ReflectionMethod ( $controller, $action );
234  ControllerParser::parseRouteArray ( $result, $controller, [ "path" => $path,"methods" => $methods,"name" => $name,"cache" => $cache,"duration" => $duration,"requirements" => $requirements ], $method, $action );
235  foreach ( $result as $k => $v ) {
236  $routesArray [$k] = $v;
237  }
238  }
239  }
240 }
static getMethod()
Returns the http method.
Definition: URequest.php:167
static url($name, $parameters=[])
Returns the generated url from a route.
Definition: Router.php:146
static setExpired($routePath, $expired=true)
static getRouteInfo($path)
Definition: Router.php:74
static checkRouteName($routeDetails, $name)
Definition: Router.php:160
static addRouteToRoutes(&$routesArray, $path, $controller, $action="index", $methods=null, $name="", $cache=false, $duration=null, $requirements=[])
Definition: Router.php:230
static addRoute($path, $controller, $action="index", $methods=null, $name="", $cache=false, $duration=null, $requirements=[])
Definition: Router.php:226
static _getURL($routePath, $params)
Definition: Router.php:150
static getRouteInfoByControllerAction($controller, $action)
Definition: Router.php:50
static getRoute($path, $cachedResponse=true)
Definition: Router.php:35
static setExpired($routePath, $expired=true)
Déclare une route comme étant expirée ou non.
Definition: Router.php:211
static getAnnotations($controllerName, $actionName)
Definition: Router.php:87
static getRouteUrlParts($routeArray, $params, $cached=false, $duration=NULL, $cachedResponse=true)
Definition: Router.php:170
static cleanParam($param)
Definition: Router.php:199
static path($name, $parameters=[], $absolute=false)
Returns the generated path from a route.
Definition: Router.php:133
static parseRouteArray(&$result, $controllerClass, $routeArray, \ReflectionMethod $method, $methodName, $prefix="", $httpMethods=NULL)
static getRouteCache($routePath, $duration)
static getRouteByName($name, $parameters=[], $absolute=true)
Returns the generated path from a route.
Definition: Router.php:108
static filterRoutes($path)
Definition: Router.php:63
This file is part of Ubiquity framework.
static slashPath($path)
Definition: Router.php:19
static endswith($hay, $needle)
Definition: UString.php:16
static startswith($hay, $needle)
Definition: UString.php:12