@@ -38,12 +38,12 @@ discard block |
||
| 38 | 38 | * @param string $method The HTTP method for which the route must respond. |
| 39 | 39 | * @return Route |
| 40 | 40 | */ |
| 41 | - public function __construct($URLPattern, $callback = null, $method='get'){ |
|
| 42 | - $prefix = static::$prefix ? rtrim(implode('',static::$prefix),'/') : ''; |
|
| 43 | - $pattern = "/" . trim($URLPattern, "/"); |
|
| 41 | + public function __construct($URLPattern, $callback = null, $method = 'get') { |
|
| 42 | + $prefix = static::$prefix ? rtrim(implode('', static::$prefix), '/') : ''; |
|
| 43 | + $pattern = "/".trim($URLPattern, "/"); |
|
| 44 | 44 | // Adjust / optionality with dynamic patterns |
| 45 | 45 | // Ex: /test/(:a) ===> /test(/:a) |
| 46 | - $this->URLPattern = str_replace('//','/',str_replace('/(','(/', rtrim("{$prefix}{$pattern}","/"))); |
|
| 46 | + $this->URLPattern = str_replace('//', '/', str_replace('/(', '(/', rtrim("{$prefix}{$pattern}", "/"))); |
|
| 47 | 47 | |
| 48 | 48 | $this->dynamic = $this->isDynamic($this->URLPattern); |
| 49 | 49 | $this->pattern = $this->dynamic ? $this->compilePatternAsRegex($this->URLPattern, $this->rules) : $this->URLPattern; |
@@ -60,19 +60,19 @@ discard block |
||
| 60 | 60 | * @param string $method The HTTP Method to check against. |
| 61 | 61 | * @return boolean |
| 62 | 62 | */ |
| 63 | - public function match($URL,$method='get'){ |
|
| 63 | + public function match($URL, $method = 'get') { |
|
| 64 | 64 | $method = strtolower($method); |
| 65 | 65 | |
| 66 | 66 | // * is an http method wildcard |
| 67 | 67 | if (empty($this->methods[$method]) && empty($this->methods['*'])) return false; |
| 68 | - $URL = rtrim($URL,'/'); |
|
| 68 | + $URL = rtrim($URL, '/'); |
|
| 69 | 69 | $args = []; |
| 70 | - if ( $this->dynamic |
|
| 71 | - ? preg_match($this->pattern,$URL,$args) |
|
| 72 | - : $URL == rtrim($this->pattern,'/') |
|
| 73 | - ){ |
|
| 70 | + if ($this->dynamic |
|
| 71 | + ? preg_match($this->pattern, $URL, $args) |
|
| 72 | + : $URL == rtrim($this->pattern, '/') |
|
| 73 | + ) { |
|
| 74 | 74 | foreach ($args as $key => $value) { |
| 75 | - if ( false === is_string($key) ) unset($args[$key]); |
|
| 75 | + if (false === is_string($key)) unset($args[$key]); |
|
| 76 | 76 | } |
| 77 | 77 | return $args; |
| 78 | 78 | } |
@@ -85,23 +85,23 @@ discard block |
||
| 85 | 85 | * @param string $method The HTTP Method requested. |
| 86 | 86 | * @return array The callback response. |
| 87 | 87 | */ |
| 88 | - public function run(array $args, $method='get'){ |
|
| 88 | + public function run(array $args, $method = 'get') { |
|
| 89 | 89 | $method = strtolower($method); |
| 90 | - $this->response = ''; |
|
| 91 | - $this->response_object = null; |
|
| 90 | + $this->response = ''; |
|
| 91 | + $this->response_object = null; |
|
| 92 | 92 | $this->response_is_object = false; |
| 93 | 93 | |
| 94 | 94 | // Call direct befores |
| 95 | - if ( $this->befores ) { |
|
| 95 | + if ($this->befores) { |
|
| 96 | 96 | // Reverse befores order |
| 97 | 97 | foreach (array_reverse($this->befores) as $mw) { |
| 98 | 98 | ob_start(); |
| 99 | 99 | $mw_result = call_user_func($mw->bindTo($this)); |
| 100 | 100 | $this->response .= ob_get_clean(); |
| 101 | - if ( false === $mw_result ) { |
|
| 101 | + if (false === $mw_result) { |
|
| 102 | 102 | return ['']; |
| 103 | - } else if (is_a($mw_result,'View') || is_string($mw_result)) { |
|
| 104 | - $this->response .= (string)$mw_result; |
|
| 103 | + } else if (is_a($mw_result, 'View') || is_string($mw_result)) { |
|
| 104 | + $this->response .= (string) $mw_result; |
|
| 105 | 105 | } |
| 106 | 106 | } |
| 107 | 107 | } |
@@ -116,45 +116,45 @@ discard block |
||
| 116 | 116 | Response::type(Response::TYPE_HTML); |
| 117 | 117 | ob_start(); |
| 118 | 118 | // Silence "Cannot bind an instance to a static closure" warnings |
| 119 | - $view_results = call_user_func_array(@$callback->bindTo($this), $args); |
|
| 119 | + $view_results = call_user_func_array(@$callback->bindTo($this), $args); |
|
| 120 | 120 | $this->response .= ob_get_clean(); |
| 121 | 121 | |
| 122 | 122 | // Render View if returned, else echo string or encode json response |
| 123 | - if ( null !== $view_results ) { |
|
| 124 | - if (is_a($view_results,'View') || is_string($view_results)) { |
|
| 125 | - $this->response .= (string)$view_results; |
|
| 123 | + if (null !== $view_results) { |
|
| 124 | + if (is_a($view_results, 'View') || is_string($view_results)) { |
|
| 125 | + $this->response .= (string) $view_results; |
|
| 126 | 126 | } else { |
| 127 | 127 | $this->response_is_object = true; |
| 128 | - $this->response_object = $view_results; |
|
| 128 | + $this->response_object = $view_results; |
|
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - } else if (is_a($callback,'View') || is_string($callback)) { |
|
| 132 | + } else if (is_a($callback, 'View') || is_string($callback)) { |
|
| 133 | 133 | // return rendered View or direct string |
| 134 | - $this->response .= (string)$callback; |
|
| 134 | + $this->response .= (string) $callback; |
|
| 135 | 135 | } else { |
| 136 | 136 | // JSON encode returned value |
| 137 | 137 | $this->response_is_object = true; |
| 138 | - $this->response_object = $callback; |
|
| 138 | + $this->response_object = $callback; |
|
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | // Apply afters |
| 142 | - if ( $this->afters ) { |
|
| 142 | + if ($this->afters) { |
|
| 143 | 143 | foreach ($this->afters as $mw) { |
| 144 | 144 | ob_start(); |
| 145 | 145 | $mw_result = call_user_func($mw->bindTo($this)); |
| 146 | 146 | $this->response .= ob_get_clean(); |
| 147 | - if ( false === $mw_result ) { |
|
| 147 | + if (false === $mw_result) { |
|
| 148 | 148 | return ['']; |
| 149 | - } else if (is_a($mw_result,'View') || is_string($mw_result)) { |
|
| 150 | - $this->response .= (string)$mw_result; |
|
| 149 | + } else if (is_a($mw_result, 'View') || is_string($mw_result)) { |
|
| 150 | + $this->response .= (string) $mw_result; |
|
| 151 | 151 | } |
| 152 | 152 | } |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | Event::trigger('core.route.after', $this); |
| 156 | 156 | |
| 157 | - if ( $this->response_is_object ){ |
|
| 157 | + if ($this->response_is_object) { |
|
| 158 | 158 | $this->response = Response::json($this->response_object); |
| 159 | 159 | } else { |
| 160 | 160 | Response::add($this->response); |
@@ -171,8 +171,8 @@ discard block |
||
| 171 | 171 | * @param string $method The HTTP Method to check against. |
| 172 | 172 | * @return array The callback response. |
| 173 | 173 | */ |
| 174 | - public function runIfMatch($URL, $method='get'){ |
|
| 175 | - return ($args = $this->match($URL,$method)) ? $this->run($args,$method) : null; |
|
| 174 | + public function runIfMatch($URL, $method = 'get') { |
|
| 175 | + return ($args = $this->match($URL, $method)) ? $this->run($args, $method) : null; |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
@@ -181,8 +181,8 @@ discard block |
||
| 181 | 181 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 182 | 182 | * @return Route |
| 183 | 183 | */ |
| 184 | - public static function on($URLPattern, $callback = null){ |
|
| 185 | - return new Route($URLPattern,$callback); |
|
| 184 | + public static function on($URLPattern, $callback = null) { |
|
| 185 | + return new Route($URLPattern, $callback); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /** |
@@ -191,8 +191,8 @@ discard block |
||
| 191 | 191 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 192 | 192 | * @return Route |
| 193 | 193 | */ |
| 194 | - public static function get($URLPattern, $callback = null){ |
|
| 195 | - return (new Route($URLPattern,$callback))->via('get'); |
|
| 194 | + public static function get($URLPattern, $callback = null) { |
|
| 195 | + return (new Route($URLPattern, $callback))->via('get'); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -201,8 +201,8 @@ discard block |
||
| 201 | 201 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 202 | 202 | * @return Route |
| 203 | 203 | */ |
| 204 | - public static function post($URLPattern, $callback = null){ |
|
| 205 | - return (new Route($URLPattern,$callback))->via('post'); |
|
| 204 | + public static function post($URLPattern, $callback = null) { |
|
| 205 | + return (new Route($URLPattern, $callback))->via('post'); |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | /** |
@@ -211,8 +211,8 @@ discard block |
||
| 211 | 211 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 212 | 212 | * @return Route |
| 213 | 213 | */ |
| 214 | - public static function any($URLPattern, $callback = null){ |
|
| 215 | - return (new Route($URLPattern,$callback))->via('*'); |
|
| 214 | + public static function any($URLPattern, $callback = null) { |
|
| 215 | + return (new Route($URLPattern, $callback))->via('*'); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | /** |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | */ |
| 258 | 258 | public function & via(){ |
| 259 | 259 | $this->methods = []; |
| 260 | - foreach (func_get_args() as $method){ |
|
| 260 | + foreach (func_get_args() as $method) { |
|
| 261 | 261 | $this->methods[strtolower($method)] = true; |
| 262 | 262 | } |
| 263 | 263 | return $this; |
@@ -279,10 +279,10 @@ discard block |
||
| 279 | 279 | * @return Route |
| 280 | 280 | */ |
| 281 | 281 | public function & rules(array $rules){ |
| 282 | - foreach ((array)$rules as $varname => $rule){ |
|
| 282 | + foreach ((array) $rules as $varname => $rule) { |
|
| 283 | 283 | $this->rules[$varname] = $rule; |
| 284 | 284 | } |
| 285 | - $this->pattern = $this->compilePatternAsRegex($this->URLPattern,$this->rules); |
|
| 285 | + $this->pattern = $this->compilePatternAsRegex($this->URLPattern, $this->rules); |
|
| 286 | 286 | return $this; |
| 287 | 287 | } |
| 288 | 288 | |
@@ -320,10 +320,10 @@ discard block |
||
| 320 | 320 | * @param string $pattern The URL schema. |
| 321 | 321 | * @return string The compiled PREG RegEx. |
| 322 | 322 | */ |
| 323 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
| 324 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
| 325 | - return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
|
| 326 | - },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
|
| 323 | + protected static function compilePatternAsRegex($pattern, $rules = []) { |
|
| 324 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S', function($g) use (&$rules){ |
|
| 325 | + return '(?<'.$g[1].'>'.(isset($rules[$g[1]]) ? $rules[$g[1]] : '[^/]+').')'; |
|
| 326 | + },str_replace(['.', ')', '*'], ['\.', ')?', '.+'], $pattern)).'$#'; |
|
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | /** |
@@ -333,10 +333,10 @@ discard block |
||
| 333 | 333 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
| 334 | 334 | * @return array The extracted variables |
| 335 | 335 | */ |
| 336 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
| 336 | + protected static function extractVariablesFromURL($pattern, $URL = null, $cut = false) { |
|
| 337 | 337 | $URL = $URL ?: Request::URI(); |
| 338 | - $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
|
| 339 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
| 338 | + $pattern = $cut ? str_replace('$#', '', $pattern).'#' : $pattern; |
|
| 339 | + if (!preg_match($pattern, $URL, $args)) return false; |
|
| 340 | 340 | foreach ($args as $key => $value) { |
| 341 | 341 | if (false === is_string($key)) unset($args[$key]); |
| 342 | 342 | } |
@@ -348,8 +348,8 @@ discard block |
||
| 348 | 348 | * @param string $pattern The URL schema. |
| 349 | 349 | * @return boolean |
| 350 | 350 | */ |
| 351 | - protected static function isDynamic($pattern){ |
|
| 352 | - return strlen($pattern) != strcspn($pattern,':(?[*+'); |
|
| 351 | + protected static function isDynamic($pattern) { |
|
| 352 | + return strlen($pattern) != strcspn($pattern, ':(?[*+'); |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | /** |
@@ -357,9 +357,9 @@ discard block |
||
| 357 | 357 | * @param Route $r |
| 358 | 358 | * @return Route |
| 359 | 359 | */ |
| 360 | - public static function add($r){ |
|
| 361 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
| 362 | - return static::$routes[implode('',static::$prefix)][] = $r; |
|
| 360 | + public static function add($r) { |
|
| 361 | + if (isset(static::$group[0])) static::$group[0]->add($r); |
|
| 362 | + return static::$routes[implode('', static::$prefix)][] = $r; |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
@@ -367,16 +367,16 @@ discard block |
||
| 367 | 367 | * @param string $prefix The url prefix for the internal route definitions. |
| 368 | 368 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
| 369 | 369 | */ |
| 370 | - public static function group($prefix,$callback=null){ |
|
| 370 | + public static function group($prefix, $callback = null) { |
|
| 371 | 371 | |
| 372 | 372 | // Skip definition if current request doesn't match group. |
| 373 | 373 | |
| 374 | - $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
|
| 374 | + $prefix_complete = rtrim(implode('', static::$prefix), '/').$prefix; |
|
| 375 | 375 | |
| 376 | - if ( static::isDynamic($prefix) ){ |
|
| 376 | + if (static::isDynamic($prefix)) { |
|
| 377 | 377 | |
| 378 | 378 | // Dynamic group, capture vars |
| 379 | - $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete),null,true); |
|
| 379 | + $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete), null, true); |
|
| 380 | 380 | |
| 381 | 381 | // Errors in compile pattern or variable extraction, aborting. |
| 382 | 382 | if (false === $vars) return; |
@@ -384,15 +384,15 @@ discard block |
||
| 384 | 384 | static::$prefix[] = $prefix; |
| 385 | 385 | if (empty(static::$group)) static::$group = []; |
| 386 | 386 | array_unshift(static::$group, new RouteGroup()); |
| 387 | - if ($callback) call_user_func_array($callback,$vars); |
|
| 387 | + if ($callback) call_user_func_array($callback, $vars); |
|
| 388 | 388 | $group = static::$group[0]; |
| 389 | 389 | array_shift(static::$group); |
| 390 | 390 | array_pop(static::$prefix); |
| 391 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
| 391 | + if (empty(static::$prefix)) static::$prefix = ['']; |
|
| 392 | 392 | |
| 393 | 393 | return $group; |
| 394 | 394 | |
| 395 | - } else if ( 0 === strpos(Request::URI(), $prefix_complete) ){ |
|
| 395 | + } else if (0 === strpos(Request::URI(), $prefix_complete)) { |
|
| 396 | 396 | |
| 397 | 397 | // Static group |
| 398 | 398 | static::$prefix[] = $prefix; |
@@ -402,7 +402,7 @@ discard block |
||
| 402 | 402 | $group = static::$group[0]; |
| 403 | 403 | array_shift(static::$group); |
| 404 | 404 | array_pop(static::$prefix); |
| 405 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
| 405 | + if (empty(static::$prefix)) static::$prefix = ['']; |
|
| 406 | 406 | |
| 407 | 407 | return $group; |
| 408 | 408 | } else { |
@@ -413,8 +413,8 @@ discard block |
||
| 413 | 413 | |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | - public static function exitWithError($code,$message="Application Error"){ |
|
| 417 | - Response::error($code,$message); |
|
| 416 | + public static function exitWithError($code, $message = "Application Error") { |
|
| 417 | + Response::error($code, $message); |
|
| 418 | 418 | Response::send(); |
| 419 | 419 | exit; |
| 420 | 420 | } |
@@ -424,20 +424,20 @@ discard block |
||
| 424 | 424 | * @param string $URL The URL to match onto. |
| 425 | 425 | * @return boolean true if a route callback was executed. |
| 426 | 426 | */ |
| 427 | - public static function dispatch($URL=null,$method=null){ |
|
| 427 | + public static function dispatch($URL = null, $method = null) { |
|
| 428 | 428 | if (!$URL) $URL = Request::URI(); |
| 429 | 429 | if (!$method) $method = Request::method(); |
| 430 | 430 | |
| 431 | - $__deferred_send = new Deferred(function(){ |
|
| 432 | - if (Options::get('core.response.autosend',true)){ |
|
| 431 | + $__deferred_send = new Deferred(function() { |
|
| 432 | + if (Options::get('core.response.autosend', true)) { |
|
| 433 | 433 | Response::send(); |
| 434 | 434 | } |
| 435 | 435 | }); |
| 436 | 436 | |
| 437 | - foreach ((array)static::$routes as $group => $routes){ |
|
| 437 | + foreach ((array) static::$routes as $group => $routes) { |
|
| 438 | 438 | foreach ($routes as $route) { |
| 439 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
| 440 | - $route->run($args,$method); |
|
| 439 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL, $method))) { |
|
| 440 | + $route->run($args, $method); |
|
| 441 | 441 | return true; |
| 442 | 442 | } |
| 443 | 443 | } |
@@ -452,34 +452,34 @@ discard block |
||
| 452 | 452 | class RouteGroup { |
| 453 | 453 | protected $routes; |
| 454 | 454 | |
| 455 | - public function __construct(){ |
|
| 455 | + public function __construct() { |
|
| 456 | 456 | $this->routes = new SplObjectStorage; |
| 457 | 457 | return Route::add($this); |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | - public function has($r){ |
|
| 460 | + public function has($r) { |
|
| 461 | 461 | return $this->routes->contains($r); |
| 462 | 462 | } |
| 463 | 463 | |
| 464 | - public function add($r){ |
|
| 464 | + public function add($r) { |
|
| 465 | 465 | $this->routes->attach($r); |
| 466 | 466 | return $this; |
| 467 | 467 | } |
| 468 | 468 | |
| 469 | - public function remove($r){ |
|
| 469 | + public function remove($r) { |
|
| 470 | 470 | if ($this->routes->contains($r)) $this->routes->detach($r); |
| 471 | 471 | return $this; |
| 472 | 472 | } |
| 473 | 473 | |
| 474 | - public function before($callbacks){ |
|
| 475 | - foreach ($this->routes as $route){ |
|
| 474 | + public function before($callbacks) { |
|
| 475 | + foreach ($this->routes as $route) { |
|
| 476 | 476 | $route->before($callbacks); |
| 477 | 477 | } |
| 478 | 478 | return $this; |
| 479 | 479 | } |
| 480 | 480 | |
| 481 | - public function after($callbacks){ |
|
| 482 | - foreach ($this->routes as $route){ |
|
| 481 | + public function after($callbacks) { |
|
| 482 | + foreach ($this->routes as $route) { |
|
| 483 | 483 | $route->after($callbacks); |
| 484 | 484 | } |
| 485 | 485 | return $this; |