@@ -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 get($URLPattern, $callback = null){ |
|
191 | - return (new Route($URLPattern,$callback))->via('get'); |
|
190 | + public static function get($URLPattern, $callback = null) { |
|
191 | + return (new Route($URLPattern, $callback))->via('get'); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | /** |
@@ -197,8 +197,8 @@ discard block |
||
197 | 197 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
198 | 198 | * @return Route |
199 | 199 | */ |
200 | - public static function post($URLPattern, $callback = null){ |
|
201 | - return (new Route($URLPattern,$callback))->via('post'); |
|
200 | + public static function post($URLPattern, $callback = null) { |
|
201 | + return (new Route($URLPattern, $callback))->via('post'); |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
@@ -207,8 +207,8 @@ discard block |
||
207 | 207 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
208 | 208 | * @return Route |
209 | 209 | */ |
210 | - public static function any($URLPattern, $callback = null){ |
|
211 | - return (new Route($URLPattern,$callback))->via('*'); |
|
210 | + public static function any($URLPattern, $callback = null) { |
|
211 | + return (new Route($URLPattern, $callback))->via('*'); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | /** |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | */ |
254 | 254 | public function & via(){ |
255 | 255 | $this->methods = []; |
256 | - foreach (func_get_args() as $method){ |
|
256 | + foreach (func_get_args() as $method) { |
|
257 | 257 | $this->methods[strtolower($method)] = true; |
258 | 258 | } |
259 | 259 | return $this; |
@@ -275,10 +275,10 @@ discard block |
||
275 | 275 | * @return Route |
276 | 276 | */ |
277 | 277 | public function & rules(array $rules){ |
278 | - foreach ((array)$rules as $varname => $rule){ |
|
278 | + foreach ((array) $rules as $varname => $rule) { |
|
279 | 279 | $this->rules[$varname] = $rule; |
280 | 280 | } |
281 | - $this->pattern = $this->compilePatternAsRegex($this->URLPattern,$this->rules); |
|
281 | + $this->pattern = $this->compilePatternAsRegex($this->URLPattern, $this->rules); |
|
282 | 282 | return $this; |
283 | 283 | } |
284 | 284 | |
@@ -316,10 +316,10 @@ discard block |
||
316 | 316 | * @param string $pattern The URL schema. |
317 | 317 | * @return string The compiled PREG RegEx. |
318 | 318 | */ |
319 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
320 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
321 | - return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
|
322 | - },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
|
319 | + protected static function compilePatternAsRegex($pattern, $rules = []) { |
|
320 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S', function($g) use (&$rules){ |
|
321 | + return '(?<'.$g[1].'>'.(isset($rules[$g[1]]) ? $rules[$g[1]] : '[^/]+').')'; |
|
322 | + },str_replace(['.', ')', '*'], ['\.', ')?', '.+'], $pattern)).'$#'; |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | /** |
@@ -329,10 +329,10 @@ discard block |
||
329 | 329 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
330 | 330 | * @return array The extracted variables |
331 | 331 | */ |
332 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
332 | + protected static function extractVariablesFromURL($pattern, $URL = null, $cut = false) { |
|
333 | 333 | $URL = $URL ?: Request::URI(); |
334 | - $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
|
335 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
334 | + $pattern = $cut ? str_replace('$#', '', $pattern).'#' : $pattern; |
|
335 | + if (!preg_match($pattern, $URL, $args)) return false; |
|
336 | 336 | foreach ($args as $key => $value) { |
337 | 337 | if (false === is_string($key)) unset($args[$key]); |
338 | 338 | } |
@@ -344,8 +344,8 @@ discard block |
||
344 | 344 | * @param string $pattern The URL schema. |
345 | 345 | * @return boolean |
346 | 346 | */ |
347 | - protected static function isDynamic($pattern){ |
|
348 | - return strlen($pattern) != strcspn($pattern,':(?[*+'); |
|
347 | + protected static function isDynamic($pattern) { |
|
348 | + return strlen($pattern) != strcspn($pattern, ':(?[*+'); |
|
349 | 349 | } |
350 | 350 | |
351 | 351 | /** |
@@ -353,9 +353,9 @@ discard block |
||
353 | 353 | * @param Route $r |
354 | 354 | * @return Route |
355 | 355 | */ |
356 | - public static function add($r){ |
|
357 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
358 | - return static::$routes[implode('',static::$prefix)][] = $r; |
|
356 | + public static function add($r) { |
|
357 | + if (isset(static::$group[0])) static::$group[0]->add($r); |
|
358 | + return static::$routes[implode('', static::$prefix)][] = $r; |
|
359 | 359 | } |
360 | 360 | |
361 | 361 | /** |
@@ -363,16 +363,16 @@ discard block |
||
363 | 363 | * @param string $prefix The url prefix for the internal route definitions. |
364 | 364 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
365 | 365 | */ |
366 | - public static function group($prefix,$callback=null){ |
|
366 | + public static function group($prefix, $callback = null) { |
|
367 | 367 | |
368 | 368 | // Skip definition if current request doesn't match group. |
369 | 369 | |
370 | - $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
|
370 | + $prefix_complete = rtrim(implode('', static::$prefix), '/').$prefix; |
|
371 | 371 | |
372 | - if ( static::isDynamic($prefix) ){ |
|
372 | + if (static::isDynamic($prefix)) { |
|
373 | 373 | |
374 | 374 | // Dynamic group, capture vars |
375 | - $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete),null,true); |
|
375 | + $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete), null, true); |
|
376 | 376 | |
377 | 377 | // Errors in compile pattern or variable extraction, aborting. |
378 | 378 | if (false === $vars) return; |
@@ -380,14 +380,14 @@ discard block |
||
380 | 380 | static::$prefix[] = $prefix; |
381 | 381 | if (empty(static::$group)) static::$group = []; |
382 | 382 | array_unshift(static::$group, new RouteGroup()); |
383 | - if ($callback) call_user_func_array($callback,$vars); |
|
383 | + if ($callback) call_user_func_array($callback, $vars); |
|
384 | 384 | $group = static::$group[0]; |
385 | 385 | array_shift(static::$group); |
386 | 386 | array_pop(static::$prefix); |
387 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
387 | + if (empty(static::$prefix)) static::$prefix = ['']; |
|
388 | 388 | return $group; |
389 | 389 | |
390 | - } else if ( 0 === strpos(Request::URI(), $prefix_complete) ){ |
|
390 | + } else if (0 === strpos(Request::URI(), $prefix_complete)) { |
|
391 | 391 | |
392 | 392 | // Static group |
393 | 393 | static::$prefix[] = $prefix; |
@@ -397,7 +397,7 @@ discard block |
||
397 | 397 | $group = static::$group[0]; |
398 | 398 | array_shift(static::$group); |
399 | 399 | array_pop(static::$prefix); |
400 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
400 | + if (empty(static::$prefix)) static::$prefix = ['']; |
|
401 | 401 | return $group; |
402 | 402 | } else { |
403 | 403 | |
@@ -407,8 +407,8 @@ discard block |
||
407 | 407 | |
408 | 408 | } |
409 | 409 | |
410 | - public static function exitWithError($code,$message="Application Error"){ |
|
411 | - Response::error($code,$message); |
|
410 | + public static function exitWithError($code, $message = "Application Error") { |
|
411 | + Response::error($code, $message); |
|
412 | 412 | Response::send(); |
413 | 413 | exit; |
414 | 414 | } |
@@ -418,20 +418,20 @@ discard block |
||
418 | 418 | * @param string $URL The URL to match onto. |
419 | 419 | * @return boolean true if a route callback was executed. |
420 | 420 | */ |
421 | - public static function dispatch($URL=null,$method=null){ |
|
421 | + public static function dispatch($URL = null, $method = null) { |
|
422 | 422 | if (!$URL) $URL = Request::URI(); |
423 | 423 | if (!$method) $method = Request::method(); |
424 | 424 | |
425 | - $__deferred_send = new Deferred(function(){ |
|
426 | - if (Options::get('core.response.autosend',false)){ |
|
425 | + $__deferred_send = new Deferred(function() { |
|
426 | + if (Options::get('core.response.autosend', false)) { |
|
427 | 427 | Response::send(); |
428 | 428 | } |
429 | 429 | }); |
430 | 430 | |
431 | - foreach ((array)static::$routes as $group => $routes){ |
|
431 | + foreach ((array) static::$routes as $group => $routes) { |
|
432 | 432 | foreach ($routes as $route) { |
433 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
434 | - $route->run($args,$method); |
|
433 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL, $method))) { |
|
434 | + $route->run($args, $method); |
|
435 | 435 | return true; |
436 | 436 | } |
437 | 437 | } |
@@ -446,34 +446,34 @@ discard block |
||
446 | 446 | class RouteGroup { |
447 | 447 | protected $routes; |
448 | 448 | |
449 | - public function __construct(){ |
|
449 | + public function __construct() { |
|
450 | 450 | $this->routes = new SplObjectStorage; |
451 | 451 | return Route::add($this); |
452 | 452 | } |
453 | 453 | |
454 | - public function has($r){ |
|
454 | + public function has($r) { |
|
455 | 455 | return $this->routes->contains($r); |
456 | 456 | } |
457 | 457 | |
458 | - public function add($r){ |
|
458 | + public function add($r) { |
|
459 | 459 | $this->routes->attach($r); |
460 | 460 | return $this; |
461 | 461 | } |
462 | 462 | |
463 | - public function remove($r){ |
|
463 | + public function remove($r) { |
|
464 | 464 | if ($this->routes->contains($r)) $this->routes->detach($r); |
465 | 465 | return $this; |
466 | 466 | } |
467 | 467 | |
468 | - public function before($callbacks){ |
|
469 | - foreach ($this->routes as $route){ |
|
468 | + public function before($callbacks) { |
|
469 | + foreach ($this->routes as $route) { |
|
470 | 470 | $route->before($callbacks); |
471 | 471 | } |
472 | 472 | return $this; |
473 | 473 | } |
474 | 474 | |
475 | - public function after($callbacks){ |
|
476 | - foreach ($this->routes as $route){ |
|
475 | + public function after($callbacks) { |
|
476 | + foreach ($this->routes as $route) { |
|
477 | 477 | $route->after($callbacks); |
478 | 478 | } |
479 | 479 | return $this; |