@@ -14,9 +14,9 @@ discard block |
||
14 | 14 | use Module; |
15 | 15 | |
16 | 16 | protected static $routes, |
17 | - $base = '', |
|
18 | - $prefix = [], |
|
19 | - $group = []; |
|
17 | + $base = '', |
|
18 | + $prefix = [], |
|
19 | + $group = []; |
|
20 | 20 | |
21 | 21 | protected $URLPattern = '', |
22 | 22 | $pattern = '', |
@@ -85,20 +85,20 @@ discard block |
||
85 | 85 | $method = strtolower($method); |
86 | 86 | $this->response = ''; |
87 | 87 | $this->response_object = null; |
88 | - $this->response_is_object = false; |
|
88 | + $this->response_is_object = false; |
|
89 | 89 | |
90 | 90 | // Call direct befores |
91 | 91 | if ( $this->befores ) { |
92 | 92 | // Reverse befores order |
93 | 93 | foreach (array_reverse($this->befores) as $mw) { |
94 | - ob_start(); |
|
94 | + ob_start(); |
|
95 | 95 | $mw_result = call_user_func($mw->bindTo($this)); |
96 | - $this->response .= ob_get_clean(); |
|
96 | + $this->response .= ob_get_clean(); |
|
97 | 97 | if ( false === $mw_result ) { |
98 | - return ['']; |
|
98 | + return ['']; |
|
99 | 99 | } else if (is_a($mw_result,'View') || is_string($mw_result)) { |
100 | 100 | $this->response .= (string)$mw_result; |
101 | - } |
|
101 | + } |
|
102 | 102 | } |
103 | 103 | } |
104 | 104 | |
@@ -109,51 +109,51 @@ discard block |
||
109 | 109 | |
110 | 110 | if (is_callable($callback)) { |
111 | 111 | // Capure callback output |
112 | - Response::type(Response::TYPE_HTML); |
|
113 | - ob_start(); |
|
114 | - // Silence "Cannot bind an instance to a static closure" warnings |
|
115 | - $view_results = call_user_func_array(@$callback->bindTo($this), $args); |
|
116 | - $this->response .= ob_get_clean(); |
|
117 | - |
|
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; |
|
122 | - } else { |
|
123 | - $this->response_is_object = true; |
|
124 | - $this->response_object = $view_results; |
|
125 | - } |
|
126 | - } |
|
112 | + Response::type(Response::TYPE_HTML); |
|
113 | + ob_start(); |
|
114 | + // Silence "Cannot bind an instance to a static closure" warnings |
|
115 | + $view_results = call_user_func_array(@$callback->bindTo($this), $args); |
|
116 | + $this->response .= ob_get_clean(); |
|
117 | + |
|
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; |
|
122 | + } else { |
|
123 | + $this->response_is_object = true; |
|
124 | + $this->response_object = $view_results; |
|
125 | + } |
|
126 | + } |
|
127 | 127 | |
128 | 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 | - $this->response_is_object = true; |
|
134 | - $this->response_object = $callback; |
|
133 | + $this->response_is_object = true; |
|
134 | + $this->response_object = $callback; |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | // Apply afters |
138 | 138 | if ( $this->afters ) { |
139 | 139 | foreach ($this->afters as $mw) { |
140 | - ob_start(); |
|
140 | + ob_start(); |
|
141 | 141 | $mw_result = call_user_func($mw->bindTo($this)); |
142 | - $this->response .= ob_get_clean(); |
|
142 | + $this->response .= ob_get_clean(); |
|
143 | 143 | if ( false === $mw_result ) { |
144 | - return ['']; |
|
144 | + return ['']; |
|
145 | 145 | } else if (is_a($mw_result,'View') || is_string($mw_result)) { |
146 | 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 | 153 | if ( $this->response_is_object ){ |
154 | - $this->response = Response::json($this->response_object); |
|
154 | + $this->response = Response::json($this->response_object); |
|
155 | 155 | } else { |
156 | - Response::add($this->response); |
|
156 | + Response::add($this->response); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | Event::trigger('core.route.end', $this); |
@@ -303,10 +303,10 @@ discard block |
||
303 | 303 | $route = new static($URLPattern); |
304 | 304 | $route->callback = []; |
305 | 305 | foreach ($callbacks as $method => $callback) { |
306 | - $method = strtolower($method); |
|
307 | - if (Request::method() !== $method) continue; |
|
308 | - $route->callback[$method] = $callback; |
|
309 | - $route->methods[$method] = 1; |
|
306 | + $method = strtolower($method); |
|
307 | + if (Request::method() !== $method) continue; |
|
308 | + $route->callback[$method] = $callback; |
|
309 | + $route->methods[$method] = 1; |
|
310 | 310 | } |
311 | 311 | return $route; |
312 | 312 | } |
@@ -408,9 +408,9 @@ discard block |
||
408 | 408 | } |
409 | 409 | |
410 | 410 | public static function exitWithError($code,$message="Application Error"){ |
411 | - Response::error($code,$message); |
|
412 | - Response::send(); |
|
413 | - exit; |
|
411 | + Response::error($code,$message); |
|
412 | + Response::send(); |
|
413 | + exit; |
|
414 | 414 | } |
415 | 415 | |
416 | 416 | /** |
@@ -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',true)){ |
|
425 | + $__deferred_send = new Deferred(function() { |
|
426 | + if (Options::get('core.response.autosend', true)) { |
|
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; |
@@ -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 get($URLPattern, $callback = null){ |
|
194 | + public static function get($URLPattern, $callback = null) { |
|
191 | 195 | return (new Route($URLPattern,$callback))->via('get'); |
192 | 196 | } |
193 | 197 | |
@@ -197,7 +201,7 @@ discard block |
||
197 | 201 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
198 | 202 | * @return Route |
199 | 203 | */ |
200 | - public static function post($URLPattern, $callback = null){ |
|
204 | + public static function post($URLPattern, $callback = null) { |
|
201 | 205 | return (new Route($URLPattern,$callback))->via('post'); |
202 | 206 | } |
203 | 207 | |
@@ -207,7 +211,7 @@ discard block |
||
207 | 211 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
208 | 212 | * @return Route |
209 | 213 | */ |
210 | - public static function any($URLPattern, $callback = null){ |
|
214 | + public static function any($URLPattern, $callback = null) { |
|
211 | 215 | return (new Route($URLPattern,$callback))->via('*'); |
212 | 216 | } |
213 | 217 | |
@@ -253,7 +257,7 @@ discard block |
||
253 | 257 | */ |
254 | 258 | public function & via(){ |
255 | 259 | $this->methods = []; |
256 | - foreach (func_get_args() as $method){ |
|
260 | + foreach (func_get_args() as $method) { |
|
257 | 261 | $this->methods[strtolower($method)] = true; |
258 | 262 | } |
259 | 263 | return $this; |
@@ -275,7 +279,7 @@ discard block |
||
275 | 279 | * @return Route |
276 | 280 | */ |
277 | 281 | public function & rules(array $rules){ |
278 | - foreach ((array)$rules as $varname => $rule){ |
|
282 | + foreach ((array)$rules as $varname => $rule) { |
|
279 | 283 | $this->rules[$varname] = $rule; |
280 | 284 | } |
281 | 285 | $this->pattern = $this->compilePatternAsRegex($this->URLPattern,$this->rules); |
@@ -304,7 +308,9 @@ discard block |
||
304 | 308 | $route->callback = []; |
305 | 309 | foreach ($callbacks as $method => $callback) { |
306 | 310 | $method = strtolower($method); |
307 | - if (Request::method() !== $method) continue; |
|
311 | + if (Request::method() !== $method) { |
|
312 | + continue; |
|
313 | + } |
|
308 | 314 | $route->callback[$method] = $callback; |
309 | 315 | $route->methods[$method] = 1; |
310 | 316 | } |
@@ -316,8 +322,8 @@ discard block |
||
316 | 322 | * @param string $pattern The URL schema. |
317 | 323 | * @return string The compiled PREG RegEx. |
318 | 324 | */ |
319 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
320 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
325 | + protected static function compilePatternAsRegex($pattern, $rules=[]) { |
|
326 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules) { |
|
321 | 327 | return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
322 | 328 | },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
323 | 329 | } |
@@ -329,12 +335,16 @@ discard block |
||
329 | 335 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
330 | 336 | * @return array The extracted variables |
331 | 337 | */ |
332 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
338 | + protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false) { |
|
333 | 339 | $URL = $URL ?: Request::URI(); |
334 | 340 | $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
335 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
341 | + if ( !preg_match($pattern,$URL,$args) ) { |
|
342 | + return false; |
|
343 | + } |
|
336 | 344 | foreach ($args as $key => $value) { |
337 | - if (false === is_string($key)) unset($args[$key]); |
|
345 | + if (false === is_string($key)) { |
|
346 | + unset($args[$key]); |
|
347 | + } |
|
338 | 348 | } |
339 | 349 | return $args; |
340 | 350 | } |
@@ -344,7 +354,7 @@ discard block |
||
344 | 354 | * @param string $pattern The URL schema. |
345 | 355 | * @return boolean |
346 | 356 | */ |
347 | - protected static function isDynamic($pattern){ |
|
357 | + protected static function isDynamic($pattern) { |
|
348 | 358 | return strlen($pattern) != strcspn($pattern,':(?[*+'); |
349 | 359 | } |
350 | 360 | |
@@ -353,8 +363,10 @@ discard block |
||
353 | 363 | * @param Route $r |
354 | 364 | * @return Route |
355 | 365 | */ |
356 | - public static function add($r){ |
|
357 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
366 | + public static function add($r) { |
|
367 | + if ( isset(static::$group[0]) ) { |
|
368 | + static::$group[0]->add($r); |
|
369 | + } |
|
358 | 370 | return static::$routes[implode('',static::$prefix)][] = $r; |
359 | 371 | } |
360 | 372 | |
@@ -363,41 +375,55 @@ discard block |
||
363 | 375 | * @param string $prefix The url prefix for the internal route definitions. |
364 | 376 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
365 | 377 | */ |
366 | - public static function group($prefix,$callback=null){ |
|
378 | + public static function group($prefix,$callback=null) { |
|
367 | 379 | |
368 | 380 | // Skip definition if current request doesn't match group. |
369 | 381 | |
370 | 382 | $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
371 | 383 | |
372 | - if ( static::isDynamic($prefix) ){ |
|
384 | + if ( static::isDynamic($prefix) ) { |
|
373 | 385 | |
374 | 386 | // Dynamic group, capture vars |
375 | 387 | $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete),null,true); |
376 | 388 | |
377 | 389 | // Errors in compile pattern or variable extraction, aborting. |
378 | - if (false === $vars) return; |
|
390 | + if (false === $vars) { |
|
391 | + return; |
|
392 | + } |
|
379 | 393 | |
380 | 394 | static::$prefix[] = $prefix; |
381 | - if (empty(static::$group)) static::$group = []; |
|
395 | + if (empty(static::$group)) { |
|
396 | + static::$group = []; |
|
397 | + } |
|
382 | 398 | array_unshift(static::$group, new RouteGroup()); |
383 | - if ($callback) call_user_func_array($callback,$vars); |
|
399 | + if ($callback) { |
|
400 | + call_user_func_array($callback,$vars); |
|
401 | + } |
|
384 | 402 | $group = static::$group[0]; |
385 | 403 | array_shift(static::$group); |
386 | 404 | array_pop(static::$prefix); |
387 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
405 | + if (empty(static::$prefix)) { |
|
406 | + static::$prefix=['']; |
|
407 | + } |
|
388 | 408 | return $group; |
389 | 409 | |
390 | - } else if ( 0 === strpos(Request::URI(), $prefix_complete) ){ |
|
410 | + } else if ( 0 === strpos(Request::URI(), $prefix_complete) ) { |
|
391 | 411 | |
392 | 412 | // Static group |
393 | 413 | static::$prefix[] = $prefix; |
394 | - if (empty(static::$group)) static::$group = []; |
|
414 | + if (empty(static::$group)) { |
|
415 | + static::$group = []; |
|
416 | + } |
|
395 | 417 | array_unshift(static::$group, new RouteGroup()); |
396 | - if ($callback) call_user_func($callback); |
|
418 | + if ($callback) { |
|
419 | + call_user_func($callback); |
|
420 | + } |
|
397 | 421 | $group = static::$group[0]; |
398 | 422 | array_shift(static::$group); |
399 | 423 | array_pop(static::$prefix); |
400 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
424 | + if (empty(static::$prefix)) { |
|
425 | + static::$prefix=['']; |
|
426 | + } |
|
401 | 427 | return $group; |
402 | 428 | } else { |
403 | 429 | |
@@ -407,7 +433,7 @@ discard block |
||
407 | 433 | |
408 | 434 | } |
409 | 435 | |
410 | - public static function exitWithError($code,$message="Application Error"){ |
|
436 | + public static function exitWithError($code,$message="Application Error") { |
|
411 | 437 | Response::error($code,$message); |
412 | 438 | Response::send(); |
413 | 439 | exit; |
@@ -418,19 +444,23 @@ discard block |
||
418 | 444 | * @param string $URL The URL to match onto. |
419 | 445 | * @return boolean true if a route callback was executed. |
420 | 446 | */ |
421 | - public static function dispatch($URL=null,$method=null){ |
|
422 | - if (!$URL) $URL = Request::URI(); |
|
423 | - if (!$method) $method = Request::method(); |
|
447 | + public static function dispatch($URL=null,$method=null) { |
|
448 | + if (!$URL) { |
|
449 | + $URL = Request::URI(); |
|
450 | + } |
|
451 | + if (!$method) { |
|
452 | + $method = Request::method(); |
|
453 | + } |
|
424 | 454 | |
425 | - $__deferred_send = new Deferred(function(){ |
|
426 | - if (Options::get('core.response.autosend',true)){ |
|
455 | + $__deferred_send = new Deferred(function() { |
|
456 | + if (Options::get('core.response.autosend',true)) { |
|
427 | 457 | Response::send(); |
428 | 458 | } |
429 | 459 | }); |
430 | 460 | |
431 | - foreach ((array)static::$routes as $group => $routes){ |
|
461 | + foreach ((array)static::$routes as $group => $routes) { |
|
432 | 462 | foreach ($routes as $route) { |
433 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
463 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))) { |
|
434 | 464 | $route->run($args,$method); |
435 | 465 | return true; |
436 | 466 | } |
@@ -446,34 +476,36 @@ discard block |
||
446 | 476 | class RouteGroup { |
447 | 477 | protected $routes; |
448 | 478 | |
449 | - public function __construct(){ |
|
479 | + public function __construct() { |
|
450 | 480 | $this->routes = new SplObjectStorage; |
451 | 481 | return Route::add($this); |
452 | 482 | } |
453 | 483 | |
454 | - public function has($r){ |
|
484 | + public function has($r) { |
|
455 | 485 | return $this->routes->contains($r); |
456 | 486 | } |
457 | 487 | |
458 | - public function add($r){ |
|
488 | + public function add($r) { |
|
459 | 489 | $this->routes->attach($r); |
460 | 490 | return $this; |
461 | 491 | } |
462 | 492 | |
463 | - public function remove($r){ |
|
464 | - if ($this->routes->contains($r)) $this->routes->detach($r); |
|
493 | + public function remove($r) { |
|
494 | + if ($this->routes->contains($r)) { |
|
495 | + $this->routes->detach($r); |
|
496 | + } |
|
465 | 497 | return $this; |
466 | 498 | } |
467 | 499 | |
468 | - public function before($callbacks){ |
|
469 | - foreach ($this->routes as $route){ |
|
500 | + public function before($callbacks) { |
|
501 | + foreach ($this->routes as $route) { |
|
470 | 502 | $route->before($callbacks); |
471 | 503 | } |
472 | 504 | return $this; |
473 | 505 | } |
474 | 506 | |
475 | - public function after($callbacks){ |
|
476 | - foreach ($this->routes as $route){ |
|
507 | + public function after($callbacks) { |
|
508 | + foreach ($this->routes as $route) { |
|
477 | 509 | $route->after($callbacks); |
478 | 510 | } |
479 | 511 | return $this; |