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