@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | trait Module { |
| 14 | 14 | static protected $__PROTOTYPE = array(); |
| 15 | 15 | |
| 16 | - final public function __call($name, $args){ |
|
| 16 | + final public function __call($name, $args) { |
|
| 17 | 17 | if (isset(static::$__PROTOTYPE[$name]) && static::$__PROTOTYPE[$name] instanceof \Closure) |
| 18 | 18 | return call_user_func_array(static::$__PROTOTYPE[$name]->bindTo($this, $this), $args); |
| 19 | 19 | if (get_parent_class()) |
@@ -21,14 +21,14 @@ discard block |
||
| 21 | 21 | else throw new \BadMethodCallException; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - final public static function __callStatic($name, $args){ |
|
| 24 | + final public static function __callStatic($name, $args) { |
|
| 25 | 25 | if (isset(static::$__PROTOTYPE[$name]) && static::$__PROTOTYPE[$name] instanceof \Closure) |
| 26 | 26 | return forward_static_call_array(static::$__PROTOTYPE[$name], $args); |
| 27 | 27 | if (get_parent_class()) return parent::__callStatic($name, $args); |
| 28 | 28 | else throw new \BadMethodCallException; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - public static function extend($methods = []){ |
|
| 31 | + public static function extend($methods = []) { |
|
| 32 | 32 | if ($methods) foreach ($methods as $name => $method) { |
| 33 | 33 | if ($method && $method instanceof \Closure) |
| 34 | 34 | static::$__PROTOTYPE[$name] = $method; |
@@ -13,26 +13,36 @@ |
||
| 13 | 13 | trait Module { |
| 14 | 14 | static protected $__PROTOTYPE = array(); |
| 15 | 15 | |
| 16 | - final public function __call($name, $args){ |
|
| 17 | - if (isset(static::$__PROTOTYPE[$name]) && static::$__PROTOTYPE[$name] instanceof \Closure) |
|
| 18 | - return call_user_func_array(static::$__PROTOTYPE[$name]->bindTo($this, $this), $args); |
|
| 19 | - if (get_parent_class()) |
|
| 20 | - return parent::__call($name, $args); |
|
| 21 | - else throw new \BadMethodCallException; |
|
| 16 | + final public function __call($name, $args) { |
|
| 17 | + if (isset(static::$__PROTOTYPE[$name]) && static::$__PROTOTYPE[$name] instanceof \Closure) { |
|
| 18 | + return call_user_func_array(static::$__PROTOTYPE[$name]->bindTo($this, $this), $args); |
|
| 19 | + } |
|
| 20 | + if (get_parent_class()) { |
|
| 21 | + return parent::__call($name, $args); |
|
| 22 | + } else { |
|
| 23 | + throw new \BadMethodCallException; |
|
| 24 | + } |
|
| 22 | 25 | } |
| 23 | 26 | |
| 24 | - final public static function __callStatic($name, $args){ |
|
| 25 | - if (isset(static::$__PROTOTYPE[$name]) && static::$__PROTOTYPE[$name] instanceof \Closure) |
|
| 26 | - return forward_static_call_array(static::$__PROTOTYPE[$name], $args); |
|
| 27 | - if (get_parent_class()) return parent::__callStatic($name, $args); |
|
| 28 | - else throw new \BadMethodCallException; |
|
| 27 | + final public static function __callStatic($name, $args) { |
|
| 28 | + if (isset(static::$__PROTOTYPE[$name]) && static::$__PROTOTYPE[$name] instanceof \Closure) { |
|
| 29 | + return forward_static_call_array(static::$__PROTOTYPE[$name], $args); |
|
| 30 | + } |
|
| 31 | + if (get_parent_class()) { |
|
| 32 | + return parent::__callStatic($name, $args); |
|
| 33 | + } else { |
|
| 34 | + throw new \BadMethodCallException; |
|
| 35 | + } |
|
| 29 | 36 | } |
| 30 | 37 | |
| 31 | - public static function extend($methods = []){ |
|
| 32 | - if ($methods) foreach ($methods as $name => $method) { |
|
| 38 | + public static function extend($methods = []) { |
|
| 39 | + if ($methods) { |
|
| 40 | + foreach ($methods as $name => $method) { |
|
| 33 | 41 | if ($method && $method instanceof \Closure) |
| 34 | 42 | static::$__PROTOTYPE[$name] = $method; |
| 35 | - else throw new \BadMethodCallException; |
|
| 43 | + } else { |
|
| 44 | + throw new \BadMethodCallException; |
|
| 45 | + } |
|
| 36 | 46 | } |
| 37 | 47 | } |
| 38 | 48 | |
@@ -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,45 +121,45 @@ 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 | /** |
@@ -172,8 +172,8 @@ discard block |
||
| 172 | 172 | * |
| 173 | 173 | * @param mixed $payload Data to append to the response buffer |
| 174 | 174 | */ |
| 175 | - public static function add(){ |
|
| 176 | - foreach(func_get_args() as $data){ |
|
| 175 | + public static function add() { |
|
| 176 | + foreach (func_get_args() as $data) { |
|
| 177 | 177 | switch (true) { |
| 178 | 178 | case is_callable($data) : |
| 179 | 179 | return static::add($data()); |
@@ -187,27 +187,27 @@ discard block |
||
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - public static function status($code,$message=''){ |
|
| 191 | - static::header('Status',$message?:$code,$code); |
|
| 190 | + public static function status($code, $message = '') { |
|
| 191 | + static::header('Status', $message ?: $code, $code); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | - public static function header($name,$value,$code=null){ |
|
| 195 | - static::$headers[$name] = [$value,$code]; |
|
| 194 | + public static function header($name, $value, $code = null) { |
|
| 195 | + static::$headers[$name] = [$value, $code]; |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - public static function error($code=500,$message='Application Error'){ |
|
| 199 | - Event::trigger('core.response.error',$code,$message); |
|
| 200 | - static::status($code,$message); |
|
| 198 | + public static function error($code = 500, $message = 'Application Error') { |
|
| 199 | + Event::trigger('core.response.error', $code, $message); |
|
| 200 | + static::status($code, $message); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | - public static function body($setBody=null){ |
|
| 203 | + public static function body($setBody = null) { |
|
| 204 | 204 | if ($setBody) static::$payload = [$setBody]; |
| 205 | 205 | return Filter::with('core.response.body', |
| 206 | - is_array(static::$payload) ? implode('',static::$payload) : static::$payload |
|
| 206 | + is_array(static::$payload) ? implode('', static::$payload) : static::$payload |
|
| 207 | 207 | ); |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | - public static function headers($setHeaders=null){ |
|
| 210 | + public static function headers($setHeaders = null) { |
|
| 211 | 211 | if ($setHeaders) static::$headers = $setHeaders; |
| 212 | 212 | return static::$headers; |
| 213 | 213 | } |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | * |
| 220 | 220 | * @return array Headers and body of the response |
| 221 | 221 | */ |
| 222 | - public static function save(){ |
|
| 222 | + public static function save() { |
|
| 223 | 223 | return [ |
| 224 | 224 | 'head' => static::$headers, |
| 225 | 225 | 'body' => static::body(), |
@@ -233,13 +233,13 @@ discard block |
||
| 233 | 233 | * |
| 234 | 234 | * @param array $data head/body saved state |
| 235 | 235 | */ |
| 236 | - public static function load($data){ |
|
| 237 | - $data = (object)$data; |
|
| 236 | + public static function load($data) { |
|
| 237 | + $data = (object) $data; |
|
| 238 | 238 | if (isset($data->head)) static::headers($data->head); |
| 239 | 239 | if (isset($data->body)) static::body($data->body); |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | - public static function send($force = false){ |
|
| 242 | + public static function send($force = false) { |
|
| 243 | 243 | if (!static::$sent || $force) { |
| 244 | 244 | static::$sent = true; |
| 245 | 245 | Event::trigger('core.response.send'); |
@@ -252,8 +252,8 @@ discard block |
||
| 252 | 252 | $code = null; |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | - if ($value == 'Status'){ |
|
| 256 | - if (function_exists('http_response_code')){ |
|
| 255 | + if ($value == 'Status') { |
|
| 256 | + if (function_exists('http_response_code')) { |
|
| 257 | 257 | http_response_code($code); |
| 258 | 258 | } else { |
| 259 | 259 | header("Status: $code", true, $code); |
@@ -32,11 +32,11 @@ 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){ |
|
| 39 | + public static function type($mime) { |
|
| 40 | 40 | static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : '')); |
| 41 | 41 | } |
| 42 | 42 | |
@@ -45,21 +45,21 @@ 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 | 65 | if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) { |
@@ -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,7 +121,7 @@ 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 | 126 | static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK)); |
| 127 | 127 | } |
@@ -130,7 +130,7 @@ discard block |
||
| 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 | 135 | static::$payload[] = implode('',func_get_args()); |
| 136 | 136 | } |
@@ -139,7 +139,7 @@ discard block |
||
| 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 | 144 | static::$payload[] = implode('',func_get_args()); |
| 145 | 145 | } |
@@ -148,7 +148,7 @@ discard block |
||
| 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 | 153 | static::$payload[] = implode('',func_get_args()); |
| 154 | 154 | } |
@@ -157,7 +157,7 @@ discard block |
||
| 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 | 162 | static::$payload[] = implode('',func_get_args()); |
| 163 | 163 | } |
@@ -172,8 +172,8 @@ discard block |
||
| 172 | 172 | * |
| 173 | 173 | * @param mixed $payload Data to append to the response buffer |
| 174 | 174 | */ |
| 175 | - public static function add(){ |
|
| 176 | - foreach(func_get_args() as $data){ |
|
| 175 | + public static function add() { |
|
| 176 | + foreach(func_get_args() as $data) { |
|
| 177 | 177 | switch (true) { |
| 178 | 178 | case is_callable($data) : |
| 179 | 179 | return static::add($data()); |
@@ -187,28 +187,32 @@ discard block |
||
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - public static function status($code,$message=''){ |
|
| 190 | + public static function status($code,$message='') { |
|
| 191 | 191 | static::header('Status',$message?:$code,$code); |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | - public static function header($name,$value,$code=null){ |
|
| 194 | + public static function header($name,$value,$code=null) { |
|
| 195 | 195 | static::$headers[$name] = [$value,$code]; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - public static function error($code=500,$message='Application Error'){ |
|
| 198 | + public static function error($code=500,$message='Application Error') { |
|
| 199 | 199 | Event::trigger('core.response.error',$code,$message); |
| 200 | 200 | static::status($code,$message); |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | - public static function body($setBody=null){ |
|
| 204 | - if ($setBody) static::$payload = [$setBody]; |
|
| 203 | + public static function body($setBody=null) { |
|
| 204 | + if ($setBody) { |
|
| 205 | + static::$payload = [$setBody]; |
|
| 206 | + } |
|
| 205 | 207 | return Filter::with('core.response.body', |
| 206 | 208 | is_array(static::$payload) ? implode('',static::$payload) : static::$payload |
| 207 | 209 | ); |
| 208 | 210 | } |
| 209 | 211 | |
| 210 | - public static function headers($setHeaders=null){ |
|
| 211 | - if ($setHeaders) static::$headers = $setHeaders; |
|
| 212 | + public static function headers($setHeaders=null) { |
|
| 213 | + if ($setHeaders) { |
|
| 214 | + static::$headers = $setHeaders; |
|
| 215 | + } |
|
| 212 | 216 | return static::$headers; |
| 213 | 217 | } |
| 214 | 218 | |
@@ -219,7 +223,7 @@ discard block |
||
| 219 | 223 | * |
| 220 | 224 | * @return array Headers and body of the response |
| 221 | 225 | */ |
| 222 | - public static function save(){ |
|
| 226 | + public static function save() { |
|
| 223 | 227 | return [ |
| 224 | 228 | 'head' => static::$headers, |
| 225 | 229 | 'body' => static::body(), |
@@ -233,27 +237,33 @@ discard block |
||
| 233 | 237 | * |
| 234 | 238 | * @param array $data head/body saved state |
| 235 | 239 | */ |
| 236 | - public static function load($data){ |
|
| 240 | + public static function load($data) { |
|
| 237 | 241 | $data = (object)$data; |
| 238 | - if (isset($data->head)) static::headers($data->head); |
|
| 239 | - if (isset($data->body)) static::body($data->body); |
|
| 242 | + if (isset($data->head)) { |
|
| 243 | + static::headers($data->head); |
|
| 244 | + } |
|
| 245 | + if (isset($data->body)) { |
|
| 246 | + static::body($data->body); |
|
| 247 | + } |
|
| 240 | 248 | } |
| 241 | 249 | |
| 242 | - public static function send($force = false){ |
|
| 250 | + public static function send($force = false) { |
|
| 243 | 251 | if (!static::$sent || $force) { |
| 244 | 252 | static::$sent = true; |
| 245 | 253 | Event::trigger('core.response.send'); |
| 246 | - if (false === headers_sent()) foreach (static::$headers as $name => $value_code) { |
|
| 254 | + if (false === headers_sent()) { |
|
| 255 | + foreach (static::$headers as $name => $value_code) { |
|
| 247 | 256 | |
| 248 | 257 | if (is_array($value_code)) { |
| 249 | 258 | list($value, $code) = (count($value_code) > 1) ? $value_code : [current($value_code), 200]; |
| 259 | + } |
|
| 250 | 260 | } else { |
| 251 | 261 | $value = $value_code; |
| 252 | 262 | $code = null; |
| 253 | 263 | } |
| 254 | 264 | |
| 255 | - if ($value == 'Status'){ |
|
| 256 | - if (function_exists('http_response_code')){ |
|
| 265 | + if ($value == 'Status') { |
|
| 266 | + if (function_exists('http_response_code')) { |
|
| 257 | 267 | http_response_code($code); |
| 258 | 268 | } else { |
| 259 | 269 | header("Status: $code", true, $code); |
@@ -265,7 +275,9 @@ discard block |
||
| 265 | 275 | : header("$name: $value", true); |
| 266 | 276 | } |
| 267 | 277 | } |
| 268 | - if (static::$force_dl) header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
|
| 278 | + if (static::$force_dl) { |
|
| 279 | + header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
|
| 280 | + } |
|
| 269 | 281 | echo static::body(); |
| 270 | 282 | } |
| 271 | 283 | } |
@@ -42,7 +42,7 @@ |
||
| 42 | 42 | public static function create($data){ |
| 43 | 43 | $tmp = new static; |
| 44 | 44 | foreach ((array)$data as $key => $value) { |
| 45 | - $tmp->$key = $value; |
|
| 45 | + $tmp->$key = $value; |
|
| 46 | 46 | } |
| 47 | 47 | $tmp->save(); |
| 48 | 48 | return $tmp; |
@@ -13,13 +13,13 @@ discard block |
||
| 13 | 13 | abstract class Model { |
| 14 | 14 | use Module, Persistence; |
| 15 | 15 | |
| 16 | - public static function where($where_sql = false){ |
|
| 16 | + public static function where($where_sql = false) { |
|
| 17 | 17 | // Forward persistence calls to caller class, not Model |
| 18 | 18 | $self = get_called_class(); |
| 19 | 19 | $table = $self::persistenceOptions('table'); |
| 20 | 20 | $key = $self::persistenceOptions('key'); |
| 21 | 21 | |
| 22 | - $sql = "select $key from $table" . ($where_sql ? " where $where_sql" : ''); |
|
| 22 | + $sql = "select $key from $table".($where_sql ? " where $where_sql" : ''); |
|
| 23 | 23 | |
| 24 | 24 | $results = []; |
| 25 | 25 | SQL::each($sql, function($row) use ($self, &$results, $key){ |
@@ -28,20 +28,20 @@ discard block |
||
| 28 | 28 | return $results; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - public static function all($page=1, $limit=-1){ |
|
| 32 | - $offset = max(1,$page)-1; |
|
| 31 | + public static function all($page = 1, $limit = -1) { |
|
| 32 | + $offset = max(1, $page) - 1; |
|
| 33 | 33 | return static::where($limit < 1 ? "" : "1 limit $limit offset $offset"); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - public function primaryKey(){ |
|
| 36 | + public function primaryKey() { |
|
| 37 | 37 | $self = get_called_class(); |
| 38 | 38 | $key = $self::persistenceOptions('key'); |
| 39 | 39 | return $this->$key; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - public static function create($data){ |
|
| 42 | + public static function create($data) { |
|
| 43 | 43 | $tmp = new static; |
| 44 | - foreach ((array)$data as $key => $value) { |
|
| 44 | + foreach ((array) $data as $key => $value) { |
|
| 45 | 45 | $tmp->$key = $value; |
| 46 | 46 | } |
| 47 | 47 | $tmp->save(); |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | abstract class Model { |
| 14 | 14 | use Module, Persistence; |
| 15 | 15 | |
| 16 | - public static function where($where_sql = false){ |
|
| 16 | + public static function where($where_sql = false) { |
|
| 17 | 17 | // Forward persistence calls to caller class, not Model |
| 18 | 18 | $self = get_called_class(); |
| 19 | 19 | $table = $self::persistenceOptions('table'); |
@@ -22,24 +22,24 @@ discard block |
||
| 22 | 22 | $sql = "select $key from $table" . ($where_sql ? " where $where_sql" : ''); |
| 23 | 23 | |
| 24 | 24 | $results = []; |
| 25 | - SQL::each($sql, function($row) use ($self, &$results, $key){ |
|
| 25 | + SQL::each($sql, function($row) use ($self, &$results, $key) { |
|
| 26 | 26 | $results[] = $self::load($row->$key); |
| 27 | 27 | }); |
| 28 | 28 | return $results; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - public static function all($page=1, $limit=-1){ |
|
| 31 | + public static function all($page=1, $limit=-1) { |
|
| 32 | 32 | $offset = max(1,$page)-1; |
| 33 | 33 | return static::where($limit < 1 ? "" : "1 limit $limit offset $offset"); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - public function primaryKey(){ |
|
| 36 | + public function primaryKey() { |
|
| 37 | 37 | $self = get_called_class(); |
| 38 | 38 | $key = $self::persistenceOptions('key'); |
| 39 | 39 | return $this->$key; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - public static function create($data){ |
|
| 42 | + public static function create($data) { |
|
| 43 | 43 | $tmp = new static; |
| 44 | 44 | foreach ((array)$data as $key => $value) { |
| 45 | 45 | $tmp->$key = $value; |
@@ -20,8 +20,8 @@ discard block |
||
| 20 | 20 | * @param mixed $options The options passed to the persistence layer. |
| 21 | 21 | * @return mixed All options array or a single value |
| 22 | 22 | */ |
| 23 | - protected static function persistenceOptions($options=null){ |
|
| 24 | - static $_options = ['table'=>null,'key'=>'id']; |
|
| 23 | + protected static function persistenceOptions($options = null) { |
|
| 24 | + static $_options = ['table'=>null, 'key'=>'id']; |
|
| 25 | 25 | if ($options === null) return $_options; |
| 26 | 26 | |
| 27 | 27 | if (is_array($options)) { |
@@ -32,18 +32,18 @@ discard block |
||
| 32 | 32 | } else { |
| 33 | 33 | if (empty($_options['table'])) { |
| 34 | 34 | $self = get_called_class(); |
| 35 | - if (defined("$self::_PRIMARY_KEY_")){ |
|
| 35 | + if (defined("$self::_PRIMARY_KEY_")) { |
|
| 36 | 36 | $x = explode('.', $self::_PRIMARY_KEY_); |
| 37 | 37 | $_options = [ |
| 38 | 38 | 'table' => current($x), |
| 39 | - 'key' => isset($x[1])?$x[1]:'id', |
|
| 39 | + 'key' => isset($x[1]) ? $x[1] : 'id', |
|
| 40 | 40 | ]; |
| 41 | 41 | } else { |
| 42 | 42 | // User pluralized class name as default table |
| 43 | - switch(substr($s = strtolower($self),-1)){ |
|
| 44 | - case 'y': $table = substr($s,0,-1).'ies'; break; |
|
| 45 | - case 's': $table = substr($s,0,-1).'es'; break; |
|
| 46 | - default: $table = $s.'s'; break; |
|
| 43 | + switch (substr($s = strtolower($self), -1)) { |
|
| 44 | + case 'y': $table = substr($s, 0, -1).'ies';break; |
|
| 45 | + case 's': $table = substr($s, 0, -1).'es';break; |
|
| 46 | + default: $table = $s.'s';break; |
|
| 47 | 47 | } |
| 48 | 48 | // Default ID |
| 49 | 49 | $_options = [ |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | * @param callable $callback The callback to use on model save |
| 65 | 65 | * @return callable Current save callback |
| 66 | 66 | */ |
| 67 | - protected static function persistenceSave(callable $callback=null){ |
|
| 67 | + protected static function persistenceSave(callable $callback = null) { |
|
| 68 | 68 | static $save_cb = null; |
| 69 | 69 | return $callback ? $save_cb = $callback : $save_cb; |
| 70 | 70 | } |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | * @param callable $callback The callback to use on model load |
| 78 | 78 | * @return callable Current load callback |
| 79 | 79 | */ |
| 80 | - protected static function persistenceLoad(callable $callback=null){ |
|
| 80 | + protected static function persistenceLoad(callable $callback = null) { |
|
| 81 | 81 | static $retrieve_cb = null; |
| 82 | 82 | return $callback ? $retrieve_cb = $callback : $retrieve_cb; |
| 83 | 83 | } |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | * @param array $options An associative array with options for the persistance layer. |
| 93 | 93 | * @return void |
| 94 | 94 | */ |
| 95 | - public static function persistOn($table, array $options=[]){ |
|
| 95 | + public static function persistOn($table, array $options = []) { |
|
| 96 | 96 | $options['table'] = $table; |
| 97 | 97 | static::persistenceOptions($options); |
| 98 | 98 | } |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | * @param callable $callback The callback to use on model save |
| 104 | 104 | * @return void |
| 105 | 105 | */ |
| 106 | - public static function onSave(callable $callback){ |
|
| 106 | + public static function onSave(callable $callback) { |
|
| 107 | 107 | static::persistenceSave($callback); |
| 108 | 108 | } |
| 109 | 109 | |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | * @param callable $callback The callback to use on model load |
| 113 | 113 | * @return void |
| 114 | 114 | */ |
| 115 | - public static function onLoad(callable $callback){ |
|
| 115 | + public static function onLoad(callable $callback) { |
|
| 116 | 116 | static::persistenceLoad($callback); |
| 117 | 117 | } |
| 118 | 118 | |
@@ -120,23 +120,23 @@ discard block |
||
| 120 | 120 | * Load the model from the persistence layer |
| 121 | 121 | * @return mixed The retrieved object |
| 122 | 122 | */ |
| 123 | - public static function load($pk){ |
|
| 123 | + public static function load($pk) { |
|
| 124 | 124 | $table = static::persistenceOptions('table'); |
| 125 | 125 | $cb = static::persistenceLoad(); |
| 126 | 126 | $op = static::persistenceOptions(); |
| 127 | 127 | |
| 128 | 128 | // Use standard persistence on DB layer |
| 129 | - return ( false == is_callable($cb) ) ? |
|
| 130 | - static::persistenceLoadDefault($pk,$table,$op) : $cb($pk,$table,$op); |
|
| 129 | + return (false == is_callable($cb)) ? |
|
| 130 | + static::persistenceLoadDefault($pk, $table, $op) : $cb($pk, $table, $op); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
| 134 | 134 | * Private Standard Load Method |
| 135 | 135 | */ |
| 136 | - private static function persistenceLoadDefault($pk, $table, $options){ |
|
| 137 | - if ( $data = SQL::single("SELECT * FROM $table WHERE {$options['key']}=? LIMIT 1",[$pk]) ){ |
|
| 136 | + private static function persistenceLoadDefault($pk, $table, $options) { |
|
| 137 | + if ($data = SQL::single("SELECT * FROM $table WHERE {$options['key']}=? LIMIT 1", [$pk])) { |
|
| 138 | 138 | $obj = new static; |
| 139 | - foreach ((array)$data as $key => $value) { |
|
| 139 | + foreach ((array) $data as $key => $value) { |
|
| 140 | 140 | $obj->$key = $value; |
| 141 | 141 | } |
| 142 | 142 | return $obj; |
@@ -149,21 +149,21 @@ discard block |
||
| 149 | 149 | * Save the model to the persistence layer |
| 150 | 150 | * @return mixed The results from the save callback. (default: lastInsertID) |
| 151 | 151 | */ |
| 152 | - public function save(){ |
|
| 152 | + public function save() { |
|
| 153 | 153 | $table = static::persistenceOptions('table'); |
| 154 | 154 | $op = static::persistenceOptions(); |
| 155 | 155 | $cb = static::persistenceSave(); |
| 156 | 156 | |
| 157 | 157 | // Use standard persistence on DB layer |
| 158 | - $cb = $cb ? Closure::bind($cb, $this) : [$this,'persistenceSaveDefault']; |
|
| 159 | - return $cb($table,$op); |
|
| 158 | + $cb = $cb ? Closure::bind($cb, $this) : [$this, 'persistenceSaveDefault']; |
|
| 159 | + return $cb($table, $op); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | /** |
| 163 | 163 | * Private Standard Save Method |
| 164 | 164 | */ |
| 165 | - private function persistenceSaveDefault($table,$options){ |
|
| 166 | - return SQL::insertOrUpdate($table,array_filter((array)$this),$options['key']); |
|
| 165 | + private function persistenceSaveDefault($table, $options) { |
|
| 166 | + return SQL::insertOrUpdate($table, array_filter((array) $this), $options['key']); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | |
@@ -20,19 +20,23 @@ discard block |
||
| 20 | 20 | * @param mixed $options The options passed to the persistence layer. |
| 21 | 21 | * @return mixed All options array or a single value |
| 22 | 22 | */ |
| 23 | - protected static function persistenceOptions($options=null){ |
|
| 23 | + protected static function persistenceOptions($options=null) { |
|
| 24 | 24 | static $_options = ['table'=>null,'key'=>'id']; |
| 25 | - if ($options === null) return $_options; |
|
| 25 | + if ($options === null) { |
|
| 26 | + return $_options; |
|
| 27 | + } |
|
| 26 | 28 | |
| 27 | 29 | if (is_array($options)) { |
| 28 | 30 | foreach ($_options as $key => &$value) { |
| 29 | - if (isset($options[$key])) $value = $options[$key]; |
|
| 31 | + if (isset($options[$key])) { |
|
| 32 | + $value = $options[$key]; |
|
| 33 | + } |
|
| 30 | 34 | } |
| 31 | 35 | return $_options; |
| 32 | 36 | } else { |
| 33 | 37 | if (empty($_options['table'])) { |
| 34 | 38 | $self = get_called_class(); |
| 35 | - if (defined("$self::_PRIMARY_KEY_")){ |
|
| 39 | + if (defined("$self::_PRIMARY_KEY_")) { |
|
| 36 | 40 | $x = explode('.', $self::_PRIMARY_KEY_); |
| 37 | 41 | $_options = [ |
| 38 | 42 | 'table' => current($x), |
@@ -40,7 +44,7 @@ discard block |
||
| 40 | 44 | ]; |
| 41 | 45 | } else { |
| 42 | 46 | // User pluralized class name as default table |
| 43 | - switch(substr($s = strtolower($self),-1)){ |
|
| 47 | + switch(substr($s = strtolower($self),-1)) { |
|
| 44 | 48 | case 'y': $table = substr($s,0,-1).'ies'; break; |
| 45 | 49 | case 's': $table = substr($s,0,-1).'es'; break; |
| 46 | 50 | default: $table = $s.'s'; break; |
@@ -64,7 +68,7 @@ discard block |
||
| 64 | 68 | * @param callable $callback The callback to use on model save |
| 65 | 69 | * @return callable Current save callback |
| 66 | 70 | */ |
| 67 | - protected static function persistenceSave(callable $callback=null){ |
|
| 71 | + protected static function persistenceSave(callable $callback=null) { |
|
| 68 | 72 | static $save_cb = null; |
| 69 | 73 | return $callback ? $save_cb = $callback : $save_cb; |
| 70 | 74 | } |
@@ -77,7 +81,7 @@ discard block |
||
| 77 | 81 | * @param callable $callback The callback to use on model load |
| 78 | 82 | * @return callable Current load callback |
| 79 | 83 | */ |
| 80 | - protected static function persistenceLoad(callable $callback=null){ |
|
| 84 | + protected static function persistenceLoad(callable $callback=null) { |
|
| 81 | 85 | static $retrieve_cb = null; |
| 82 | 86 | return $callback ? $retrieve_cb = $callback : $retrieve_cb; |
| 83 | 87 | } |
@@ -92,7 +96,7 @@ discard block |
||
| 92 | 96 | * @param array $options An associative array with options for the persistance layer. |
| 93 | 97 | * @return void |
| 94 | 98 | */ |
| 95 | - public static function persistOn($table, array $options=[]){ |
|
| 99 | + public static function persistOn($table, array $options=[]) { |
|
| 96 | 100 | $options['table'] = $table; |
| 97 | 101 | static::persistenceOptions($options); |
| 98 | 102 | } |
@@ -103,7 +107,7 @@ discard block |
||
| 103 | 107 | * @param callable $callback The callback to use on model save |
| 104 | 108 | * @return void |
| 105 | 109 | */ |
| 106 | - public static function onSave(callable $callback){ |
|
| 110 | + public static function onSave(callable $callback) { |
|
| 107 | 111 | static::persistenceSave($callback); |
| 108 | 112 | } |
| 109 | 113 | |
@@ -112,7 +116,7 @@ discard block |
||
| 112 | 116 | * @param callable $callback The callback to use on model load |
| 113 | 117 | * @return void |
| 114 | 118 | */ |
| 115 | - public static function onLoad(callable $callback){ |
|
| 119 | + public static function onLoad(callable $callback) { |
|
| 116 | 120 | static::persistenceLoad($callback); |
| 117 | 121 | } |
| 118 | 122 | |
@@ -120,7 +124,7 @@ discard block |
||
| 120 | 124 | * Load the model from the persistence layer |
| 121 | 125 | * @return mixed The retrieved object |
| 122 | 126 | */ |
| 123 | - public static function load($pk){ |
|
| 127 | + public static function load($pk) { |
|
| 124 | 128 | $table = static::persistenceOptions('table'); |
| 125 | 129 | $cb = static::persistenceLoad(); |
| 126 | 130 | $op = static::persistenceOptions(); |
@@ -133,8 +137,8 @@ discard block |
||
| 133 | 137 | /** |
| 134 | 138 | * Private Standard Load Method |
| 135 | 139 | */ |
| 136 | - private static function persistenceLoadDefault($pk, $table, $options){ |
|
| 137 | - if ( $data = SQL::single("SELECT * FROM $table WHERE {$options['key']}=? LIMIT 1",[$pk]) ){ |
|
| 140 | + private static function persistenceLoadDefault($pk, $table, $options) { |
|
| 141 | + if ( $data = SQL::single("SELECT * FROM $table WHERE {$options['key']}=? LIMIT 1",[$pk]) ) { |
|
| 138 | 142 | $obj = new static; |
| 139 | 143 | foreach ((array)$data as $key => $value) { |
| 140 | 144 | $obj->$key = $value; |
@@ -149,7 +153,7 @@ discard block |
||
| 149 | 153 | * Save the model to the persistence layer |
| 150 | 154 | * @return mixed The results from the save callback. (default: lastInsertID) |
| 151 | 155 | */ |
| 152 | - public function save(){ |
|
| 156 | + public function save() { |
|
| 153 | 157 | $table = static::persistenceOptions('table'); |
| 154 | 158 | $op = static::persistenceOptions(); |
| 155 | 159 | $cb = static::persistenceSave(); |
@@ -162,7 +166,7 @@ discard block |
||
| 162 | 166 | /** |
| 163 | 167 | * Private Standard Save Method |
| 164 | 168 | */ |
| 165 | - private function persistenceSaveDefault($table,$options){ |
|
| 169 | + private function persistenceSaveDefault($table,$options) { |
|
| 166 | 170 | return SQL::insertOrUpdate($table,array_filter((array)$this),$options['key']); |
| 167 | 171 | } |
| 168 | 172 | |
@@ -140,7 +140,7 @@ |
||
| 140 | 140 | Event::trigger('core.route.end', $this); |
| 141 | 141 | |
| 142 | 142 | return [Filter::with('core.route.response', Response::body())]; |
| 143 | - } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | 146 | * Check if route match URL and HTTP Method and run if it is valid. |
@@ -38,12 +38,12 @@ discard block |
||
| 38 | 38 | * @param string $method The HTTP method for which the route must respond. |
| 39 | 39 | * @return Route |
| 40 | 40 | */ |
| 41 | - public function __construct($URLPattern, $callback = null, $method='get'){ |
|
| 42 | - $prefix = static::$prefix ? rtrim(implode('',static::$prefix),'/') : ''; |
|
| 43 | - $pattern = "/" . trim($URLPattern, "/"); |
|
| 41 | + public function __construct($URLPattern, $callback = null, $method = 'get') { |
|
| 42 | + $prefix = static::$prefix ? rtrim(implode('', static::$prefix), '/') : ''; |
|
| 43 | + $pattern = "/".trim($URLPattern, "/"); |
|
| 44 | 44 | // Adjust / optionality with dynamic patterns |
| 45 | 45 | // Ex: /test/(:a) ===> /test(/:a) |
| 46 | - $this->URLPattern = str_replace('//','/',str_replace('/(','(/', rtrim("{$prefix}{$pattern}","/"))); |
|
| 46 | + $this->URLPattern = str_replace('//', '/', str_replace('/(', '(/', rtrim("{$prefix}{$pattern}", "/"))); |
|
| 47 | 47 | |
| 48 | 48 | $this->dynamic = $this->isDynamic($this->URLPattern); |
| 49 | 49 | $this->pattern = $this->dynamic ? $this->compilePatternAsRegex($this->URLPattern, $this->rules) : $this->URLPattern; |
@@ -60,19 +60,19 @@ discard block |
||
| 60 | 60 | * @param string $method The HTTP Method to check against. |
| 61 | 61 | * @return boolean |
| 62 | 62 | */ |
| 63 | - public function match($URL,$method='get'){ |
|
| 63 | + public function match($URL, $method = 'get') { |
|
| 64 | 64 | $method = strtolower($method); |
| 65 | 65 | |
| 66 | 66 | // * is an http method wildcard |
| 67 | 67 | if (empty($this->methods[$method]) && empty($this->methods['*'])) return false; |
| 68 | - $URL = rtrim($URL,'/'); |
|
| 68 | + $URL = rtrim($URL, '/'); |
|
| 69 | 69 | $args = []; |
| 70 | - if ( $this->dynamic |
|
| 71 | - ? preg_match($this->pattern,$URL,$args) |
|
| 72 | - : $URL == rtrim($this->pattern,'/') |
|
| 73 | - ){ |
|
| 74 | - foreach ( $args as $key => $value ) { |
|
| 75 | - if ( false === is_string($key) ) unset($args[$key]); |
|
| 70 | + if ($this->dynamic |
|
| 71 | + ? preg_match($this->pattern, $URL, $args) |
|
| 72 | + : $URL == rtrim($this->pattern, '/') |
|
| 73 | + ) { |
|
| 74 | + foreach ($args as $key => $value) { |
|
| 75 | + if (false === is_string($key)) unset($args[$key]); |
|
| 76 | 76 | } |
| 77 | 77 | return $args; |
| 78 | 78 | } |
@@ -85,12 +85,12 @@ discard block |
||
| 85 | 85 | * @param string $method The HTTP Method requested. |
| 86 | 86 | * @return array The callback response. |
| 87 | 87 | */ |
| 88 | - public function run(array $args, $method='get'){ |
|
| 88 | + public function run(array $args, $method = 'get') { |
|
| 89 | 89 | $method = strtolower($method); |
| 90 | - $append_echoed_text = Options::get('core.route.append_echoed_text',true); |
|
| 90 | + $append_echoed_text = Options::get('core.route.append_echoed_text', true); |
|
| 91 | 91 | |
| 92 | 92 | // Call direct befores |
| 93 | - if ( $this->befores ) { |
|
| 93 | + if ($this->befores) { |
|
| 94 | 94 | // Reverse befores order |
| 95 | 95 | foreach (array_reverse($this->befores) as $mw) { |
| 96 | 96 | Event::trigger('core.route.before', $this, $mw); |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | $mw_result = call_user_func($mw); |
| 99 | 99 | $raw_echoed = ob_get_clean(); |
| 100 | 100 | if ($append_echoed_text) Response::add($raw_echoed); |
| 101 | - if ( false === $mw_result ) { |
|
| 101 | + if (false === $mw_result) { |
|
| 102 | 102 | return ['']; |
| 103 | 103 | } else { |
| 104 | 104 | Response::add($mw_result); |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | : $this->callback; |
| 112 | 112 | |
| 113 | 113 | if (is_callable($callback)) { |
| 114 | - Response::type( Options::get('core.route.response_default_type', Response::TYPE_HTML) ); |
|
| 114 | + Response::type(Options::get('core.route.response_default_type', Response::TYPE_HTML)); |
|
| 115 | 115 | |
| 116 | 116 | ob_start(); |
| 117 | 117 | $view_results = call_user_func_array($callback, $args); |
@@ -122,14 +122,14 @@ discard block |
||
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | // Apply afters |
| 125 | - if ( $this->afters ) { |
|
| 125 | + if ($this->afters) { |
|
| 126 | 126 | foreach ($this->afters as $mw) { |
| 127 | 127 | Event::trigger('core.route.after', $this, $mw); |
| 128 | 128 | ob_start(); |
| 129 | 129 | $mw_result = call_user_func($mw); |
| 130 | 130 | $raw_echoed = ob_get_clean(); |
| 131 | 131 | if ($append_echoed_text) Response::add($raw_echoed); |
| 132 | - if ( false === $mw_result ) { |
|
| 132 | + if (false === $mw_result) { |
|
| 133 | 133 | return ['']; |
| 134 | 134 | } else { |
| 135 | 135 | Response::add($mw_result); |
@@ -148,8 +148,8 @@ discard block |
||
| 148 | 148 | * @param string $method The HTTP Method to check against. |
| 149 | 149 | * @return array The callback response. |
| 150 | 150 | */ |
| 151 | - public function runIfMatch($URL, $method='get'){ |
|
| 152 | - return ($args = $this->match($URL,$method)) ? $this->run($args,$method) : null; |
|
| 151 | + public function runIfMatch($URL, $method = 'get') { |
|
| 152 | + return ($args = $this->match($URL, $method)) ? $this->run($args, $method) : null; |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
@@ -158,8 +158,8 @@ discard block |
||
| 158 | 158 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 159 | 159 | * @return Route |
| 160 | 160 | */ |
| 161 | - public static function on($URLPattern, $callback = null){ |
|
| 162 | - return new Route($URLPattern,$callback); |
|
| 161 | + public static function on($URLPattern, $callback = null) { |
|
| 162 | + return new Route($URLPattern, $callback); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
@@ -168,8 +168,8 @@ discard block |
||
| 168 | 168 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 169 | 169 | * @return Route |
| 170 | 170 | */ |
| 171 | - public static function get($URLPattern, $callback = null){ |
|
| 172 | - return (new Route($URLPattern,$callback))->via('get'); |
|
| 171 | + public static function get($URLPattern, $callback = null) { |
|
| 172 | + return (new Route($URLPattern, $callback))->via('get'); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | /** |
@@ -178,8 +178,8 @@ discard block |
||
| 178 | 178 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 179 | 179 | * @return Route |
| 180 | 180 | */ |
| 181 | - public static function post($URLPattern, $callback = null){ |
|
| 182 | - return (new Route($URLPattern,$callback))->via('post'); |
|
| 181 | + public static function post($URLPattern, $callback = null) { |
|
| 182 | + return (new Route($URLPattern, $callback))->via('post'); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | /** |
@@ -188,8 +188,8 @@ discard block |
||
| 188 | 188 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 189 | 189 | * @return Route |
| 190 | 190 | */ |
| 191 | - public static function any($URLPattern, $callback = null){ |
|
| 192 | - return (new Route($URLPattern,$callback))->via('*'); |
|
| 191 | + public static function any($URLPattern, $callback = null) { |
|
| 192 | + return (new Route($URLPattern, $callback))->via('*'); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | /** |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | */ |
| 235 | 235 | public function & via(){ |
| 236 | 236 | $this->methods = []; |
| 237 | - foreach (func_get_args() as $method){ |
|
| 237 | + foreach (func_get_args() as $method) { |
|
| 238 | 238 | $this->methods[strtolower($method)] = true; |
| 239 | 239 | } |
| 240 | 240 | return $this; |
@@ -256,10 +256,10 @@ discard block |
||
| 256 | 256 | * @return Route |
| 257 | 257 | */ |
| 258 | 258 | public function & rules(array $rules){ |
| 259 | - foreach ((array)$rules as $varname => $rule){ |
|
| 259 | + foreach ((array) $rules as $varname => $rule) { |
|
| 260 | 260 | $this->rules[$varname] = $rule; |
| 261 | 261 | } |
| 262 | - $this->pattern = $this->compilePatternAsRegex( $this->URLPattern, $this->rules ); |
|
| 262 | + $this->pattern = $this->compilePatternAsRegex($this->URLPattern, $this->rules); |
|
| 263 | 263 | return $this; |
| 264 | 264 | } |
| 265 | 265 | |
@@ -297,10 +297,10 @@ discard block |
||
| 297 | 297 | * @param string $pattern The URL schema. |
| 298 | 298 | * @return string The compiled PREG RegEx. |
| 299 | 299 | */ |
| 300 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
| 301 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
| 302 | - return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
|
| 303 | - },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
|
| 300 | + protected static function compilePatternAsRegex($pattern, $rules = []) { |
|
| 301 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S', function($g) use (&$rules){ |
|
| 302 | + return '(?<'.$g[1].'>'.(isset($rules[$g[1]]) ? $rules[$g[1]] : '[^/]+').')'; |
|
| 303 | + },str_replace(['.', ')', '*'], ['\.', ')?', '.+'], $pattern)).'$#'; |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | /** |
@@ -310,10 +310,10 @@ discard block |
||
| 310 | 310 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
| 311 | 311 | * @return array The extracted variables |
| 312 | 312 | */ |
| 313 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
| 313 | + protected static function extractVariablesFromURL($pattern, $URL = null, $cut = false) { |
|
| 314 | 314 | $URL = $URL ?: Request::URI(); |
| 315 | - $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
|
| 316 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
| 315 | + $pattern = $cut ? str_replace('$#', '', $pattern).'#' : $pattern; |
|
| 316 | + if (!preg_match($pattern, $URL, $args)) return false; |
|
| 317 | 317 | foreach ($args as $key => $value) { |
| 318 | 318 | if (false === is_string($key)) unset($args[$key]); |
| 319 | 319 | } |
@@ -325,8 +325,8 @@ discard block |
||
| 325 | 325 | * @param string $pattern The URL schema. |
| 326 | 326 | * @return boolean |
| 327 | 327 | */ |
| 328 | - protected static function isDynamic($pattern){ |
|
| 329 | - return strlen($pattern) != strcspn($pattern,':(?[*+'); |
|
| 328 | + protected static function isDynamic($pattern) { |
|
| 329 | + return strlen($pattern) != strcspn($pattern, ':(?[*+'); |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | /** |
@@ -334,8 +334,8 @@ discard block |
||
| 334 | 334 | * @param Route $r |
| 335 | 335 | * @return Route |
| 336 | 336 | */ |
| 337 | - public static function add($r){ |
|
| 338 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
| 337 | + public static function add($r) { |
|
| 338 | + if (isset(static::$group[0])) static::$group[0]->add($r); |
|
| 339 | 339 | return static::$routes[implode('', static::$prefix)][] = $r; |
| 340 | 340 | } |
| 341 | 341 | |
@@ -344,10 +344,10 @@ discard block |
||
| 344 | 344 | * @param string $prefix The url prefix for the internal route definitions. |
| 345 | 345 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
| 346 | 346 | */ |
| 347 | - public static function group($prefix, $callback=null){ |
|
| 347 | + public static function group($prefix, $callback = null) { |
|
| 348 | 348 | |
| 349 | 349 | // Skip definition if current request doesn't match group. |
| 350 | - $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
|
| 350 | + $prefix_complete = rtrim(implode('', static::$prefix), '/').$prefix; |
|
| 351 | 351 | |
| 352 | 352 | static::$prefix[] = $prefix; |
| 353 | 353 | if (empty(static::$group)) static::$group = []; |
@@ -361,7 +361,7 @@ discard block |
||
| 361 | 361 | $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete), null, true); |
| 362 | 362 | // Errors in compile pattern or variable extraction, aborting. |
| 363 | 363 | if (false === $vars) return false; |
| 364 | - if ($callback) call_user_func_array($callback,$vars); |
|
| 364 | + if ($callback) call_user_func_array($callback, $vars); |
|
| 365 | 365 | break; |
| 366 | 366 | |
| 367 | 367 | // Static group |
@@ -379,8 +379,8 @@ discard block |
||
| 379 | 379 | |
| 380 | 380 | } |
| 381 | 381 | |
| 382 | - public static function exitWithError($code,$message="Application Error"){ |
|
| 383 | - Response::error($code,$message); |
|
| 382 | + public static function exitWithError($code, $message = "Application Error") { |
|
| 383 | + Response::error($code, $message); |
|
| 384 | 384 | Response::send(); |
| 385 | 385 | exit; |
| 386 | 386 | } |
@@ -390,27 +390,27 @@ discard block |
||
| 390 | 390 | * @param string $URL The URL to match onto. |
| 391 | 391 | * @return boolean true if a route callback was executed. |
| 392 | 392 | */ |
| 393 | - public static function dispatch($URL=null,$method=null){ |
|
| 393 | + public static function dispatch($URL = null, $method = null) { |
|
| 394 | 394 | if (!$URL) $URL = Request::URI(); |
| 395 | 395 | if (!$method) $method = Request::method(); |
| 396 | 396 | |
| 397 | - $__deferred_send = new Deferred(function(){ |
|
| 398 | - if (Options::get('core.response.autosend',true)){ |
|
| 397 | + $__deferred_send = new Deferred(function() { |
|
| 398 | + if (Options::get('core.response.autosend', true)) { |
|
| 399 | 399 | Response::send(); |
| 400 | 400 | } |
| 401 | 401 | }); |
| 402 | 402 | |
| 403 | - foreach ((array)static::$routes as $group => $routes){ |
|
| 403 | + foreach ((array) static::$routes as $group => $routes) { |
|
| 404 | 404 | foreach ($routes as $route) { |
| 405 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
| 406 | - $route->run($args,$method); |
|
| 405 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL, $method))) { |
|
| 406 | + $route->run($args, $method); |
|
| 407 | 407 | return true; |
| 408 | 408 | } |
| 409 | 409 | } |
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | Response::status(404, '404 Resource not found.'); |
| 413 | - foreach (array_filter(Event::trigger(404)) as $res){ |
|
| 413 | + foreach (array_filter(Event::trigger(404)) as $res) { |
|
| 414 | 414 | |
| 415 | 415 | } |
| 416 | 416 | return false; |
@@ -420,34 +420,34 @@ discard block |
||
| 420 | 420 | class RouteGroup { |
| 421 | 421 | protected $routes; |
| 422 | 422 | |
| 423 | - public function __construct(){ |
|
| 423 | + public function __construct() { |
|
| 424 | 424 | $this->routes = new SplObjectStorage; |
| 425 | 425 | return Route::add($this); |
| 426 | 426 | } |
| 427 | 427 | |
| 428 | - public function has($r){ |
|
| 428 | + public function has($r) { |
|
| 429 | 429 | return $this->routes->contains($r); |
| 430 | 430 | } |
| 431 | 431 | |
| 432 | - public function add($r){ |
|
| 432 | + public function add($r) { |
|
| 433 | 433 | $this->routes->attach($r); |
| 434 | 434 | return $this; |
| 435 | 435 | } |
| 436 | 436 | |
| 437 | - public function remove($r){ |
|
| 437 | + public function remove($r) { |
|
| 438 | 438 | if ($this->routes->contains($r)) $this->routes->detach($r); |
| 439 | 439 | return $this; |
| 440 | 440 | } |
| 441 | 441 | |
| 442 | - public function before($callbacks){ |
|
| 443 | - foreach ($this->routes as $route){ |
|
| 442 | + public function before($callbacks) { |
|
| 443 | + foreach ($this->routes as $route) { |
|
| 444 | 444 | $route->before($callbacks); |
| 445 | 445 | } |
| 446 | 446 | return $this; |
| 447 | 447 | } |
| 448 | 448 | |
| 449 | - public function after($callbacks){ |
|
| 450 | - foreach ($this->routes as $route){ |
|
| 449 | + public function after($callbacks) { |
|
| 450 | + foreach ($this->routes as $route) { |
|
| 451 | 451 | $route->after($callbacks); |
| 452 | 452 | } |
| 453 | 453 | 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 | $prefix = static::$prefix ? rtrim(implode('',static::$prefix),'/') : ''; |
| 43 | 43 | $pattern = "/" . trim($URLPattern, "/"); |
| 44 | 44 | // Adjust / optionality with dynamic patterns |
@@ -60,19 +60,23 @@ discard block |
||
| 60 | 60 | * @param string $method The HTTP Method to check against. |
| 61 | 61 | * @return boolean |
| 62 | 62 | */ |
| 63 | - public function match($URL,$method='get'){ |
|
| 63 | + public function match($URL,$method='get') { |
|
| 64 | 64 | $method = strtolower($method); |
| 65 | 65 | |
| 66 | 66 | // * is an http method wildcard |
| 67 | - if (empty($this->methods[$method]) && empty($this->methods['*'])) return false; |
|
| 67 | + if (empty($this->methods[$method]) && empty($this->methods['*'])) { |
|
| 68 | + return false; |
|
| 69 | + } |
|
| 68 | 70 | $URL = rtrim($URL,'/'); |
| 69 | 71 | $args = []; |
| 70 | 72 | if ( $this->dynamic |
| 71 | 73 | ? preg_match($this->pattern,$URL,$args) |
| 72 | 74 | : $URL == rtrim($this->pattern,'/') |
| 73 | - ){ |
|
| 75 | + ) { |
|
| 74 | 76 | foreach ( $args as $key => $value ) { |
| 75 | - if ( false === is_string($key) ) unset($args[$key]); |
|
| 77 | + if ( false === is_string($key) ) { |
|
| 78 | + unset($args[$key]); |
|
| 79 | + } |
|
| 76 | 80 | } |
| 77 | 81 | return $args; |
| 78 | 82 | } |
@@ -85,7 +89,7 @@ discard block |
||
| 85 | 89 | * @param string $method The HTTP Method requested. |
| 86 | 90 | * @return array The callback response. |
| 87 | 91 | */ |
| 88 | - public function run(array $args, $method='get'){ |
|
| 92 | + public function run(array $args, $method='get') { |
|
| 89 | 93 | $method = strtolower($method); |
| 90 | 94 | $append_echoed_text = Options::get('core.route.append_echoed_text',true); |
| 91 | 95 | |
@@ -97,7 +101,9 @@ discard block |
||
| 97 | 101 | ob_start(); |
| 98 | 102 | $mw_result = call_user_func($mw); |
| 99 | 103 | $raw_echoed = ob_get_clean(); |
| 100 | - if ($append_echoed_text) Response::add($raw_echoed); |
|
| 104 | + if ($append_echoed_text) { |
|
| 105 | + Response::add($raw_echoed); |
|
| 106 | + } |
|
| 101 | 107 | if ( false === $mw_result ) { |
| 102 | 108 | return ['']; |
| 103 | 109 | } else { |
@@ -117,7 +123,9 @@ discard block |
||
| 117 | 123 | $view_results = call_user_func_array($callback, $args); |
| 118 | 124 | $raw_echoed = ob_get_clean(); |
| 119 | 125 | |
| 120 | - if ($append_echoed_text) Response::add($raw_echoed); |
|
| 126 | + if ($append_echoed_text) { |
|
| 127 | + Response::add($raw_echoed); |
|
| 128 | + } |
|
| 121 | 129 | Response::add($view_results); |
| 122 | 130 | } |
| 123 | 131 | |
@@ -128,7 +136,9 @@ discard block |
||
| 128 | 136 | ob_start(); |
| 129 | 137 | $mw_result = call_user_func($mw); |
| 130 | 138 | $raw_echoed = ob_get_clean(); |
| 131 | - if ($append_echoed_text) Response::add($raw_echoed); |
|
| 139 | + if ($append_echoed_text) { |
|
| 140 | + Response::add($raw_echoed); |
|
| 141 | + } |
|
| 132 | 142 | if ( false === $mw_result ) { |
| 133 | 143 | return ['']; |
| 134 | 144 | } else { |
@@ -148,7 +158,7 @@ discard block |
||
| 148 | 158 | * @param string $method The HTTP Method to check against. |
| 149 | 159 | * @return array The callback response. |
| 150 | 160 | */ |
| 151 | - public function runIfMatch($URL, $method='get'){ |
|
| 161 | + public function runIfMatch($URL, $method='get') { |
|
| 152 | 162 | return ($args = $this->match($URL,$method)) ? $this->run($args,$method) : null; |
| 153 | 163 | } |
| 154 | 164 | |
@@ -158,7 +168,7 @@ discard block |
||
| 158 | 168 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 159 | 169 | * @return Route |
| 160 | 170 | */ |
| 161 | - public static function on($URLPattern, $callback = null){ |
|
| 171 | + public static function on($URLPattern, $callback = null) { |
|
| 162 | 172 | return new Route($URLPattern,$callback); |
| 163 | 173 | } |
| 164 | 174 | |
@@ -168,7 +178,7 @@ discard block |
||
| 168 | 178 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 169 | 179 | * @return Route |
| 170 | 180 | */ |
| 171 | - public static function get($URLPattern, $callback = null){ |
|
| 181 | + public static function get($URLPattern, $callback = null) { |
|
| 172 | 182 | return (new Route($URLPattern,$callback))->via('get'); |
| 173 | 183 | } |
| 174 | 184 | |
@@ -178,7 +188,7 @@ discard block |
||
| 178 | 188 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 179 | 189 | * @return Route |
| 180 | 190 | */ |
| 181 | - public static function post($URLPattern, $callback = null){ |
|
| 191 | + public static function post($URLPattern, $callback = null) { |
|
| 182 | 192 | return (new Route($URLPattern,$callback))->via('post'); |
| 183 | 193 | } |
| 184 | 194 | |
@@ -188,7 +198,7 @@ discard block |
||
| 188 | 198 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 189 | 199 | * @return Route |
| 190 | 200 | */ |
| 191 | - public static function any($URLPattern, $callback = null){ |
|
| 201 | + public static function any($URLPattern, $callback = null) { |
|
| 192 | 202 | return (new Route($URLPattern,$callback))->via('*'); |
| 193 | 203 | } |
| 194 | 204 | |
@@ -234,7 +244,7 @@ discard block |
||
| 234 | 244 | */ |
| 235 | 245 | public function & via(){ |
| 236 | 246 | $this->methods = []; |
| 237 | - foreach (func_get_args() as $method){ |
|
| 247 | + foreach (func_get_args() as $method) { |
|
| 238 | 248 | $this->methods[strtolower($method)] = true; |
| 239 | 249 | } |
| 240 | 250 | return $this; |
@@ -256,7 +266,7 @@ discard block |
||
| 256 | 266 | * @return Route |
| 257 | 267 | */ |
| 258 | 268 | public function & rules(array $rules){ |
| 259 | - foreach ((array)$rules as $varname => $rule){ |
|
| 269 | + foreach ((array)$rules as $varname => $rule) { |
|
| 260 | 270 | $this->rules[$varname] = $rule; |
| 261 | 271 | } |
| 262 | 272 | $this->pattern = $this->compilePatternAsRegex( $this->URLPattern, $this->rules ); |
@@ -285,7 +295,9 @@ discard block |
||
| 285 | 295 | $route->callback = []; |
| 286 | 296 | foreach ($callbacks as $method => $callback) { |
| 287 | 297 | $method = strtolower($method); |
| 288 | - if (Request::method() !== $method) continue; |
|
| 298 | + if (Request::method() !== $method) { |
|
| 299 | + continue; |
|
| 300 | + } |
|
| 289 | 301 | $route->callback[$method] = $callback; |
| 290 | 302 | $route->methods[$method] = 1; |
| 291 | 303 | } |
@@ -297,8 +309,8 @@ discard block |
||
| 297 | 309 | * @param string $pattern The URL schema. |
| 298 | 310 | * @return string The compiled PREG RegEx. |
| 299 | 311 | */ |
| 300 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
| 301 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
| 312 | + protected static function compilePatternAsRegex($pattern, $rules=[]) { |
|
| 313 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules) { |
|
| 302 | 314 | return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
| 303 | 315 | },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
| 304 | 316 | } |
@@ -310,12 +322,16 @@ discard block |
||
| 310 | 322 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
| 311 | 323 | * @return array The extracted variables |
| 312 | 324 | */ |
| 313 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
| 325 | + protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false) { |
|
| 314 | 326 | $URL = $URL ?: Request::URI(); |
| 315 | 327 | $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
| 316 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
| 328 | + if ( !preg_match($pattern,$URL,$args) ) { |
|
| 329 | + return false; |
|
| 330 | + } |
|
| 317 | 331 | foreach ($args as $key => $value) { |
| 318 | - if (false === is_string($key)) unset($args[$key]); |
|
| 332 | + if (false === is_string($key)) { |
|
| 333 | + unset($args[$key]); |
|
| 334 | + } |
|
| 319 | 335 | } |
| 320 | 336 | return $args; |
| 321 | 337 | } |
@@ -325,7 +341,7 @@ discard block |
||
| 325 | 341 | * @param string $pattern The URL schema. |
| 326 | 342 | * @return boolean |
| 327 | 343 | */ |
| 328 | - protected static function isDynamic($pattern){ |
|
| 344 | + protected static function isDynamic($pattern) { |
|
| 329 | 345 | return strlen($pattern) != strcspn($pattern,':(?[*+'); |
| 330 | 346 | } |
| 331 | 347 | |
@@ -334,8 +350,10 @@ discard block |
||
| 334 | 350 | * @param Route $r |
| 335 | 351 | * @return Route |
| 336 | 352 | */ |
| 337 | - public static function add($r){ |
|
| 338 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
| 353 | + public static function add($r) { |
|
| 354 | + if ( isset(static::$group[0]) ) { |
|
| 355 | + static::$group[0]->add($r); |
|
| 356 | + } |
|
| 339 | 357 | return static::$routes[implode('', static::$prefix)][] = $r; |
| 340 | 358 | } |
| 341 | 359 | |
@@ -344,13 +362,15 @@ discard block |
||
| 344 | 362 | * @param string $prefix The url prefix for the internal route definitions. |
| 345 | 363 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
| 346 | 364 | */ |
| 347 | - public static function group($prefix, $callback=null){ |
|
| 365 | + public static function group($prefix, $callback=null) { |
|
| 348 | 366 | |
| 349 | 367 | // Skip definition if current request doesn't match group. |
| 350 | 368 | $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
| 351 | 369 | |
| 352 | 370 | static::$prefix[] = $prefix; |
| 353 | - if (empty(static::$group)) static::$group = []; |
|
| 371 | + if (empty(static::$group)) { |
|
| 372 | + static::$group = []; |
|
| 373 | + } |
|
| 354 | 374 | array_unshift(static::$group, new RouteGroup()); |
| 355 | 375 | |
| 356 | 376 | switch (true) { |
@@ -360,13 +380,19 @@ discard block |
||
| 360 | 380 | // Capture vars |
| 361 | 381 | $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete), null, true); |
| 362 | 382 | // Errors in compile pattern or variable extraction, aborting. |
| 363 | - if (false === $vars) return false; |
|
| 364 | - if ($callback) call_user_func_array($callback,$vars); |
|
| 383 | + if (false === $vars) { |
|
| 384 | + return false; |
|
| 385 | + } |
|
| 386 | + if ($callback) { |
|
| 387 | + call_user_func_array($callback,$vars); |
|
| 388 | + } |
|
| 365 | 389 | break; |
| 366 | 390 | |
| 367 | 391 | // Static group |
| 368 | 392 | case 0 === strpos(Request::URI(), $prefix_complete) : |
| 369 | - if ($callback) call_user_func($callback); |
|
| 393 | + if ($callback) { |
|
| 394 | + call_user_func($callback); |
|
| 395 | + } |
|
| 370 | 396 | break; |
| 371 | 397 | |
| 372 | 398 | } |
@@ -374,12 +400,14 @@ discard block |
||
| 374 | 400 | $group = static::$group[0]; |
| 375 | 401 | array_shift(static::$group); |
| 376 | 402 | array_pop(static::$prefix); |
| 377 | - if (empty(static::$prefix)) static::$prefix = ['']; |
|
| 403 | + if (empty(static::$prefix)) { |
|
| 404 | + static::$prefix = ['']; |
|
| 405 | + } |
|
| 378 | 406 | return $group; |
| 379 | 407 | |
| 380 | 408 | } |
| 381 | 409 | |
| 382 | - public static function exitWithError($code,$message="Application Error"){ |
|
| 410 | + public static function exitWithError($code,$message="Application Error") { |
|
| 383 | 411 | Response::error($code,$message); |
| 384 | 412 | Response::send(); |
| 385 | 413 | exit; |
@@ -390,19 +418,23 @@ discard block |
||
| 390 | 418 | * @param string $URL The URL to match onto. |
| 391 | 419 | * @return boolean true if a route callback was executed. |
| 392 | 420 | */ |
| 393 | - public static function dispatch($URL=null,$method=null){ |
|
| 394 | - if (!$URL) $URL = Request::URI(); |
|
| 395 | - if (!$method) $method = Request::method(); |
|
| 421 | + public static function dispatch($URL=null,$method=null) { |
|
| 422 | + if (!$URL) { |
|
| 423 | + $URL = Request::URI(); |
|
| 424 | + } |
|
| 425 | + if (!$method) { |
|
| 426 | + $method = Request::method(); |
|
| 427 | + } |
|
| 396 | 428 | |
| 397 | - $__deferred_send = new Deferred(function(){ |
|
| 398 | - if (Options::get('core.response.autosend',true)){ |
|
| 429 | + $__deferred_send = new Deferred(function() { |
|
| 430 | + if (Options::get('core.response.autosend',true)) { |
|
| 399 | 431 | Response::send(); |
| 400 | 432 | } |
| 401 | 433 | }); |
| 402 | 434 | |
| 403 | - foreach ((array)static::$routes as $group => $routes){ |
|
| 435 | + foreach ((array)static::$routes as $group => $routes) { |
|
| 404 | 436 | foreach ($routes as $route) { |
| 405 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
| 437 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))) { |
|
| 406 | 438 | $route->run($args,$method); |
| 407 | 439 | return true; |
| 408 | 440 | } |
@@ -410,7 +442,7 @@ discard block |
||
| 410 | 442 | } |
| 411 | 443 | |
| 412 | 444 | Response::status(404, '404 Resource not found.'); |
| 413 | - foreach (array_filter(Event::trigger(404)) as $res){ |
|
| 445 | + foreach (array_filter(Event::trigger(404)) as $res) { |
|
| 414 | 446 | |
| 415 | 447 | } |
| 416 | 448 | return false; |
@@ -420,34 +452,36 @@ discard block |
||
| 420 | 452 | class RouteGroup { |
| 421 | 453 | protected $routes; |
| 422 | 454 | |
| 423 | - public function __construct(){ |
|
| 455 | + public function __construct() { |
|
| 424 | 456 | $this->routes = new SplObjectStorage; |
| 425 | 457 | return Route::add($this); |
| 426 | 458 | } |
| 427 | 459 | |
| 428 | - public function has($r){ |
|
| 460 | + public function has($r) { |
|
| 429 | 461 | return $this->routes->contains($r); |
| 430 | 462 | } |
| 431 | 463 | |
| 432 | - public function add($r){ |
|
| 464 | + public function add($r) { |
|
| 433 | 465 | $this->routes->attach($r); |
| 434 | 466 | return $this; |
| 435 | 467 | } |
| 436 | 468 | |
| 437 | - public function remove($r){ |
|
| 438 | - if ($this->routes->contains($r)) $this->routes->detach($r); |
|
| 469 | + public function remove($r) { |
|
| 470 | + if ($this->routes->contains($r)) { |
|
| 471 | + $this->routes->detach($r); |
|
| 472 | + } |
|
| 439 | 473 | return $this; |
| 440 | 474 | } |
| 441 | 475 | |
| 442 | - public function before($callbacks){ |
|
| 443 | - foreach ($this->routes as $route){ |
|
| 476 | + public function before($callbacks) { |
|
| 477 | + foreach ($this->routes as $route) { |
|
| 444 | 478 | $route->before($callbacks); |
| 445 | 479 | } |
| 446 | 480 | return $this; |
| 447 | 481 | } |
| 448 | 482 | |
| 449 | - public function after($callbacks){ |
|
| 450 | - foreach ($this->routes as $route){ |
|
| 483 | + public function after($callbacks) { |
|
| 484 | + foreach ($this->routes as $route) { |
|
| 451 | 485 | $route->after($callbacks); |
| 452 | 486 | } |
| 453 | 487 | return $this; |