@@ -12,12 +12,12 @@ discard block |
||
12 | 12 | |
13 | 13 | class Deferred { |
14 | 14 | |
15 | - protected $callback, |
|
15 | + protected $callback, |
|
16 | 16 | $enabled = true; |
17 | 17 | |
18 | - public function __construct( callable $callback ) { |
|
19 | - $this->callback = $callback; |
|
20 | - } |
|
18 | + public function __construct( callable $callback ) { |
|
19 | + $this->callback = $callback; |
|
20 | + } |
|
21 | 21 | |
22 | 22 | public function disarm() { |
23 | 23 | $this->enabled = false; |
@@ -27,8 +27,8 @@ discard block |
||
27 | 27 | $this->enabled = true; |
28 | 28 | } |
29 | 29 | |
30 | - public function __destruct() { |
|
31 | - if ( $this->enabled ) call_user_func( $this->callback ); |
|
32 | - } |
|
30 | + public function __destruct() { |
|
31 | + if ( $this->enabled ) call_user_func( $this->callback ); |
|
32 | + } |
|
33 | 33 | |
34 | 34 | } |
35 | 35 | \ No newline at end of file |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | protected $callback, |
16 | 16 | $enabled = true; |
17 | 17 | |
18 | - public function __construct( callable $callback ) { |
|
18 | + public function __construct(callable $callback) { |
|
19 | 19 | $this->callback = $callback; |
20 | 20 | } |
21 | 21 | |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | } |
29 | 29 | |
30 | 30 | public function __destruct() { |
31 | - if ( $this->enabled ) call_user_func( $this->callback ); |
|
31 | + if ($this->enabled) call_user_func($this->callback); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | } |
35 | 35 | \ No newline at end of file |
@@ -28,7 +28,9 @@ |
||
28 | 28 | } |
29 | 29 | |
30 | 30 | public function __destruct() { |
31 | - if ( $this->enabled ) call_user_func( $this->callback ); |
|
31 | + if ( $this->enabled ) { |
|
32 | + call_user_func( $this->callback ); |
|
33 | + } |
|
32 | 34 | } |
33 | 35 | |
34 | 36 | } |
35 | 37 | \ No newline at end of file |
@@ -38,9 +38,9 @@ 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 | - $this->URLPattern = rtrim(implode('',static::$prefix),'/') . '/' . trim($URLPattern,'/')?:'/'; |
|
43 | - $this->URLPattern = $this->URLPattern != '/' ? rtrim($this->URLPattern,'/') : $this->URLPattern; |
|
41 | + public function __construct($URLPattern, $callback = null, $method = 'get') { |
|
42 | + $this->URLPattern = rtrim(implode('', static::$prefix), '/').'/'.trim($URLPattern, '/') ?: '/'; |
|
43 | + $this->URLPattern = $this->URLPattern != '/' ? rtrim($this->URLPattern, '/') : $this->URLPattern; |
|
44 | 44 | $this->dynamic = $this->isDynamic($this->URLPattern); |
45 | 45 | $this->pattern = $this->dynamic ? $this->compilePatternAsRegex($this->URLPattern, $this->rules) : $this->URLPattern; |
46 | 46 | $this->callback = $callback; |
@@ -56,19 +56,19 @@ discard block |
||
56 | 56 | * @param string $method The HTTP Method to check against. |
57 | 57 | * @return boolean |
58 | 58 | */ |
59 | - public function match($URL,$method='get'){ |
|
59 | + public function match($URL, $method = 'get') { |
|
60 | 60 | $method = strtolower($method); |
61 | 61 | |
62 | 62 | // * is an http method wildcard |
63 | 63 | if (empty($this->methods[$method]) && empty($this->methods['*'])) return false; |
64 | - $URL = rtrim($URL,'/'); |
|
64 | + $URL = rtrim($URL, '/'); |
|
65 | 65 | $args = []; |
66 | - if ( $this->dynamic |
|
67 | - ? preg_match($this->pattern,$URL,$args) |
|
68 | - : $URL == rtrim($this->pattern,'/') |
|
69 | - ){ |
|
66 | + if ($this->dynamic |
|
67 | + ? preg_match($this->pattern, $URL, $args) |
|
68 | + : $URL == rtrim($this->pattern, '/') |
|
69 | + ) { |
|
70 | 70 | foreach ($args as $key => $value) { |
71 | - if ( false === is_string($key) ) unset($args[$key]); |
|
71 | + if (false === is_string($key)) unset($args[$key]); |
|
72 | 72 | } |
73 | 73 | return $args; |
74 | 74 | } |
@@ -81,23 +81,23 @@ discard block |
||
81 | 81 | * @param string $method The HTTP Method requested. |
82 | 82 | * @return array The callback response. |
83 | 83 | */ |
84 | - public function run(array $args,$method='get'){ |
|
84 | + public function run(array $args, $method = 'get') { |
|
85 | 85 | $method = strtolower($method); |
86 | - $this->response = ''; |
|
87 | - $this->response_object = null; |
|
86 | + $this->response = ''; |
|
87 | + $this->response_object = null; |
|
88 | 88 | $this->response_is_object = false; |
89 | 89 | |
90 | 90 | // Call direct befores |
91 | - if ( $this->befores ) { |
|
91 | + if ($this->befores) { |
|
92 | 92 | // Reverse befores order |
93 | 93 | foreach (array_reverse($this->befores) as $mw) { |
94 | 94 | ob_start(); |
95 | 95 | $mw_result = call_user_func($mw->bindTo($this)); |
96 | 96 | $this->response .= ob_get_clean(); |
97 | - if ( false === $mw_result ) { |
|
97 | + if (false === $mw_result) { |
|
98 | 98 | return ['']; |
99 | - } else if (is_a($mw_result,'View') || is_string($mw_result)) { |
|
100 | - $this->response .= (string)$mw_result; |
|
99 | + } else if (is_a($mw_result, 'View') || is_string($mw_result)) { |
|
100 | + $this->response .= (string) $mw_result; |
|
101 | 101 | } |
102 | 102 | } |
103 | 103 | } |
@@ -112,45 +112,45 @@ discard block |
||
112 | 112 | Response::type(Response::TYPE_HTML); |
113 | 113 | ob_start(); |
114 | 114 | // Silence "Cannot bind an instance to a static closure" warnings |
115 | - $view_results = call_user_func_array(@$callback->bindTo($this), $args); |
|
115 | + $view_results = call_user_func_array(@$callback->bindTo($this), $args); |
|
116 | 116 | $this->response .= ob_get_clean(); |
117 | 117 | |
118 | 118 | // Render View if returned, else echo string or encode json response |
119 | - if ( null !== $view_results ) { |
|
120 | - if (is_a($view_results,'View') || is_string($view_results)) { |
|
121 | - $this->response .= (string)$view_results; |
|
119 | + if (null !== $view_results) { |
|
120 | + if (is_a($view_results, 'View') || is_string($view_results)) { |
|
121 | + $this->response .= (string) $view_results; |
|
122 | 122 | } else { |
123 | 123 | $this->response_is_object = true; |
124 | - $this->response_object = $view_results; |
|
124 | + $this->response_object = $view_results; |
|
125 | 125 | } |
126 | 126 | } |
127 | 127 | |
128 | - } else if (is_a($callback,'View') || is_string($callback)) { |
|
128 | + } else if (is_a($callback, 'View') || is_string($callback)) { |
|
129 | 129 | // return rendered View or direct string |
130 | - $this->response .= (string)$callback; |
|
130 | + $this->response .= (string) $callback; |
|
131 | 131 | } else { |
132 | 132 | // JSON encode returned value |
133 | 133 | $this->response_is_object = true; |
134 | - $this->response_object = $callback; |
|
134 | + $this->response_object = $callback; |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | // Apply afters |
138 | - if ( $this->afters ) { |
|
138 | + if ($this->afters) { |
|
139 | 139 | foreach ($this->afters as $mw) { |
140 | 140 | ob_start(); |
141 | 141 | $mw_result = call_user_func($mw->bindTo($this)); |
142 | 142 | $this->response .= ob_get_clean(); |
143 | - if ( false === $mw_result ) { |
|
143 | + if (false === $mw_result) { |
|
144 | 144 | return ['']; |
145 | - } else if (is_a($mw_result,'View') || is_string($mw_result)) { |
|
146 | - $this->response .= (string)$mw_result; |
|
145 | + } else if (is_a($mw_result, 'View') || is_string($mw_result)) { |
|
146 | + $this->response .= (string) $mw_result; |
|
147 | 147 | } |
148 | 148 | } |
149 | 149 | } |
150 | 150 | |
151 | 151 | Event::trigger('core.route.after', $this); |
152 | 152 | |
153 | - if ( $this->response_is_object ){ |
|
153 | + if ($this->response_is_object) { |
|
154 | 154 | $this->response = Response::json($this->response_object); |
155 | 155 | } else { |
156 | 156 | Response::add($this->response); |
@@ -167,8 +167,8 @@ discard block |
||
167 | 167 | * @param string $method The HTTP Method to check against. |
168 | 168 | * @return array The callback response. |
169 | 169 | */ |
170 | - public function runIfMatch($URL, $method='get'){ |
|
171 | - return ($args = $this->match($URL,$method)) ? $this->run($args,$method) : null; |
|
170 | + public function runIfMatch($URL, $method = 'get') { |
|
171 | + return ($args = $this->match($URL, $method)) ? $this->run($args, $method) : null; |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | /** |
@@ -177,8 +177,8 @@ discard block |
||
177 | 177 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
178 | 178 | * @return Route |
179 | 179 | */ |
180 | - public static function on($URLPattern, $callback = null){ |
|
181 | - return new Route($URLPattern,$callback); |
|
180 | + public static function on($URLPattern, $callback = null) { |
|
181 | + return new Route($URLPattern, $callback); |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | /** |
@@ -187,8 +187,8 @@ discard block |
||
187 | 187 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
188 | 188 | * @return Route |
189 | 189 | */ |
190 | - public static function any($URLPattern, $callback = null){ |
|
191 | - return (new Route($URLPattern,$callback))->via('*'); |
|
190 | + public static function any($URLPattern, $callback = null) { |
|
191 | + return (new Route($URLPattern, $callback))->via('*'); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | /** |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | */ |
234 | 234 | public function & via(){ |
235 | 235 | $this->methods = []; |
236 | - foreach (func_get_args() as $method){ |
|
236 | + foreach (func_get_args() as $method) { |
|
237 | 237 | $this->methods[strtolower($method)] = true; |
238 | 238 | } |
239 | 239 | return $this; |
@@ -255,10 +255,10 @@ discard block |
||
255 | 255 | * @return Route |
256 | 256 | */ |
257 | 257 | public function & rules(array $rules){ |
258 | - foreach ((array)$rules as $varname => $rule){ |
|
258 | + foreach ((array) $rules as $varname => $rule) { |
|
259 | 259 | $this->rules[$varname] = $rule; |
260 | 260 | } |
261 | - $this->pattern = $this->compilePatternAsRegex($this->URLPattern,$this->rules); |
|
261 | + $this->pattern = $this->compilePatternAsRegex($this->URLPattern, $this->rules); |
|
262 | 262 | return $this; |
263 | 263 | } |
264 | 264 | |
@@ -296,10 +296,10 @@ discard block |
||
296 | 296 | * @param string $pattern The URL schema. |
297 | 297 | * @return string The compiled PREG RegEx. |
298 | 298 | */ |
299 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
300 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
301 | - return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
|
302 | - },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
|
299 | + protected static function compilePatternAsRegex($pattern, $rules = []) { |
|
300 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S', function($g) use (&$rules){ |
|
301 | + return '(?<'.$g[1].'>'.(isset($rules[$g[1]]) ? $rules[$g[1]] : '[^/]+').')'; |
|
302 | + },str_replace(['.', ')', '*'], ['\.', ')?', '.+'], $pattern)).'$#'; |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | /** |
@@ -309,10 +309,10 @@ discard block |
||
309 | 309 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
310 | 310 | * @return array The extracted variables |
311 | 311 | */ |
312 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
312 | + protected static function extractVariablesFromURL($pattern, $URL = null, $cut = false) { |
|
313 | 313 | $URL = $URL ?: Request::URI(); |
314 | - $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
|
315 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
314 | + $pattern = $cut ? str_replace('$#', '', $pattern).'#' : $pattern; |
|
315 | + if (!preg_match($pattern, $URL, $args)) return false; |
|
316 | 316 | foreach ($args as $key => $value) { |
317 | 317 | if (false === is_string($key)) unset($args[$key]); |
318 | 318 | } |
@@ -324,8 +324,8 @@ discard block |
||
324 | 324 | * @param string $pattern The URL schema. |
325 | 325 | * @return boolean |
326 | 326 | */ |
327 | - protected static function isDynamic($pattern){ |
|
328 | - return strlen($pattern) != strcspn($pattern,':(?[*+'); |
|
327 | + protected static function isDynamic($pattern) { |
|
328 | + return strlen($pattern) != strcspn($pattern, ':(?[*+'); |
|
329 | 329 | } |
330 | 330 | |
331 | 331 | /** |
@@ -333,9 +333,9 @@ discard block |
||
333 | 333 | * @param Route $r |
334 | 334 | * @return Route |
335 | 335 | */ |
336 | - public static function add($r){ |
|
337 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
338 | - return static::$routes[implode('',static::$prefix)][] = $r; |
|
336 | + public static function add($r) { |
|
337 | + if (isset(static::$group[0])) static::$group[0]->add($r); |
|
338 | + return static::$routes[implode('', static::$prefix)][] = $r; |
|
339 | 339 | } |
340 | 340 | |
341 | 341 | /** |
@@ -343,16 +343,16 @@ discard block |
||
343 | 343 | * @param string $prefix The url prefix for the internal route definitions. |
344 | 344 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
345 | 345 | */ |
346 | - public static function group($prefix,$callback=null){ |
|
346 | + public static function group($prefix, $callback = null) { |
|
347 | 347 | |
348 | 348 | // Skip definition if current request doesn't match group. |
349 | 349 | |
350 | - $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
|
350 | + $prefix_complete = rtrim(implode('', static::$prefix), '/').$prefix; |
|
351 | 351 | |
352 | - if ( static::isDynamic($prefix) ){ |
|
352 | + if (static::isDynamic($prefix)) { |
|
353 | 353 | |
354 | 354 | // Dynamic group, capture vars |
355 | - $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete),null,true); |
|
355 | + $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete), null, true); |
|
356 | 356 | |
357 | 357 | // Errors in compile pattern or variable extraction, aborting. |
358 | 358 | if (false === $vars) return; |
@@ -360,14 +360,14 @@ discard block |
||
360 | 360 | static::$prefix[] = $prefix; |
361 | 361 | if (empty(static::$group)) static::$group = []; |
362 | 362 | array_unshift(static::$group, new RouteGroup()); |
363 | - if ($callback) call_user_func_array($callback,$vars); |
|
363 | + if ($callback) call_user_func_array($callback, $vars); |
|
364 | 364 | $group = static::$group[0]; |
365 | 365 | array_shift(static::$group); |
366 | 366 | array_pop(static::$prefix); |
367 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
367 | + if (empty(static::$prefix)) static::$prefix = ['']; |
|
368 | 368 | return $group; |
369 | 369 | |
370 | - } else if ( 0 === strpos(Request::URI(), $prefix_complete) ){ |
|
370 | + } else if (0 === strpos(Request::URI(), $prefix_complete)) { |
|
371 | 371 | |
372 | 372 | // Static group |
373 | 373 | static::$prefix[] = $prefix; |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | $group = static::$group[0]; |
378 | 378 | array_shift(static::$group); |
379 | 379 | array_pop(static::$prefix); |
380 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
380 | + if (empty(static::$prefix)) static::$prefix = ['']; |
|
381 | 381 | return $group; |
382 | 382 | } else { |
383 | 383 | |
@@ -387,8 +387,8 @@ discard block |
||
387 | 387 | |
388 | 388 | } |
389 | 389 | |
390 | - public static function exitWithError($code,$message="Application Error"){ |
|
391 | - Response::error($code,$message); |
|
390 | + public static function exitWithError($code, $message = "Application Error") { |
|
391 | + Response::error($code, $message); |
|
392 | 392 | Response::send(); |
393 | 393 | exit; |
394 | 394 | } |
@@ -398,18 +398,18 @@ discard block |
||
398 | 398 | * @param string $URL The URL to match onto. |
399 | 399 | * @return boolean true if a route callback was executed. |
400 | 400 | */ |
401 | - public static function dispatch($URL=null,$method=null){ |
|
401 | + public static function dispatch($URL = null, $method = null) { |
|
402 | 402 | if (!$URL) $URL = Request::URI(); |
403 | 403 | if (!$method) $method = Request::method(); |
404 | 404 | |
405 | - if (Options::get('core.response.autosend',true)){ |
|
405 | + if (Options::get('core.response.autosend', true)) { |
|
406 | 406 | $__deferred_send = new Deferred('Response::send'); |
407 | 407 | } |
408 | 408 | |
409 | - foreach ((array)static::$routes as $group => $routes){ |
|
409 | + foreach ((array) static::$routes as $group => $routes) { |
|
410 | 410 | foreach ($routes as $route) { |
411 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
412 | - $route->run($args,$method); |
|
411 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL, $method))) { |
|
412 | + $route->run($args, $method); |
|
413 | 413 | return true; |
414 | 414 | } |
415 | 415 | } |
@@ -424,34 +424,34 @@ discard block |
||
424 | 424 | class RouteGroup { |
425 | 425 | protected $routes; |
426 | 426 | |
427 | - public function __construct(){ |
|
427 | + public function __construct() { |
|
428 | 428 | $this->routes = new SplObjectStorage; |
429 | 429 | return Route::add($this); |
430 | 430 | } |
431 | 431 | |
432 | - public function has($r){ |
|
432 | + public function has($r) { |
|
433 | 433 | return $this->routes->contains($r); |
434 | 434 | } |
435 | 435 | |
436 | - public function add($r){ |
|
436 | + public function add($r) { |
|
437 | 437 | $this->routes->attach($r); |
438 | 438 | return $this; |
439 | 439 | } |
440 | 440 | |
441 | - public function remove($r){ |
|
441 | + public function remove($r) { |
|
442 | 442 | if ($this->routes->contains($r)) $this->routes->detach($r); |
443 | 443 | return $this; |
444 | 444 | } |
445 | 445 | |
446 | - public function before($callbacks){ |
|
447 | - foreach ($this->routes as $route){ |
|
446 | + public function before($callbacks) { |
|
447 | + foreach ($this->routes as $route) { |
|
448 | 448 | $route->before($callbacks); |
449 | 449 | } |
450 | 450 | return $this; |
451 | 451 | } |
452 | 452 | |
453 | - public function after($callbacks){ |
|
454 | - foreach ($this->routes as $route){ |
|
453 | + public function after($callbacks) { |
|
454 | + foreach ($this->routes as $route) { |
|
455 | 455 | $route->after($callbacks); |
456 | 456 | } |
457 | 457 | return $this; |
@@ -38,7 +38,7 @@ 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'){ |
|
41 | + public function __construct($URLPattern, $callback = null, $method='get') { |
|
42 | 42 | $this->URLPattern = rtrim(implode('',static::$prefix),'/') . '/' . trim($URLPattern,'/')?:'/'; |
43 | 43 | $this->URLPattern = $this->URLPattern != '/' ? rtrim($this->URLPattern,'/') : $this->URLPattern; |
44 | 44 | $this->dynamic = $this->isDynamic($this->URLPattern); |
@@ -56,19 +56,23 @@ discard block |
||
56 | 56 | * @param string $method The HTTP Method to check against. |
57 | 57 | * @return boolean |
58 | 58 | */ |
59 | - public function match($URL,$method='get'){ |
|
59 | + public function match($URL,$method='get') { |
|
60 | 60 | $method = strtolower($method); |
61 | 61 | |
62 | 62 | // * is an http method wildcard |
63 | - if (empty($this->methods[$method]) && empty($this->methods['*'])) return false; |
|
63 | + if (empty($this->methods[$method]) && empty($this->methods['*'])) { |
|
64 | + return false; |
|
65 | + } |
|
64 | 66 | $URL = rtrim($URL,'/'); |
65 | 67 | $args = []; |
66 | 68 | if ( $this->dynamic |
67 | 69 | ? preg_match($this->pattern,$URL,$args) |
68 | 70 | : $URL == rtrim($this->pattern,'/') |
69 | - ){ |
|
71 | + ) { |
|
70 | 72 | foreach ($args as $key => $value) { |
71 | - if ( false === is_string($key) ) unset($args[$key]); |
|
73 | + if ( false === is_string($key) ) { |
|
74 | + unset($args[$key]); |
|
75 | + } |
|
72 | 76 | } |
73 | 77 | return $args; |
74 | 78 | } |
@@ -81,7 +85,7 @@ discard block |
||
81 | 85 | * @param string $method The HTTP Method requested. |
82 | 86 | * @return array The callback response. |
83 | 87 | */ |
84 | - public function run(array $args,$method='get'){ |
|
88 | + public function run(array $args,$method='get') { |
|
85 | 89 | $method = strtolower($method); |
86 | 90 | $this->response = ''; |
87 | 91 | $this->response_object = null; |
@@ -150,7 +154,7 @@ discard block |
||
150 | 154 | |
151 | 155 | Event::trigger('core.route.after', $this); |
152 | 156 | |
153 | - if ( $this->response_is_object ){ |
|
157 | + if ( $this->response_is_object ) { |
|
154 | 158 | $this->response = Response::json($this->response_object); |
155 | 159 | } else { |
156 | 160 | Response::add($this->response); |
@@ -167,7 +171,7 @@ discard block |
||
167 | 171 | * @param string $method The HTTP Method to check against. |
168 | 172 | * @return array The callback response. |
169 | 173 | */ |
170 | - public function runIfMatch($URL, $method='get'){ |
|
174 | + public function runIfMatch($URL, $method='get') { |
|
171 | 175 | return ($args = $this->match($URL,$method)) ? $this->run($args,$method) : null; |
172 | 176 | } |
173 | 177 | |
@@ -177,7 +181,7 @@ discard block |
||
177 | 181 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
178 | 182 | * @return Route |
179 | 183 | */ |
180 | - public static function on($URLPattern, $callback = null){ |
|
184 | + public static function on($URLPattern, $callback = null) { |
|
181 | 185 | return new Route($URLPattern,$callback); |
182 | 186 | } |
183 | 187 | |
@@ -187,7 +191,7 @@ discard block |
||
187 | 191 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
188 | 192 | * @return Route |
189 | 193 | */ |
190 | - public static function any($URLPattern, $callback = null){ |
|
194 | + public static function any($URLPattern, $callback = null) { |
|
191 | 195 | return (new Route($URLPattern,$callback))->via('*'); |
192 | 196 | } |
193 | 197 | |
@@ -233,7 +237,7 @@ discard block |
||
233 | 237 | */ |
234 | 238 | public function & via(){ |
235 | 239 | $this->methods = []; |
236 | - foreach (func_get_args() as $method){ |
|
240 | + foreach (func_get_args() as $method) { |
|
237 | 241 | $this->methods[strtolower($method)] = true; |
238 | 242 | } |
239 | 243 | return $this; |
@@ -255,7 +259,7 @@ discard block |
||
255 | 259 | * @return Route |
256 | 260 | */ |
257 | 261 | public function & rules(array $rules){ |
258 | - foreach ((array)$rules as $varname => $rule){ |
|
262 | + foreach ((array)$rules as $varname => $rule) { |
|
259 | 263 | $this->rules[$varname] = $rule; |
260 | 264 | } |
261 | 265 | $this->pattern = $this->compilePatternAsRegex($this->URLPattern,$this->rules); |
@@ -284,7 +288,9 @@ discard block |
||
284 | 288 | $route->callback = []; |
285 | 289 | foreach ($callbacks as $method => $callback) { |
286 | 290 | $method = strtolower($method); |
287 | - if (Request::method() !== $method) continue; |
|
291 | + if (Request::method() !== $method) { |
|
292 | + continue; |
|
293 | + } |
|
288 | 294 | $route->callback[$method] = $callback; |
289 | 295 | $route->methods[$method] = 1; |
290 | 296 | } |
@@ -296,8 +302,8 @@ discard block |
||
296 | 302 | * @param string $pattern The URL schema. |
297 | 303 | * @return string The compiled PREG RegEx. |
298 | 304 | */ |
299 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
300 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
305 | + protected static function compilePatternAsRegex($pattern, $rules=[]) { |
|
306 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules) { |
|
301 | 307 | return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
302 | 308 | },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
303 | 309 | } |
@@ -309,12 +315,16 @@ discard block |
||
309 | 315 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
310 | 316 | * @return array The extracted variables |
311 | 317 | */ |
312 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
318 | + protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false) { |
|
313 | 319 | $URL = $URL ?: Request::URI(); |
314 | 320 | $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
315 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
321 | + if ( !preg_match($pattern,$URL,$args) ) { |
|
322 | + return false; |
|
323 | + } |
|
316 | 324 | foreach ($args as $key => $value) { |
317 | - if (false === is_string($key)) unset($args[$key]); |
|
325 | + if (false === is_string($key)) { |
|
326 | + unset($args[$key]); |
|
327 | + } |
|
318 | 328 | } |
319 | 329 | return $args; |
320 | 330 | } |
@@ -324,7 +334,7 @@ discard block |
||
324 | 334 | * @param string $pattern The URL schema. |
325 | 335 | * @return boolean |
326 | 336 | */ |
327 | - protected static function isDynamic($pattern){ |
|
337 | + protected static function isDynamic($pattern) { |
|
328 | 338 | return strlen($pattern) != strcspn($pattern,':(?[*+'); |
329 | 339 | } |
330 | 340 | |
@@ -333,8 +343,10 @@ discard block |
||
333 | 343 | * @param Route $r |
334 | 344 | * @return Route |
335 | 345 | */ |
336 | - public static function add($r){ |
|
337 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
346 | + public static function add($r) { |
|
347 | + if ( isset(static::$group[0]) ) { |
|
348 | + static::$group[0]->add($r); |
|
349 | + } |
|
338 | 350 | return static::$routes[implode('',static::$prefix)][] = $r; |
339 | 351 | } |
340 | 352 | |
@@ -343,41 +355,55 @@ discard block |
||
343 | 355 | * @param string $prefix The url prefix for the internal route definitions. |
344 | 356 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
345 | 357 | */ |
346 | - public static function group($prefix,$callback=null){ |
|
358 | + public static function group($prefix,$callback=null) { |
|
347 | 359 | |
348 | 360 | // Skip definition if current request doesn't match group. |
349 | 361 | |
350 | 362 | $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
351 | 363 | |
352 | - if ( static::isDynamic($prefix) ){ |
|
364 | + if ( static::isDynamic($prefix) ) { |
|
353 | 365 | |
354 | 366 | // Dynamic group, capture vars |
355 | 367 | $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete),null,true); |
356 | 368 | |
357 | 369 | // Errors in compile pattern or variable extraction, aborting. |
358 | - if (false === $vars) return; |
|
370 | + if (false === $vars) { |
|
371 | + return; |
|
372 | + } |
|
359 | 373 | |
360 | 374 | static::$prefix[] = $prefix; |
361 | - if (empty(static::$group)) static::$group = []; |
|
375 | + if (empty(static::$group)) { |
|
376 | + static::$group = []; |
|
377 | + } |
|
362 | 378 | array_unshift(static::$group, new RouteGroup()); |
363 | - if ($callback) call_user_func_array($callback,$vars); |
|
379 | + if ($callback) { |
|
380 | + call_user_func_array($callback,$vars); |
|
381 | + } |
|
364 | 382 | $group = static::$group[0]; |
365 | 383 | array_shift(static::$group); |
366 | 384 | array_pop(static::$prefix); |
367 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
385 | + if (empty(static::$prefix)) { |
|
386 | + static::$prefix=['']; |
|
387 | + } |
|
368 | 388 | return $group; |
369 | 389 | |
370 | - } else if ( 0 === strpos(Request::URI(), $prefix_complete) ){ |
|
390 | + } else if ( 0 === strpos(Request::URI(), $prefix_complete) ) { |
|
371 | 391 | |
372 | 392 | // Static group |
373 | 393 | static::$prefix[] = $prefix; |
374 | - if (empty(static::$group)) static::$group = []; |
|
394 | + if (empty(static::$group)) { |
|
395 | + static::$group = []; |
|
396 | + } |
|
375 | 397 | array_unshift(static::$group, new RouteGroup()); |
376 | - if ($callback) call_user_func($callback); |
|
398 | + if ($callback) { |
|
399 | + call_user_func($callback); |
|
400 | + } |
|
377 | 401 | $group = static::$group[0]; |
378 | 402 | array_shift(static::$group); |
379 | 403 | array_pop(static::$prefix); |
380 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
404 | + if (empty(static::$prefix)) { |
|
405 | + static::$prefix=['']; |
|
406 | + } |
|
381 | 407 | return $group; |
382 | 408 | } else { |
383 | 409 | |
@@ -387,7 +413,7 @@ discard block |
||
387 | 413 | |
388 | 414 | } |
389 | 415 | |
390 | - public static function exitWithError($code,$message="Application Error"){ |
|
416 | + public static function exitWithError($code,$message="Application Error") { |
|
391 | 417 | Response::error($code,$message); |
392 | 418 | Response::send(); |
393 | 419 | exit; |
@@ -398,17 +424,21 @@ discard block |
||
398 | 424 | * @param string $URL The URL to match onto. |
399 | 425 | * @return boolean true if a route callback was executed. |
400 | 426 | */ |
401 | - public static function dispatch($URL=null,$method=null){ |
|
402 | - if (!$URL) $URL = Request::URI(); |
|
403 | - if (!$method) $method = Request::method(); |
|
427 | + public static function dispatch($URL=null,$method=null) { |
|
428 | + if (!$URL) { |
|
429 | + $URL = Request::URI(); |
|
430 | + } |
|
431 | + if (!$method) { |
|
432 | + $method = Request::method(); |
|
433 | + } |
|
404 | 434 | |
405 | - if (Options::get('core.response.autosend',true)){ |
|
435 | + if (Options::get('core.response.autosend',true)) { |
|
406 | 436 | $__deferred_send = new Deferred('Response::send'); |
407 | 437 | } |
408 | 438 | |
409 | - foreach ((array)static::$routes as $group => $routes){ |
|
439 | + foreach ((array)static::$routes as $group => $routes) { |
|
410 | 440 | foreach ($routes as $route) { |
411 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
441 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))) { |
|
412 | 442 | $route->run($args,$method); |
413 | 443 | return true; |
414 | 444 | } |
@@ -424,34 +454,36 @@ discard block |
||
424 | 454 | class RouteGroup { |
425 | 455 | protected $routes; |
426 | 456 | |
427 | - public function __construct(){ |
|
457 | + public function __construct() { |
|
428 | 458 | $this->routes = new SplObjectStorage; |
429 | 459 | return Route::add($this); |
430 | 460 | } |
431 | 461 | |
432 | - public function has($r){ |
|
462 | + public function has($r) { |
|
433 | 463 | return $this->routes->contains($r); |
434 | 464 | } |
435 | 465 | |
436 | - public function add($r){ |
|
466 | + public function add($r) { |
|
437 | 467 | $this->routes->attach($r); |
438 | 468 | return $this; |
439 | 469 | } |
440 | 470 | |
441 | - public function remove($r){ |
|
442 | - if ($this->routes->contains($r)) $this->routes->detach($r); |
|
471 | + public function remove($r) { |
|
472 | + if ($this->routes->contains($r)) { |
|
473 | + $this->routes->detach($r); |
|
474 | + } |
|
443 | 475 | return $this; |
444 | 476 | } |
445 | 477 | |
446 | - public function before($callbacks){ |
|
447 | - foreach ($this->routes as $route){ |
|
478 | + public function before($callbacks) { |
|
479 | + foreach ($this->routes as $route) { |
|
448 | 480 | $route->before($callbacks); |
449 | 481 | } |
450 | 482 | return $this; |
451 | 483 | } |
452 | 484 | |
453 | - public function after($callbacks){ |
|
454 | - foreach ($this->routes as $route){ |
|
485 | + public function after($callbacks) { |
|
486 | + foreach ($this->routes as $route) { |
|
455 | 487 | $route->after($callbacks); |
456 | 488 | } |
457 | 489 | return $this; |