27 private static function cleanParam(
string $param): string {
28 if (\substr($param, -1) ===
'/') {
29 return \substr($param, 0, -1);
34 private static function getRoute_(&$routeDetails, $routePath, $matches, $cachedResponse) {
36 if (!isset ($routeDetails [
'controller'])) {
37 $method = \strtolower($_SERVER [
'REQUEST_METHOD']);
38 if (isset ($routeDetails [$method])) {
39 $routeDetailsMethod = $routeDetails [$method];
40 return self::getRouteUrlParts([
'path' => $routePath,
'details' => $routeDetailsMethod], $matches, $routeDetailsMethod [
'cache'] ??
false, $routeDetailsMethod [
'duration'] ??
null, $cachedResponse);
44 return self::getRouteUrlParts([
'path' => $routePath,
'details' => $routeDetails], $matches, $routeDetails [
'cache'] ??
false, $routeDetails [
'duration'] ??
null, $cachedResponse);
52 protected static function _getURL($routePath, $params) {
53 $result = \preg_replace_callback(
'~\((.*?)\)~',
function () use (&$params) {
54 return \array_shift($params);
56 if (\count($params) > 0) {
57 $result = \rtrim($result,
'/') .
'/' . \implode(
'/', $params);
63 if (!isset ($routeDetails [
'name'])) {
64 foreach ($routeDetails as $methodRouteDetail) {
65 if (isset ($methodRouteDetail [
'name']) && $methodRouteDetail [
'name'] == $name) {
70 return isset ($routeDetails [
'name']) && $routeDetails [
'name'] == $name;
76 foreach ($paramsOrder as $order) {
78 if (isset ($params [$index])) {
79 $newParams = \array_merge($newParams, \array_diff(\explode(
'/', $params [$index]), [
'']));
83 if (($order [0] ??
'') ===
'~') {
84 $order = \intval(\substr($order, 1, 1));
85 if (isset ($params [$order])) {
86 $newParams = \array_merge($newParams, \array_diff(\explode(
'/', $params [$order]), [
'']));
90 $newParams [] = self::cleanParam($params [$order]);
91 unset ($params [$order]);
100 public static function start(): void {
125 public static function getRoute($path, $cachedResponse =
true, $debug =
false) {
126 $path = self::slashPath($path);
127 if (isset (self::$routes [$path]) && !$debug) {
128 return self::getRoute_(self::$routes [$path], $path, [$path], $cachedResponse);
130 foreach (self::$routes as $routePath => $routeDetails) {
131 if (\preg_match(
"@^{$routePath}\$@s", $path, $matches)) {
132 if (($r = self::getRoute_($routeDetails, $routePath, $matches, $cachedResponse)) !==
false) {
147 public static function getRouteByName($name, $parameters = [], $absolute =
true) {
148 foreach (self::$routes as $routePath => $routeDetails) {
149 if (self::checkRouteName($routeDetails, $name)) {
150 if (\trim($routePath,
'/') ==
'_default') {
151 return ($absolute) ?
'/' :
'';
153 if (\count($parameters) > 0) {
154 $routePath = self::_getURL($routePath, $parameters);
156 $routePath = \str_replace(
'//',
'/', \preg_replace(
'~\((.*?)\)~',
'', $routePath));
157 return ($absolute) ? $routePath : \ltrim($routePath,
'/');
164 foreach (self::$routes as $routeDetails) {
165 if (self::checkRouteName($routeDetails, $name)) {
166 return $routeDetails;
180 public static function path($name, $parameters = [], $absolute =
false) {
181 return self::getRouteByName($name, $parameters, $absolute);
191 public static function url($name, $parameters = []): string {
192 return
URequest::getUrl(self::getRouteByName($name, $parameters, false));
195 public static function getRouteUrlParts($routeArray, $params, $cached =
false, $duration =
null, $cachedResponse =
true) {
196 $realPath = \current($params);
197 \array_shift($params);
198 $routeDetails = $routeArray [
'details'];
199 if ($routeDetails [
'controller'] instanceof \Closure) {
200 $result = [
'callback' => $routeDetails [
'controller']];
201 $resultStr =
'callable function';
204 if (($mainMethodParams = $routeDetails[
'main.params'] ??
null) !==
null) {
205 foreach ($mainMethodParams as $index => $mainMethodParam) {
206 $mainParams[$mainMethodParam] = $params[$index];
208 $params = \array_slice($params, $index + 1);
210 $result = [
'controller' => \str_replace(
"\\\\",
"\\", $routeDetails [
'controller']),
'action' => $routeDetails [
'action'],
'mainParams' => $mainParams];
211 $resultStr = \json_encode($result);
213 if (($paramsOrder = $routeDetails [
'parameters']) && (\count($paramsOrder) > 0)) {
214 $result[
'params'] = self::setParamsInOrder($paramsOrder, $params);
216 if (!$cached || !$cachedResponse) {
217 Logger::info(
'Router', \sprintf(
'Route found for %s : %s', $routeArray [
'path'], $resultStr),
'getRouteUrlParts');
218 if (isset ($routeDetails [
'callback'])) {
220 if ($routeDetails [
'callback'] instanceof \Closure) {
221 return $routeDetails [
'callback'] ($result);
226 Logger::info(
'Router', sprintf(
'Route found for %s (from cache) : %s', $realPath, $resultStr),
'getRouteUrlParts');
227 return CacheManager::getRouteCache($realPath, $result, $duration);
237 if (\substr($path, 0, 1) !==
'/') {
240 if (\substr($path, -1) !==
'/') {
261 return self::$routes;
270 return self::$statusCode;
279 self::$statusCode = $statusCode;
Manager for caches (Router, Rest, models).
static setStatusCode($statusCode)
Set router response status code.
static start()
Starts the router by loading normal routes (not rest).
static _getURL($routePath, $params)
static slashPath($path)
Adds a slash before and after a path.
static getRoute($path, $cachedResponse=true, $debug=false)
Returns the route corresponding to a path.
static getRouteInfoByName($name)
static getRouteUrlParts($routeArray, $params, $cached=false, $duration=null, $cachedResponse=true)
static path($name, $parameters=[], $absolute=false)
Returns the generated path from a route.
static getRoutes()
Returns the array of loaded routes.
static getRouteByName($name, $parameters=[], $absolute=true)
Returns the generated path from a route.
static cleanParam(string $param)
static checkRouteName($routeDetails, $name)
static startAll()
Starts the router by loading all routes (normal + rest routes).
static url($name, $parameters=[])
Returns the generated url from a route.
static getStatusCode()
Return router response status code.
static setParamsInOrder($paramsOrder, $params)
static setExpired($routePath)
Declares a route as expired.
static startRest()
Starts the router by loading rest routes (not normal routes).
static getRoute_(&$routeDetails, $routePath, $matches, $cachedResponse)
Ubiquity\controllers\router$RouterStatus This class is part of Ubiquity.
Ubiquity\controllers\traits$RouterModifierTrait This class is part of Ubiquity.
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.