@@ -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,20 +398,20 @@ 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 | - $__deferred_send = new Deferred(function(){ |
|
| 406 | - if (Options::get('core.response.autosend',false)){ |
|
| 405 | + $__deferred_send = new Deferred(function() { |
|
| 406 | + if (Options::get('core.response.autosend', false)) { |
|
| 407 | 407 | Response::send(); |
| 408 | 408 | } |
| 409 | 409 | }); |
| 410 | 410 | |
| 411 | - foreach ((array)static::$routes as $group => $routes){ |
|
| 411 | + foreach ((array) static::$routes as $group => $routes) { |
|
| 412 | 412 | foreach ($routes as $route) { |
| 413 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
| 414 | - $route->run($args,$method); |
|
| 413 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL, $method))) { |
|
| 414 | + $route->run($args, $method); |
|
| 415 | 415 | return true; |
| 416 | 416 | } |
| 417 | 417 | } |
@@ -426,34 +426,34 @@ discard block |
||
| 426 | 426 | class RouteGroup { |
| 427 | 427 | protected $routes; |
| 428 | 428 | |
| 429 | - public function __construct(){ |
|
| 429 | + public function __construct() { |
|
| 430 | 430 | $this->routes = new SplObjectStorage; |
| 431 | 431 | return Route::add($this); |
| 432 | 432 | } |
| 433 | 433 | |
| 434 | - public function has($r){ |
|
| 434 | + public function has($r) { |
|
| 435 | 435 | return $this->routes->contains($r); |
| 436 | 436 | } |
| 437 | 437 | |
| 438 | - public function add($r){ |
|
| 438 | + public function add($r) { |
|
| 439 | 439 | $this->routes->attach($r); |
| 440 | 440 | return $this; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | - public function remove($r){ |
|
| 443 | + public function remove($r) { |
|
| 444 | 444 | if ($this->routes->contains($r)) $this->routes->detach($r); |
| 445 | 445 | return $this; |
| 446 | 446 | } |
| 447 | 447 | |
| 448 | - public function before($callbacks){ |
|
| 449 | - foreach ($this->routes as $route){ |
|
| 448 | + public function before($callbacks) { |
|
| 449 | + foreach ($this->routes as $route) { |
|
| 450 | 450 | $route->before($callbacks); |
| 451 | 451 | } |
| 452 | 452 | return $this; |
| 453 | 453 | } |
| 454 | 454 | |
| 455 | - public function after($callbacks){ |
|
| 456 | - foreach ($this->routes as $route){ |
|
| 455 | + public function after($callbacks) { |
|
| 456 | + foreach ($this->routes as $route) { |
|
| 457 | 457 | $route->after($callbacks); |
| 458 | 458 | } |
| 459 | 459 | 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,19 +424,23 @@ 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 | - $__deferred_send = new Deferred(function(){ |
|
| 406 | - if (Options::get('core.response.autosend',false)){ |
|
| 435 | + $__deferred_send = new Deferred(function() { |
|
| 436 | + if (Options::get('core.response.autosend',false)) { |
|
| 407 | 437 | Response::send(); |
| 408 | 438 | } |
| 409 | 439 | }); |
| 410 | 440 | |
| 411 | - foreach ((array)static::$routes as $group => $routes){ |
|
| 441 | + foreach ((array)static::$routes as $group => $routes) { |
|
| 412 | 442 | foreach ($routes as $route) { |
| 413 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
| 443 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))) { |
|
| 414 | 444 | $route->run($args,$method); |
| 415 | 445 | return true; |
| 416 | 446 | } |
@@ -426,34 +456,36 @@ discard block |
||
| 426 | 456 | class RouteGroup { |
| 427 | 457 | protected $routes; |
| 428 | 458 | |
| 429 | - public function __construct(){ |
|
| 459 | + public function __construct() { |
|
| 430 | 460 | $this->routes = new SplObjectStorage; |
| 431 | 461 | return Route::add($this); |
| 432 | 462 | } |
| 433 | 463 | |
| 434 | - public function has($r){ |
|
| 464 | + public function has($r) { |
|
| 435 | 465 | return $this->routes->contains($r); |
| 436 | 466 | } |
| 437 | 467 | |
| 438 | - public function add($r){ |
|
| 468 | + public function add($r) { |
|
| 439 | 469 | $this->routes->attach($r); |
| 440 | 470 | return $this; |
| 441 | 471 | } |
| 442 | 472 | |
| 443 | - public function remove($r){ |
|
| 444 | - if ($this->routes->contains($r)) $this->routes->detach($r); |
|
| 473 | + public function remove($r) { |
|
| 474 | + if ($this->routes->contains($r)) { |
|
| 475 | + $this->routes->detach($r); |
|
| 476 | + } |
|
| 445 | 477 | return $this; |
| 446 | 478 | } |
| 447 | 479 | |
| 448 | - public function before($callbacks){ |
|
| 449 | - foreach ($this->routes as $route){ |
|
| 480 | + public function before($callbacks) { |
|
| 481 | + foreach ($this->routes as $route) { |
|
| 450 | 482 | $route->before($callbacks); |
| 451 | 483 | } |
| 452 | 484 | return $this; |
| 453 | 485 | } |
| 454 | 486 | |
| 455 | - public function after($callbacks){ |
|
| 456 | - foreach ($this->routes as $route){ |
|
| 487 | + public function after($callbacks) { |
|
| 488 | + foreach ($this->routes as $route) { |
|
| 457 | 489 | $route->after($callbacks); |
| 458 | 490 | } |
| 459 | 491 | return $this; |
@@ -29,11 +29,11 @@ discard block |
||
| 29 | 29 | * @param mixed $default (optional) The default value. If is a callable it will executed and the return value will be used. |
| 30 | 30 | * @return mixed The value of the key or the default (resolved) value if the key not existed. |
| 31 | 31 | */ |
| 32 | - public function get($key, $default=null){ |
|
| 33 | - if ($ptr =& $this->find($key,false)){ |
|
| 32 | + public function get($key, $default = null) { |
|
| 33 | + if ($ptr = & $this->find($key, false)) { |
|
| 34 | 34 | return $ptr; |
| 35 | 35 | } else { |
| 36 | - if ($default !== null){ |
|
| 36 | + if ($default !== null) { |
|
| 37 | 37 | return $this->set($key, is_callable($default) ? call_user_func($default) : $default); |
| 38 | 38 | } else { |
| 39 | 39 | return null; |
@@ -47,11 +47,11 @@ discard block |
||
| 47 | 47 | * @param mixed $value (optional) The value. If is a callable it will executed and the return value will be used. |
| 48 | 48 | * @return mixed The value of the key or the default (resolved) value if the key not existed. |
| 49 | 49 | */ |
| 50 | - public function set($key, $value=null){ |
|
| 50 | + public function set($key, $value = null) { |
|
| 51 | 51 | if (is_array($key)) { |
| 52 | 52 | return $this->merge($key); |
| 53 | 53 | } else { |
| 54 | - $ptr =& $this->find($key, true); |
|
| 54 | + $ptr = & $this->find($key, true); |
|
| 55 | 55 | return $ptr = $value; |
| 56 | 56 | } |
| 57 | 57 | } |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * @param string $key The key path in dot notation |
| 62 | 62 | * @param boolean $compact (optional) Compact map removing empty paths. |
| 63 | 63 | */ |
| 64 | - public function delete($key, $compact=true){ |
|
| 64 | + public function delete($key, $compact = true) { |
|
| 65 | 65 | $this->set($key, null); |
| 66 | 66 | if ($compact) $this->compact(); |
| 67 | 67 | } |
@@ -71,18 +71,18 @@ discard block |
||
| 71 | 71 | * @param string $key The key path in dot notation |
| 72 | 72 | * @return boolean |
| 73 | 73 | */ |
| 74 | - public function exists($key){ |
|
| 74 | + public function exists($key) { |
|
| 75 | 75 | return null !== $this->find($key, false); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * Clear all key path in map. |
| 80 | 80 | */ |
| 81 | - public function clear(){ |
|
| 81 | + public function clear() { |
|
| 82 | 82 | $this->fields = []; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - public function __construct($fields=null){ |
|
| 85 | + public function __construct($fields = null) { |
|
| 86 | 86 | $this->load($fields); |
| 87 | 87 | } |
| 88 | 88 | |
@@ -90,8 +90,8 @@ discard block |
||
| 90 | 90 | * Load an associative array/object as the map source. |
| 91 | 91 | * @param string $fields The array to merge |
| 92 | 92 | */ |
| 93 | - public function load($fields){ |
|
| 94 | - if ($fields) $this->fields = (array)$fields; |
|
| 93 | + public function load($fields) { |
|
| 94 | + if ($fields) $this->fields = (array) $fields; |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -99,17 +99,17 @@ discard block |
||
| 99 | 99 | * @param array $array The array to merge |
| 100 | 100 | * @param boolean $merge_back If `true` merge the map over the $array, if `false` (default) the reverse. |
| 101 | 101 | */ |
| 102 | - public function merge($array, $merge_back=false){ |
|
| 102 | + public function merge($array, $merge_back = false) { |
|
| 103 | 103 | $this->fields = $merge_back |
| 104 | - ? array_replace_recursive((array)$array, $this->fields) |
|
| 105 | - : array_replace_recursive($this->fields, (array)$array); |
|
| 104 | + ? array_replace_recursive((array) $array, $this->fields) |
|
| 105 | + : array_replace_recursive($this->fields, (array) $array); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | /** |
| 109 | 109 | * Compact map removing empty paths |
| 110 | 110 | */ |
| 111 | 111 | |
| 112 | - public function compact(){ |
|
| 112 | + public function compact() { |
|
| 113 | 113 | |
| 114 | 114 | $array_filter_rec = function($input, $callback = null) use (&$array_filter_rec) { |
| 115 | 115 | foreach ($input as &$value) { |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | return array_filter($input, $callback); |
| 121 | 121 | }; |
| 122 | 122 | |
| 123 | - $this->fields = $array_filter_rec($this->fields,function($a){ return $a !== null; }); |
|
| 123 | + $this->fields = $array_filter_rec($this->fields, function($a) { return $a !== null;}); |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
@@ -130,17 +130,17 @@ discard block |
||
| 130 | 130 | * @param callable If passed this callback will be applied to the founded value. |
| 131 | 131 | * @return mixed The founded value. |
| 132 | 132 | */ |
| 133 | - public function & find($path, $create=false, callable $operation=null) { |
|
| 134 | - $tok = strtok($path,'.'); |
|
| 133 | + public function & find($path, $create = false, callable $operation = null) { |
|
| 134 | + $tok = strtok($path, '.'); |
|
| 135 | 135 | |
| 136 | - if($create){ |
|
| 137 | - $value =& $this->fields; |
|
| 136 | + if ($create) { |
|
| 137 | + $value = & $this->fields; |
|
| 138 | 138 | } else { |
| 139 | 139 | $value = $this->fields; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - while($tok !== false){ |
|
| 143 | - $value =& $value[$tok]; |
|
| 142 | + while ($tok !== false) { |
|
| 143 | + $value = & $value[$tok]; |
|
| 144 | 144 | $tok = strtok('.'); |
| 145 | 145 | } |
| 146 | 146 | if (is_callable($operation)) $operation($value); |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | * |
| 155 | 155 | * @return string The json object |
| 156 | 156 | */ |
| 157 | - public function jsonSerialize(){ |
|
| 157 | + public function jsonSerialize() { |
|
| 158 | 158 | return $this->fields; |
| 159 | 159 | } |
| 160 | 160 | |
@@ -29,11 +29,11 @@ discard block |
||
| 29 | 29 | * @param mixed $default (optional) The default value. If is a callable it will executed and the return value will be used. |
| 30 | 30 | * @return mixed The value of the key or the default (resolved) value if the key not existed. |
| 31 | 31 | */ |
| 32 | - public function get($key, $default=null){ |
|
| 33 | - if ($ptr =& $this->find($key,false)){ |
|
| 32 | + public function get($key, $default=null) { |
|
| 33 | + if ($ptr =& $this->find($key,false)) { |
|
| 34 | 34 | return $ptr; |
| 35 | 35 | } else { |
| 36 | - if ($default !== null){ |
|
| 36 | + if ($default !== null) { |
|
| 37 | 37 | return $this->set($key, is_callable($default) ? call_user_func($default) : $default); |
| 38 | 38 | } else { |
| 39 | 39 | return null; |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | * @param mixed $value (optional) The value. If is a callable it will executed and the return value will be used. |
| 48 | 48 | * @return mixed The value of the key or the default (resolved) value if the key not existed. |
| 49 | 49 | */ |
| 50 | - public function set($key, $value=null){ |
|
| 50 | + public function set($key, $value=null) { |
|
| 51 | 51 | if (is_array($key)) { |
| 52 | 52 | return $this->merge($key); |
| 53 | 53 | } else { |
@@ -61,9 +61,11 @@ discard block |
||
| 61 | 61 | * @param string $key The key path in dot notation |
| 62 | 62 | * @param boolean $compact (optional) Compact map removing empty paths. |
| 63 | 63 | */ |
| 64 | - public function delete($key, $compact=true){ |
|
| 64 | + public function delete($key, $compact=true) { |
|
| 65 | 65 | $this->set($key, null); |
| 66 | - if ($compact) $this->compact(); |
|
| 66 | + if ($compact) { |
|
| 67 | + $this->compact(); |
|
| 68 | + } |
|
| 67 | 69 | } |
| 68 | 70 | |
| 69 | 71 | /** |
@@ -71,18 +73,18 @@ discard block |
||
| 71 | 73 | * @param string $key The key path in dot notation |
| 72 | 74 | * @return boolean |
| 73 | 75 | */ |
| 74 | - public function exists($key){ |
|
| 76 | + public function exists($key) { |
|
| 75 | 77 | return null !== $this->find($key, false); |
| 76 | 78 | } |
| 77 | 79 | |
| 78 | 80 | /** |
| 79 | 81 | * Clear all key path in map. |
| 80 | 82 | */ |
| 81 | - public function clear(){ |
|
| 83 | + public function clear() { |
|
| 82 | 84 | $this->fields = []; |
| 83 | 85 | } |
| 84 | 86 | |
| 85 | - public function __construct($fields=null){ |
|
| 87 | + public function __construct($fields=null) { |
|
| 86 | 88 | $this->load($fields); |
| 87 | 89 | } |
| 88 | 90 | |
@@ -90,8 +92,10 @@ discard block |
||
| 90 | 92 | * Load an associative array/object as the map source. |
| 91 | 93 | * @param string $fields The array to merge |
| 92 | 94 | */ |
| 93 | - public function load($fields){ |
|
| 94 | - if ($fields) $this->fields = (array)$fields; |
|
| 95 | + public function load($fields) { |
|
| 96 | + if ($fields) { |
|
| 97 | + $this->fields = (array)$fields; |
|
| 98 | + } |
|
| 95 | 99 | } |
| 96 | 100 | |
| 97 | 101 | /** |
@@ -99,7 +103,7 @@ discard block |
||
| 99 | 103 | * @param array $array The array to merge |
| 100 | 104 | * @param boolean $merge_back If `true` merge the map over the $array, if `false` (default) the reverse. |
| 101 | 105 | */ |
| 102 | - public function merge($array, $merge_back=false){ |
|
| 106 | + public function merge($array, $merge_back=false) { |
|
| 103 | 107 | $this->fields = $merge_back |
| 104 | 108 | ? array_replace_recursive((array)$array, $this->fields) |
| 105 | 109 | : array_replace_recursive($this->fields, (array)$array); |
@@ -109,7 +113,7 @@ discard block |
||
| 109 | 113 | * Compact map removing empty paths |
| 110 | 114 | */ |
| 111 | 115 | |
| 112 | - public function compact(){ |
|
| 116 | + public function compact() { |
|
| 113 | 117 | |
| 114 | 118 | $array_filter_rec = function($input, $callback = null) use (&$array_filter_rec) { |
| 115 | 119 | foreach ($input as &$value) { |
@@ -133,17 +137,19 @@ discard block |
||
| 133 | 137 | public function & find($path, $create=false, callable $operation=null) { |
| 134 | 138 | $tok = strtok($path,'.'); |
| 135 | 139 | |
| 136 | - if($create){ |
|
| 140 | + if($create) { |
|
| 137 | 141 | $value =& $this->fields; |
| 138 | 142 | } else { |
| 139 | 143 | $value = $this->fields; |
| 140 | 144 | } |
| 141 | 145 | |
| 142 | - while($tok !== false){ |
|
| 146 | + while($tok !== false) { |
|
| 143 | 147 | $value =& $value[$tok]; |
| 144 | 148 | $tok = strtok('.'); |
| 145 | 149 | } |
| 146 | - if (is_callable($operation)) $operation($value); |
|
| 150 | + if (is_callable($operation)) { |
|
| 151 | + $operation($value); |
|
| 152 | + } |
|
| 147 | 153 | return $value; |
| 148 | 154 | } |
| 149 | 155 | |
@@ -154,7 +160,7 @@ discard block |
||
| 154 | 160 | * |
| 155 | 161 | * @return string The json object |
| 156 | 162 | */ |
| 157 | - public function jsonSerialize(){ |
|
| 163 | + public function jsonSerialize() { |
|
| 158 | 164 | return $this->fields; |
| 159 | 165 | } |
| 160 | 166 | |
@@ -23,13 +23,13 @@ discard block |
||
| 23 | 23 | TYPE_BIN = 'application/octet-stream'; |
| 24 | 24 | |
| 25 | 25 | protected static $payload = [], |
| 26 | - $status = 200, |
|
| 27 | - $charset = "utf-8", |
|
| 28 | - $headers = ['Content-Type' => ['text/html; charset=utf-8']], |
|
| 29 | - $buffer = null, |
|
| 30 | - $force_dl = false, |
|
| 31 | - $link = null, |
|
| 32 | - $sent = false; |
|
| 26 | + $status = 200, |
|
| 27 | + $charset = "utf-8", |
|
| 28 | + $headers = ['Content-Type' => ['text/html; charset=utf-8']], |
|
| 29 | + $buffer = null, |
|
| 30 | + $force_dl = false, |
|
| 31 | + $link = null, |
|
| 32 | + $sent = false; |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | 35 | public static function charset($charset){ |
@@ -187,12 +187,12 @@ discard block |
||
| 187 | 187 | if ($setBody) static::$payload = [$setBody]; |
| 188 | 188 | return Filter::with('core.response.body', |
| 189 | 189 | is_array(static::$payload) ? implode('',static::$payload) : static::$payload |
| 190 | - ); |
|
| 190 | + ); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | public static function headers($setHeaders=null){ |
| 194 | - if ($setHeaders) static::$headers = $setHeaders; |
|
| 195 | - return static::$headers; |
|
| 194 | + if ($setHeaders) static::$headers = $setHeaders; |
|
| 195 | + return static::$headers; |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -32,12 +32,12 @@ discard block |
||
| 32 | 32 | $sent = false; |
| 33 | 33 | |
| 34 | 34 | |
| 35 | - public static function charset($charset){ |
|
| 35 | + public static function charset($charset) { |
|
| 36 | 36 | static::$charset = $charset; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - public static function type($mime){ |
|
| 40 | - static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : '')); |
|
| 39 | + public static function type($mime) { |
|
| 40 | + static::header('Content-Type', $mime.(static::$charset ? '; charset='.static::$charset : '')); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -45,39 +45,39 @@ discard block |
||
| 45 | 45 | * @param string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content |
| 46 | 46 | * @return [type] [description] |
| 47 | 47 | */ |
| 48 | - public static function download($filename){ |
|
| 48 | + public static function download($filename) { |
|
| 49 | 49 | static::$force_dl = $filename; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * Start capturing output |
| 54 | 54 | */ |
| 55 | - public static function start(){ |
|
| 55 | + public static function start() { |
|
| 56 | 56 | static::$buffer = ob_start(); |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | 60 | * Enable CORS HTTP headers. |
| 61 | 61 | */ |
| 62 | - public static function enableCORS(){ |
|
| 62 | + public static function enableCORS() { |
|
| 63 | 63 | |
| 64 | 64 | // Allow from any origin |
| 65 | - if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) { |
|
| 65 | + if ($origin = filter_input(INPUT_SERVER, 'HTTP_ORIGIN')) { |
|
| 66 | 66 | static::header('Access-Control-Allow-Origin', $origin); |
| 67 | 67 | static::header('Access-Control-Allow-Credentials', 'true'); |
| 68 | 68 | static::header('Access-Control-Max-Age', 86400); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | // Access-Control headers are received during OPTIONS requests |
| 72 | - if (filter_input(INPUT_SERVER,'REQUEST_METHOD') == 'OPTIONS') { |
|
| 72 | + if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') == 'OPTIONS') { |
|
| 73 | 73 | static::clean(); |
| 74 | 74 | |
| 75 | - if (filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) { |
|
| 75 | + if (filter_input(INPUT_SERVER, 'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) { |
|
| 76 | 76 | static::header('Access-Control-Allow-Methods', |
| 77 | 77 | 'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, PATCH, TRACE'); |
| 78 | 78 | } |
| 79 | - if ($req_h = filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) { |
|
| 80 | - static::header('Access-Control-Allow-Headers',$req_h); |
|
| 79 | + if ($req_h = filter_input(INPUT_SERVER, 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) { |
|
| 80 | + static::header('Access-Control-Allow-Headers', $req_h); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | static::send(); |
@@ -93,8 +93,8 @@ discard block |
||
| 93 | 93 | * Finish the output buffer capturing. |
| 94 | 94 | * @return string The captured buffer |
| 95 | 95 | */ |
| 96 | - public static function end(){ |
|
| 97 | - if (static::$buffer){ |
|
| 96 | + public static function end() { |
|
| 97 | + if (static::$buffer) { |
|
| 98 | 98 | static::$payload[] = ob_get_contents(); |
| 99 | 99 | ob_end_clean(); |
| 100 | 100 | static::$buffer = null; |
@@ -106,14 +106,14 @@ discard block |
||
| 106 | 106 | * Check if an response output buffering is active. |
| 107 | 107 | * @return boolean |
| 108 | 108 | */ |
| 109 | - public static function isBuffering(){ |
|
| 109 | + public static function isBuffering() { |
|
| 110 | 110 | return static::$buffer; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | 114 | * Clear the response body |
| 115 | 115 | */ |
| 116 | - public static function clean(){ |
|
| 116 | + public static function clean() { |
|
| 117 | 117 | static::$payload = []; |
| 118 | 118 | } |
| 119 | 119 | |
@@ -121,76 +121,76 @@ discard block |
||
| 121 | 121 | * Append a JSON object to the buffer. |
| 122 | 122 | * @param mixed $payload Data to append to the response buffer |
| 123 | 123 | */ |
| 124 | - public static function json($payload){ |
|
| 124 | + public static function json($payload) { |
|
| 125 | 125 | static::type(static::TYPE_JSON); |
| 126 | - static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK)); |
|
| 126 | + static::$payload[] = json_encode($payload, Options::get('core.response.json_flags', JSON_NUMERIC_CHECK)); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | /** |
| 130 | 130 | * Append a text to the buffer. |
| 131 | 131 | * @param mixed $payload Text to append to the response buffer |
| 132 | 132 | */ |
| 133 | - public static function text(){ |
|
| 133 | + public static function text() { |
|
| 134 | 134 | static::type(static::TYPE_TEXT); |
| 135 | - static::$payload[] = implode('',func_get_args()); |
|
| 135 | + static::$payload[] = implode('', func_get_args()); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
| 139 | 139 | * Append an XML string to the buffer. |
| 140 | 140 | * @param mixed $payload Data to append to the response buffer |
| 141 | 141 | */ |
| 142 | - public static function xml(){ |
|
| 142 | + public static function xml() { |
|
| 143 | 143 | static::type(static::TYPE_XML); |
| 144 | - static::$payload[] = implode('',func_get_args()); |
|
| 144 | + static::$payload[] = implode('', func_get_args()); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
| 148 | 148 | * Append a SVG string to the buffer. |
| 149 | 149 | * @param mixed $payload Data to append to the response buffer |
| 150 | 150 | */ |
| 151 | - public static function svg(){ |
|
| 151 | + public static function svg() { |
|
| 152 | 152 | static::type(static::TYPE_SVG); |
| 153 | - static::$payload[] = implode('',func_get_args()); |
|
| 153 | + static::$payload[] = implode('', func_get_args()); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /** |
| 157 | 157 | * Append an HTML string to the buffer. |
| 158 | 158 | * @param mixed $payload Data to append to the response buffer |
| 159 | 159 | */ |
| 160 | - public static function html(){ |
|
| 160 | + public static function html() { |
|
| 161 | 161 | static::type(static::TYPE_HTML); |
| 162 | - static::$payload[] = implode('',func_get_args()); |
|
| 162 | + static::$payload[] = implode('', func_get_args()); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
| 166 | 166 | * Append a raw string to the buffer. |
| 167 | 167 | * @param mixed $payload Data to append to the response buffer |
| 168 | 168 | */ |
| 169 | - public static function add(){ |
|
| 170 | - static::$payload[] = implode('',func_get_args()); |
|
| 169 | + public static function add() { |
|
| 170 | + static::$payload[] = implode('', func_get_args()); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - public static function status($code,$message=''){ |
|
| 174 | - static::header('Status',$message?:$code,$code); |
|
| 173 | + public static function status($code, $message = '') { |
|
| 174 | + static::header('Status', $message ?: $code, $code); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | - public static function header($name,$value,$code=null){ |
|
| 178 | - static::$headers[$name] = [$value,$code]; |
|
| 177 | + public static function header($name, $value, $code = null) { |
|
| 178 | + static::$headers[$name] = [$value, $code]; |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - public static function error($code=500,$message='Application Error'){ |
|
| 182 | - Event::trigger('core.response.error',$code,$message); |
|
| 183 | - static::status($code,$message); |
|
| 181 | + public static function error($code = 500, $message = 'Application Error') { |
|
| 182 | + Event::trigger('core.response.error', $code, $message); |
|
| 183 | + static::status($code, $message); |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | - public static function body($setBody=null){ |
|
| 186 | + public static function body($setBody = null) { |
|
| 187 | 187 | if ($setBody) static::$payload = [$setBody]; |
| 188 | 188 | return Filter::with('core.response.body', |
| 189 | - is_array(static::$payload) ? implode('',static::$payload) : static::$payload |
|
| 189 | + is_array(static::$payload) ? implode('', static::$payload) : static::$payload |
|
| 190 | 190 | ); |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | - public static function headers($setHeaders=null){ |
|
| 193 | + public static function headers($setHeaders = null) { |
|
| 194 | 194 | if ($setHeaders) static::$headers = $setHeaders; |
| 195 | 195 | return static::$headers; |
| 196 | 196 | } |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | * |
| 203 | 203 | * @return array Headers and body of the response |
| 204 | 204 | */ |
| 205 | - public static function save(){ |
|
| 205 | + public static function save() { |
|
| 206 | 206 | return [ |
| 207 | 207 | 'head' => static::$headers, |
| 208 | 208 | 'body' => static::body(), |
@@ -216,13 +216,13 @@ discard block |
||
| 216 | 216 | * |
| 217 | 217 | * @param array $data head/body saved state |
| 218 | 218 | */ |
| 219 | - public static function load($data){ |
|
| 220 | - $data = (object)$data; |
|
| 219 | + public static function load($data) { |
|
| 220 | + $data = (object) $data; |
|
| 221 | 221 | if (isset($data->head)) static::headers($data->head); |
| 222 | 222 | if (isset($data->body)) static::body($data->body); |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | - public static function send($force = false){ |
|
| 225 | + public static function send($force = false) { |
|
| 226 | 226 | if (!static::$sent || $force) { |
| 227 | 227 | static::$sent = true; |
| 228 | 228 | Event::trigger('core.response.send'); |
@@ -235,8 +235,8 @@ discard block |
||
| 235 | 235 | $code = null; |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | - if ($value == 'Status'){ |
|
| 239 | - if (function_exists('http_response_code')){ |
|
| 238 | + if ($value == 'Status') { |
|
| 239 | + if (function_exists('http_response_code')) { |
|
| 240 | 240 | http_response_code($code); |
| 241 | 241 | } else { |
| 242 | 242 | header("Status: $code", true, $code); |