Skip to content

Commit 5a9d73f

Browse files
committed
[skip ci][router] add setStatusCode
1 parent 0ee03e6 commit 5a9d73f

File tree

1 file changed

+86
-76
lines changed

1 file changed

+86
-76
lines changed

src/Ubiquity/controllers/Router.php

Lines changed: 86 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,93 +3,94 @@
33
namespace Ubiquity\controllers;
44

55
use Ubiquity\cache\CacheManager;
6+
use Ubiquity\controllers\router\RouterStatus;
67
use Ubiquity\controllers\traits\RouterAdminTrait;
78
use Ubiquity\controllers\traits\RouterModifierTrait;
89
use Ubiquity\controllers\traits\RouterTestTrait;
910
use Ubiquity\log\Logger;
1011
use Ubiquity\utils\http\URequest;
11-
use Ubiquity\controllers\router\RouterStatus;
1212

1313
/**
1414
* Router manager.
1515
* Ubiquity\controllers$Router
1616
* This class is part of Ubiquity
1717
*
1818
* @author jcheron <[email protected]>
19-
* @version 1.1.0
19+
* @version 1.1.1
2020
*
2121
*/
2222
class Router {
23-
use RouterModifierTrait,RouterAdminTrait,RouterTestTrait;
23+
use RouterModifierTrait, RouterAdminTrait, RouterTestTrait;
24+
2425
protected static $routes;
2526
protected static $statusCode;
2627

2728
private static function cleanParam(string $param): string {
28-
if (\substr ( $param, - 1 ) === '/') {
29-
return \substr ( $param, 0, - 1 );
29+
if (\substr($param, -1) === '/') {
30+
return \substr($param, 0, -1);
3031
}
3132
return $param;
3233
}
3334

3435
private static function getRoute_(&$routeDetails, $routePath, $matches, $cachedResponse) {
35-
self::$statusCode=RouterStatus::OK;
36-
if (! isset ( $routeDetails ['controller'] )) {
37-
$method = \strtolower ( $_SERVER ['REQUEST_METHOD'] );
38-
if (isset ( $routeDetails [$method] )) {
36+
self::$statusCode = RouterStatus::OK;
37+
if (!isset ($routeDetails ['controller'])) {
38+
$method = \strtolower($_SERVER ['REQUEST_METHOD']);
39+
if (isset ($routeDetails [$method])) {
3940
$routeDetailsMethod = $routeDetails [$method];
40-
return self::getRouteUrlParts ( [ 'path' => $routePath,'details' => $routeDetailsMethod ], $matches, $routeDetailsMethod ['cache'] ?? false, $routeDetailsMethod ['duration'] ?? null, $cachedResponse );
41+
return self::getRouteUrlParts(['path' => $routePath, 'details' => $routeDetailsMethod], $matches, $routeDetailsMethod ['cache'] ?? false, $routeDetailsMethod ['duration'] ?? null, $cachedResponse);
4142
}
42-
self::$statusCode=RouterStatus::METHOD_NOT_ALLOWED;
43+
self::$statusCode = RouterStatus::METHOD_NOT_ALLOWED;
4344
} else {
44-
return self::getRouteUrlParts ( [ 'path' => $routePath,'details' => $routeDetails ], $matches, $routeDetails ['cache'] ?? false, $routeDetails ['duration'] ?? null, $cachedResponse );
45+
return self::getRouteUrlParts(['path' => $routePath, 'details' => $routeDetails], $matches, $routeDetails ['cache'] ?? false, $routeDetails ['duration'] ?? null, $cachedResponse);
4546
}
46-
if(self::$statusCode===RouterStatus::OK) {
47+
if (self::$statusCode === RouterStatus::OK) {
4748
self::$statusCode = RouterStatus::NOT_FOUND;
4849
}
4950
return false;
5051
}
5152

5253
protected static function _getURL($routePath, $params) {
53-
$result = \preg_replace_callback ( '~\((.*?)\)~', function () use (&$params) {
54-
return \array_shift ( $params );
55-
}, $routePath );
56-
if (\count ( $params ) > 0) {
57-
$result = \rtrim ( $result, '/' ) . '/' . \implode ( '/', $params );
54+
$result = \preg_replace_callback('~\((.*?)\)~', function () use (&$params) {
55+
return \array_shift($params);
56+
}, $routePath);
57+
if (\count($params) > 0) {
58+
$result = \rtrim($result, '/') . '/' . \implode('/', $params);
5859
}
5960
return $result;
6061
}
6162

6263
protected static function checkRouteName($routeDetails, $name) {
63-
if (! isset ( $routeDetails ['name'] )) {
64-
foreach ( $routeDetails as $methodRouteDetail ) {
65-
if (isset ( $methodRouteDetail ['name'] ) && $methodRouteDetail ['name'] == $name) {
64+
if (!isset ($routeDetails ['name'])) {
65+
foreach ($routeDetails as $methodRouteDetail) {
66+
if (isset ($methodRouteDetail ['name']) && $methodRouteDetail ['name'] == $name) {
6667
return true;
6768
}
6869
}
6970
}
70-
return isset ( $routeDetails ['name'] ) && $routeDetails ['name'] == $name;
71+
return isset ($routeDetails ['name']) && $routeDetails ['name'] == $name;
7172
}
7273

7374
protected static function setParamsInOrder($paramsOrder, $params) {
7475
$index = 0;
75-
$newParams=[];
76-
foreach ( $paramsOrder as $order ) {
76+
$newParams = [];
77+
foreach ($paramsOrder as $order) {
7778
if ($order === '*') {
78-
if (isset ( $params [$index] )) {
79-
$newParams = \array_merge ( $newParams, \array_diff ( \explode ( '/', $params [$index] ), [ '' ] ) );
79+
if (isset ($params [$index])) {
80+
$newParams = \array_merge($newParams, \array_diff(\explode('/', $params [$index]), ['']));
8081
}
8182
break;
8283
}
8384
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] ), [ '' ] ) );
85+
$order = \intval(\substr($order, 1, 1));
86+
if (isset ($params [$order])) {
87+
$newParams = \array_merge($newParams, \array_diff(\explode('/', $params [$order]), ['']));
8788
break;
8889
}
8990
}
90-
$newParams [] = self::cleanParam ( $params [$order] );
91-
unset ( $params [$order] );
92-
$index ++;
91+
$newParams [] = self::cleanParam($params [$order]);
92+
unset ($params [$order]);
93+
$index++;
9394
}
9495
return $newParams;
9596
}
@@ -98,21 +99,21 @@ protected static function setParamsInOrder($paramsOrder, $params) {
9899
* Starts the router by loading normal routes (not rest).
99100
*/
100101
public static function start(): void {
101-
self::$routes = CacheManager::getControllerCache ();
102+
self::$routes = CacheManager::getControllerCache();
102103
}
103104

104105
/**
105106
* Starts the router by loading rest routes (not normal routes).
106107
*/
107108
public static function startRest(): void {
108-
self::$routes = CacheManager::getControllerCache ( true );
109+
self::$routes = CacheManager::getControllerCache(true);
109110
}
110111

111112
/**
112113
* Starts the router by loading all routes (normal + rest routes).
113114
*/
114115
public static function startAll(): void {
115-
self::$routes = \array_merge ( CacheManager::getControllerCache (), CacheManager::getControllerCache ( true ) );
116+
self::$routes = \array_merge(CacheManager::getControllerCache(), CacheManager::getControllerCache(true));
116117
}
117118

118119
/**
@@ -123,13 +124,13 @@ public static function startAll(): void {
123124
* @return boolean|mixed[]|string
124125
*/
125126
public static function getRoute($path, $cachedResponse = true, $debug = false) {
126-
$path = self::slashPath ( $path );
127-
if (isset ( self::$routes [$path] ) && ! $debug) { // No direct access to route in debug mode (for maintenance mode activation)
128-
return self::getRoute_ ( self::$routes [$path], $path, [ $path ], $cachedResponse );
127+
$path = self::slashPath($path);
128+
if (isset (self::$routes [$path]) && !$debug) { // No direct access to route in debug mode (for maintenance mode activation)
129+
return self::getRoute_(self::$routes [$path], $path, [$path], $cachedResponse);
129130
}
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) {
131+
foreach (self::$routes as $routePath => $routeDetails) {
132+
if (\preg_match("@^{$routePath}\$@s", $path, $matches)) {
133+
if (($r = self::getRoute_($routeDetails, $routePath, $matches, $cachedResponse)) !== false) {
133134
return $r;
134135
}
135136
}
@@ -145,24 +146,24 @@ public static function getRoute($path, $cachedResponse = true, $debug = false) {
145146
* @param boolean $absolute
146147
*/
147148
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)?'/':'';
149+
foreach (self::$routes as $routePath => $routeDetails) {
150+
if (self::checkRouteName($routeDetails, $name)) {
151+
if (\trim($routePath, '/') == '_default') {
152+
return ($absolute) ? '/' : '';
152153
}
153-
if (\count ( $parameters ) > 0) {
154-
$routePath = self::_getURL ( $routePath, $parameters );
154+
if (\count($parameters) > 0) {
155+
$routePath = self::_getURL($routePath, $parameters);
155156
}
156-
$routePath = \str_replace('//', '/',\preg_replace('~\((.*?)\)~', '', $routePath));
157-
return ($absolute)?$routePath:\ltrim ( $routePath, '/' );
157+
$routePath = \str_replace('//', '/', \preg_replace('~\((.*?)\)~', '', $routePath));
158+
return ($absolute) ? $routePath : \ltrim($routePath, '/');
158159
}
159160
}
160161
return false;
161162
}
162163

163164
public static function getRouteInfoByName($name) {
164-
foreach ( self::$routes as $routeDetails ) {
165-
if (self::checkRouteName ( $routeDetails, $name )) {
165+
foreach (self::$routes as $routeDetails) {
166+
if (self::checkRouteName($routeDetails, $name)) {
166167
return $routeDetails;
167168
}
168169
}
@@ -178,7 +179,7 @@ public static function getRouteInfoByName($name) {
178179
* @return boolean|string|array|mixed the generated path (/path/to/route)
179180
*/
180181
public static function path($name, $parameters = [], $absolute = false) {
181-
return self::getRouteByName ( $name, $parameters, $absolute );
182+
return self::getRouteByName($name, $parameters, $absolute);
182183
}
183184

184185
/**
@@ -189,42 +190,42 @@ public static function path($name, $parameters = [], $absolute = false) {
189190
* @return string the generated url (http://myApp/path/to/route)
190191
*/
191192
public static function url($name, $parameters = []): string {
192-
return URequest::getUrl ( self::getRouteByName ( $name, $parameters, false ) );
193+
return URequest::getUrl(self::getRouteByName($name, $parameters, false));
193194
}
194195

195-
public static function getRouteUrlParts($routeArray, $params, $cached = false, $duration = NULL, $cachedResponse = true) {
196-
$realPath = \current ( $params );
197-
\array_shift ( $params );
196+
public static function getRouteUrlParts($routeArray, $params, $cached = false, $duration = null, $cachedResponse = true) {
197+
$realPath = \current($params);
198+
\array_shift($params);
198199
$routeDetails = $routeArray ['details'];
199200
if ($routeDetails ['controller'] instanceof \Closure) {
200-
$result = [ 'callback'=>$routeDetails ['controller'] ];
201+
$result = ['callback' => $routeDetails ['controller']];
201202
$resultStr = 'callable function';
202203
} else {
203-
$mainParams=null;
204-
if(($mainMethodParams=$routeDetails['main.params']??null)!==null){
205-
foreach ($mainMethodParams as $index=>$mainMethodParam) {
206-
$mainParams[$mainMethodParam]=$params[$index];
204+
$mainParams = null;
205+
if (($mainMethodParams = $routeDetails['main.params'] ?? null) !== null) {
206+
foreach ($mainMethodParams as $index => $mainMethodParam) {
207+
$mainParams[$mainMethodParam] = $params[$index];
207208
}
208-
$params=\array_slice ( $params, $index+1);
209+
$params = \array_slice($params, $index + 1);
209210
}
210-
$result = [ 'controller'=>\str_replace ( "\\\\", "\\", $routeDetails ['controller'] ),'action'=>$routeDetails ['action'],'mainParams'=>$mainParams];
211+
$result = ['controller' => \str_replace("\\\\", "\\", $routeDetails ['controller']), 'action' => $routeDetails ['action'], 'mainParams' => $mainParams];
211212
$resultStr = \json_encode($result);
212213
}
213-
if (($paramsOrder = $routeDetails ['parameters']) && (\count ( $paramsOrder ) > 0)) {
214-
$result['params']=self::setParamsInOrder ( $paramsOrder, $params );
214+
if (($paramsOrder = $routeDetails ['parameters']) && (\count($paramsOrder) > 0)) {
215+
$result['params'] = self::setParamsInOrder($paramsOrder, $params);
215216
}
216-
if (! $cached || ! $cachedResponse) {
217-
Logger::info ( 'Router', \sprintf ( 'Route found for %s : %s', $routeArray ['path'], $resultStr ), 'getRouteUrlParts' );
218-
if (isset ( $routeDetails ['callback'] )) {
217+
if (!$cached || !$cachedResponse) {
218+
Logger::info('Router', \sprintf('Route found for %s : %s', $routeArray ['path'], $resultStr), 'getRouteUrlParts');
219+
if (isset ($routeDetails ['callback'])) {
219220
// Used for maintenance mode
220221
if ($routeDetails ['callback'] instanceof \Closure) {
221-
return $routeDetails ['callback'] ( $result );
222+
return $routeDetails ['callback'] ($result);
222223
}
223224
}
224225
return $result;
225226
}
226-
Logger::info ( 'Router', sprintf ( 'Route found for %s (from cache) : %s', $realPath, $resultStr ), 'getRouteUrlParts' );
227-
return CacheManager::getRouteCache ( $realPath, $result, $duration );
227+
Logger::info('Router', sprintf('Route found for %s (from cache) : %s', $realPath, $resultStr), 'getRouteUrlParts');
228+
return CacheManager::getRouteCache($realPath, $result, $duration);
228229
}
229230

230231
/**
@@ -234,10 +235,10 @@ public static function getRouteUrlParts($routeArray, $params, $cached = false, $
234235
* @return string The path with slashes
235236
*/
236237
public static function slashPath($path): string {
237-
if (\substr ( $path, 0, 1 ) !== '/') {
238+
if (\substr($path, 0, 1) !== '/') {
238239
$path = '/' . $path;
239240
}
240-
if (\substr ( $path, - 1 ) !== '/') {
241+
if (\substr($path, -1) !== '/') {
241242
$path = $path . '/';
242243
}
243244
return $path;
@@ -249,7 +250,7 @@ public static function slashPath($path): string {
249250
* @param string $routePath
250251
*/
251252
public static function setExpired($routePath): void {
252-
CacheManager::setExpired ( $routePath );
253+
CacheManager::setExpired($routePath);
253254
}
254255

255256
/**
@@ -260,13 +261,22 @@ public static function setExpired($routePath): void {
260261
public static function getRoutes() {
261262
return self::$routes;
262263
}
263-
264+
264265
/**
265266
* Return router response status code.
266267
* @return int
267268
* @since 2.4.5
268269
*/
269-
public static function getStatusCode():int{
270+
public static function getStatusCode(): int {
270271
return self::$statusCode;
271272
}
273+
274+
/**
275+
* Set router response status code.
276+
* @param mixed $statusCode
277+
* @since 2.5.2
278+
*/
279+
public static function setStatusCode($statusCode): void {
280+
self::$statusCode = $statusCode;
281+
}
272282
}

0 commit comments

Comments
 (0)