27 public static function start() {
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"] )) {
41 if (isset ( $routeDetails [$method] ))
42 return self::getRouteUrlParts ( [
"path" => $routePath,
"details" => $routeDetails [$method] ], $matches, $routeDetails [$method] [
"cache"], $routeDetails [$method] [
"duration"], $cachedResponse );
44 return self::getRouteUrlParts ( [
"path" => $routePath,
"details" => $routeDetails ], $matches, $routeDetails [
"cache"], $routeDetails [
"duration"], $cachedResponse );
51 foreach ( self::$routes as $routePath => $routeDetails ) {
52 if (! isset ( $routeDetails [
"controller"] )) {
53 $routeDetails = \reset ( $routeDetails );
55 if ($controller === $routeDetails [
"controller"] && $action === $routeDetails [
"action"]) {
56 $routeDetails [
"path"] = $routePath;
64 $path = self::slashPath ( $path );
66 foreach ( self::$routes as $routePath => $routeDetails ) {
67 if (preg_match (
"@^" . $routePath .
".*?$@s", $path, $matches )) {
68 $result [$routePath] = $routeDetails;
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 );
89 foreach ( self::$routes as $routePath => $routeDetails ) {
90 if (! isset ( $routeDetails [
"controller"] )) {
91 $routeDetails = \reset ( $routeDetails );
93 if ($routeDetails [
"controller"] === $controllerName && $routeDetails [
"action"] === $actionName)
94 $result [$routePath] = $routeDetails;
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 );
114 return \ltrim ( $routePath,
'/' );
133 public static function path($name, $parameters = [], $absolute =
false) {
134 return self::getRouteByName ( $name, $parameters, $absolute );
146 public static function url($name, $parameters = []) {
147 return URequest::getUrl ( self::getRouteByName ( $name, $parameters,
false ) );
150 protected static function _getURL($routePath, $params) {
151 $result = \preg_replace_callback (
'~\((.*?)\)~',
function () use (&$params) {
152 return array_shift ( $params );
154 if (\
sizeof ( $params ) > 0) {
155 $result = \rtrim ( $result,
'/' ) .
'/' . \implode (
'/', $params );
161 if (! isset ( $routeDetails [
"name"] )) {
162 foreach ( $routeDetails as $methodRouteDetail ) {
163 if (isset ( $methodRouteDetail [
"name"] ) && $methodRouteDetail == $name)
167 return isset ( $routeDetails [
"name"] ) && $routeDetails [
"name"] == $name;
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"];
176 foreach ( $paramsOrder as $order ) {
177 if ($order ===
"*") {
178 if (isset ( $params [$index] ))
179 $result = \array_merge ( $result, \array_diff ( \explode (
"/", $params [$index] ), [
"" ] ) );
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] ), [
"" ] ) );
189 $result [] = self::cleanParam ( $params [$order] );
190 unset ( $params [$order] );
193 if ($cached ===
true && $cachedResponse ===
true) {
201 return \substr ( $param, 0, - 1 );
211 public static function setExpired($routePath, $expired =
true) {
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 );
230 public static function addRouteToRoutes(&$routesArray, $path, $controller, $action =
"index", $methods = null, $name =
"", $cache =
false, $duration = null, $requirements = []) {
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;
static getMethod()
Returns the http method.
static url($name, $parameters=[])
Returns the generated url from a route.
static setExpired($routePath, $expired=true)
static getRouteInfo($path)
static checkRouteName($routeDetails, $name)
static addRouteToRoutes(&$routesArray, $path, $controller, $action="index", $methods=null, $name="", $cache=false, $duration=null, $requirements=[])
static addRoute($path, $controller, $action="index", $methods=null, $name="", $cache=false, $duration=null, $requirements=[])
static _getURL($routePath, $params)
static getRouteInfoByControllerAction($controller, $action)
static getRoute($path, $cachedResponse=true)
static setExpired($routePath, $expired=true)
Déclare une route comme étant expirée ou non.
static getAnnotations($controllerName, $actionName)
static getRouteUrlParts($routeArray, $params, $cached=false, $duration=NULL, $cachedResponse=true)
static cleanParam($param)
static path($name, $parameters=[], $absolute=false)
Returns the generated path from a route.
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.
static filterRoutes($path)
This file is part of Ubiquity framework.
static getControllerCache($isRest=false)
static endswith($hay, $needle)
static startswith($hay, $needle)