@@ -20,6 +20,9 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | const PARAMETER_IDENTIFIER = ':'; |
| 22 | 22 | |
| 23 | + /** |
|
| 24 | + * @param string $path |
|
| 25 | + */ |
|
| 23 | 26 | public function __construct($path) { |
| 24 | 27 | $this->path = $path; |
| 25 | 28 | } |
@@ -65,14 +65,14 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | |
| 67 | 67 | public function countParams($pattern) { |
| 68 | - if($this->hasParams($pattern) === false) { |
|
| 68 | + if ($this->hasParams($pattern) === false) { |
|
| 69 | 69 | return 0; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | $pattern = str_split($pattern); // split string in characters |
| 73 | 73 | $totalParams = 0; |
| 74 | - for($i = 0; $i < count($pattern); $i++) { |
|
| 75 | - if($pattern[$i] === self::PARAMETER_IDENTIFIER) { |
|
| 74 | + for ($i = 0; $i < count($pattern); $i++) { |
|
| 75 | + if ($pattern[$i] === self::PARAMETER_IDENTIFIER) { |
|
| 76 | 76 | $totalParams++; |
| 77 | 77 | } |
| 78 | 78 | } |
@@ -90,15 +90,15 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | |
| 92 | 92 | public function getParams($pattern) { |
| 93 | - if(!$this->hasParams($pattern)) { |
|
| 93 | + if (!$this->hasParams($pattern)) { |
|
| 94 | 94 | return []; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | $pattern = explode('/', $pattern); |
| 98 | 98 | $path = explode('/', $this->path); |
| 99 | 99 | $params = []; |
| 100 | - for($i = 0; $i < count($pattern); $i++) { |
|
| 101 | - if(strpos($pattern[$i], self::PARAMETER_IDENTIFIER) !== false) { |
|
| 100 | + for ($i = 0; $i < count($pattern); $i++) { |
|
| 101 | + if (strpos($pattern[$i], self::PARAMETER_IDENTIFIER) !== false) { |
|
| 102 | 102 | $params[substr($pattern[$i], 1)] = $path[$i]; |
| 103 | 103 | } |
| 104 | 104 | } |
@@ -117,20 +117,20 @@ discard block |
||
| 117 | 117 | $pattern = explode('/', $pattern); |
| 118 | 118 | $path = explode('/', $this->path); |
| 119 | 119 | |
| 120 | - if(count($pattern) !== count($path)) { |
|
| 120 | + if (count($pattern) !== count($path)) { |
|
| 121 | 121 | return false; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | $found = true; |
| 125 | 125 | $index = 0; |
| 126 | - while($found && $index < count($pattern)) { |
|
| 126 | + while ($found && $index < count($pattern)) { |
|
| 127 | 127 | $check = true; |
| 128 | - if(strpos($pattern[$index], self::PARAMETER_IDENTIFIER) !== false) { |
|
| 128 | + if (strpos($pattern[$index], self::PARAMETER_IDENTIFIER) !== false) { |
|
| 129 | 129 | $check = false; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - if($check) { |
|
| 133 | - if (!preg_match('/^'. $pattern[$index] .'$/', $path[$index])) { |
|
| 132 | + if ($check) { |
|
| 133 | + if (!preg_match('/^'.$pattern[$index].'$/', $path[$index])) { |
|
| 134 | 134 | $found = false; |
| 135 | 135 | } |
| 136 | 136 | } |
@@ -44,12 +44,12 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | public function __construct($uri, $method, $response, array $middlewares = []) { |
| 46 | 46 | $this->uri = $uri; |
| 47 | - if(!in_array(mb_strtoupper($method), $this->availableRequestMethods)) { |
|
| 47 | + if (!in_array(mb_strtoupper($method), $this->availableRequestMethods)) { |
|
| 48 | 48 | throw new MethodException("The request method is invalid"); |
| 49 | 49 | } |
| 50 | 50 | $this->method = $method; |
| 51 | 51 | $this->response = $response; |
| 52 | - if($this->isValidMiddleware($middlewares)) { |
|
| 52 | + if ($this->isValidMiddleware($middlewares)) { |
|
| 53 | 53 | $this->middlewares = $middlewares; |
| 54 | 54 | } |
| 55 | 55 | } |
@@ -126,15 +126,15 @@ discard block |
||
| 126 | 126 | |
| 127 | 127 | private function isValidMiddleware($middleware) { |
| 128 | 128 | // if there is not middlewares then it is valid |
| 129 | - if(count($middleware) === 0) { |
|
| 129 | + if (count($middleware) === 0) { |
|
| 130 | 130 | return true; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - foreach($middleware as $key => $value) { |
|
| 134 | - if(!in_array($key, $this->middlewareTypes)) { |
|
| 133 | + foreach ($middleware as $key => $value) { |
|
| 134 | + if (!in_array($key, $this->middlewareTypes)) { |
|
| 135 | 135 | throw new InvalidMiddlewareException("Only before and after middleware types are valid"); |
| 136 | 136 | } |
| 137 | - if(!is_callable($value)) { |
|
| 137 | + if (!is_callable($value)) { |
|
| 138 | 138 | throw new InvalidMiddlewareException("The middleware must be callable"); |
| 139 | 139 | } |
| 140 | 140 | } |
@@ -118,12 +118,12 @@ discard block |
||
| 118 | 118 | $found = false; |
| 119 | 119 | $counter = 0; |
| 120 | 120 | |
| 121 | - if(count($this->routes) === 0) { |
|
| 121 | + if (count($this->routes) === 0) { |
|
| 122 | 122 | return $found; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - while($found === false && $counter < count($this->routes)) { |
|
| 126 | - if($this->routes[$counter]->getUri() === $uri && $this->routes[$counter]->getMethod() === $method) { |
|
| 125 | + while ($found === false && $counter < count($this->routes)) { |
|
| 126 | + if ($this->routes[$counter]->getUri() === $uri && $this->routes[$counter]->getMethod() === $method) { |
|
| 127 | 127 | $found = true; |
| 128 | 128 | } |
| 129 | 129 | |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | */ |
| 144 | 144 | |
| 145 | 145 | private function addRoute($uri, $method, $response, array $middleware = []) { |
| 146 | - if($this->find($uri, $method)) { // search if exists an apparition |
|
| 146 | + if ($this->find($uri, $method)) { // search if exists an apparition |
|
| 147 | 147 | return false; |
| 148 | 148 | } |
| 149 | 149 | |
@@ -159,13 +159,13 @@ discard block |
||
| 159 | 159 | */ |
| 160 | 160 | private function getRequestHeaders() { |
| 161 | 161 | // If getallheaders() is available, use that |
| 162 | - if(function_exists('getallheaders')) { |
|
| 162 | + if (function_exists('getallheaders')) { |
|
| 163 | 163 | return getallheaders(); |
| 164 | 164 | } |
| 165 | 165 | // Method getallheaders() not available: manually extract 'm |
| 166 | 166 | $headers = []; |
| 167 | - foreach(filter_input_array(INPUT_SERVER) as $key => $value) { |
|
| 168 | - if((substr($key, 0, 5) == 'HTTP_') || ($key == 'CONTENT_TYPE') || ($key == 'CONTENT_LENGTH')) { |
|
| 167 | + foreach (filter_input_array(INPUT_SERVER) as $key => $value) { |
|
| 168 | + if ((substr($key, 0, 5) == 'HTTP_') || ($key == 'CONTENT_TYPE') || ($key == 'CONTENT_LENGTH')) { |
|
| 169 | 169 | $headers[str_replace([' ', 'Http'], ['-', 'HTTP'], ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))))] = $value; |
| 170 | 170 | } |
| 171 | 171 | } |
@@ -179,13 +179,13 @@ discard block |
||
| 179 | 179 | */ |
| 180 | 180 | |
| 181 | 181 | private function overrideMethod() { |
| 182 | - if($this->requestMethod === 'HEAD') { |
|
| 182 | + if ($this->requestMethod === 'HEAD') { |
|
| 183 | 183 | $this->requestMethod = 'GET'; |
| 184 | - }elseif($this->requestMethod === 'POST' && filter_input(INPUT_POST, '_method') !== null) { |
|
| 184 | + }elseif ($this->requestMethod === 'POST' && filter_input(INPUT_POST, '_method') !== null) { |
|
| 185 | 185 | $this->requestMethod = filter_input(INPUT_POST, '_method'); |
| 186 | - }elseif($this->requestMethod === 'POST') { |
|
| 186 | + }elseif ($this->requestMethod === 'POST') { |
|
| 187 | 187 | $headers = $this->getRequestHeaders(); |
| 188 | - if(isset($headers['X-HTTP-Method-Override']) && in_array($headers['X-HTTP-Method-Override'], ['PUT', 'DELETE', 'PATCH'])) { |
|
| 188 | + if (isset($headers['X-HTTP-Method-Override']) && in_array($headers['X-HTTP-Method-Override'], ['PUT', 'DELETE', 'PATCH'])) { |
|
| 189 | 189 | $this->requestMethod = $headers['X-HTTP-Method-Override']; |
| 190 | 190 | } |
| 191 | 191 | } |
@@ -264,8 +264,8 @@ discard block |
||
| 264 | 264 | */ |
| 265 | 265 | |
| 266 | 266 | public function both($uri, $response, $methods = Method::GET, array $middleware = []) { |
| 267 | - if(is_array($methods)) { |
|
| 268 | - foreach($methods as $method) { |
|
| 267 | + if (is_array($methods)) { |
|
| 268 | + foreach ($methods as $method) { |
|
| 269 | 269 | $this->addRoute($uri, $method, $response, $middleware); |
| 270 | 270 | } |
| 271 | 271 | } else { |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | */ |
| 293 | 293 | |
| 294 | 294 | public function notFound($func) { |
| 295 | - if(is_callable($func)) { |
|
| 295 | + if (is_callable($func)) { |
|
| 296 | 296 | $this->notFound = $func; |
| 297 | 297 | } |
| 298 | 298 | } |
@@ -310,17 +310,17 @@ discard block |
||
| 310 | 310 | public function run() { |
| 311 | 311 | $found = false; |
| 312 | 312 | $counter = 0; |
| 313 | - while($found === false && $counter < count($this->routes)) { |
|
| 314 | - if($this->routerParser->match($this->routes[$counter]->getUri()) && $this->routes[$counter]->getMethod() === $this->requestMethod) { |
|
| 313 | + while ($found === false && $counter < count($this->routes)) { |
|
| 314 | + if ($this->routerParser->match($this->routes[$counter]->getUri()) && $this->routes[$counter]->getMethod() === $this->requestMethod) { |
|
| 315 | 315 | $found = true; |
| 316 | 316 | } else { |
| 317 | 317 | $counter++; |
| 318 | 318 | } |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | - if($found) { |
|
| 321 | + if ($found) { |
|
| 322 | 322 | // run the before middleware if it exists |
| 323 | - if($this->routes[$counter]->hasBefore()) { |
|
| 323 | + if ($this->routes[$counter]->hasBefore()) { |
|
| 324 | 324 | call_user_func($this->routes[$counter]->getMiddlewares()['before']); |
| 325 | 325 | } |
| 326 | 326 | |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | $response = call_user_func_array($this->routes[$counter]->getResponse(), $params); |
| 329 | 329 | |
| 330 | 330 | // run the after middleware if it exists |
| 331 | - if($this->routes[$counter]->hasAfter()) { |
|
| 331 | + if ($this->routes[$counter]->hasAfter()) { |
|
| 332 | 332 | call_user_func($this->routes[$counter]->getMiddlewares()['after']); |
| 333 | 333 | } |
| 334 | 334 | |
@@ -181,9 +181,9 @@ |
||
| 181 | 181 | private function overrideMethod() { |
| 182 | 182 | if($this->requestMethod === 'HEAD') { |
| 183 | 183 | $this->requestMethod = 'GET'; |
| 184 | - }elseif($this->requestMethod === 'POST' && filter_input(INPUT_POST, '_method') !== null) { |
|
| 184 | + } elseif($this->requestMethod === 'POST' && filter_input(INPUT_POST, '_method') !== null) { |
|
| 185 | 185 | $this->requestMethod = filter_input(INPUT_POST, '_method'); |
| 186 | - }elseif($this->requestMethod === 'POST') { |
|
| 186 | + } elseif($this->requestMethod === 'POST') { |
|
| 187 | 187 | $headers = $this->getRequestHeaders(); |
| 188 | 188 | if(isset($headers['X-HTTP-Method-Override']) && in_array($headers['X-HTTP-Method-Override'], ['PUT', 'DELETE', 'PATCH'])) { |
| 189 | 189 | $this->requestMethod = $headers['X-HTTP-Method-Override']; |