@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace HnrAzevedo\Router; |
| 6 | 6 | |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | protected function sorted(?bool $sorted = null): bool |
| 14 | 14 | { |
| 15 | - if(null !== $sorted){ |
|
| 15 | + if (null !== $sorted) { |
|
| 16 | 16 | $this->sorted = $sorted; |
| 17 | 17 | } |
| 18 | 18 | return $this->sorted; |
@@ -20,35 +20,35 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | protected function sortRoutes(): bool |
| 22 | 22 | { |
| 23 | - if($this->sorted()){ |
|
| 23 | + if ($this->sorted()) { |
|
| 24 | 24 | return true; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | $staticRoutes = []; |
| 28 | 28 | $paramRoutes = []; |
| 29 | 29 | |
| 30 | - foreach($this->getRoutes() as $r => $route){ |
|
| 30 | + foreach ($this->getRoutes() as $r => $route) { |
|
| 31 | 31 | |
| 32 | 32 | $path = urldecode(unserialize($route['uri'])->getPath()); |
| 33 | 33 | |
| 34 | - if(strstr($path,'{')){ |
|
| 35 | - $paramRoutes[$this->getKeyArray(substr_count($path,'/') + substr_count($path,'{'),$paramRoutes)] = $route; |
|
| 34 | + if (strstr($path, '{')) { |
|
| 35 | + $paramRoutes[$this->getKeyArray(substr_count($path, '/')+substr_count($path, '{'), $paramRoutes)] = $route; |
|
| 36 | 36 | continue; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - $staticRoutes[$this->getKeyArray(substr_count($path,'/'),$staticRoutes)] = $route; |
|
| 39 | + $staticRoutes[$this->getKeyArray(substr_count($path, '/'), $staticRoutes)] = $route; |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | rsort($paramRoutes); |
| 43 | 43 | rsort($staticRoutes); |
| 44 | 44 | |
| 45 | - $this->orderRoutes(array_merge($staticRoutes,$paramRoutes)); |
|
| 45 | + $this->orderRoutes(array_merge($staticRoutes, $paramRoutes)); |
|
| 46 | 46 | return $this->sorted(true); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | private function getKeyArray(int $index, array $array): int |
| 50 | 50 | { |
| 51 | - while(array_key_exists($index,$array)){ |
|
| 51 | + while (array_key_exists($index, $array)) { |
|
| 52 | 52 | $index++; |
| 53 | 53 | } |
| 54 | 54 | return $index; |
@@ -57,8 +57,8 @@ discard block |
||
| 57 | 57 | private function orderRoutes(array $routes):void |
| 58 | 58 | { |
| 59 | 59 | $kRoutes = $routes; |
| 60 | - foreach($routes as $r => $route){ |
|
| 61 | - if(array_key_exists('name',$route)){ |
|
| 60 | + foreach ($routes as $r => $route) { |
|
| 61 | + if (array_key_exists('name', $route)) { |
|
| 62 | 62 | unset($kRoutes[$r]); |
| 63 | 63 | $kRoutes["'{$route['name']}'"] = $route; |
| 64 | 64 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace HnrAzevedo\Router; |
| 6 | 6 | |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | public static function group(string $prefix, \Closure $closure): Router |
| 34 | 34 | { |
| 35 | 35 | $id = sha1(date('d/m/Y h:m:i')); |
| 36 | - while(array_key_exists($id, self::getInstance()->groupsName)){ |
|
| 36 | + while (array_key_exists($id, self::getInstance()->groupsName)) { |
|
| 37 | 37 | $id = sha1(date('d/m/Y h:m:i').$id); |
| 38 | 38 | } |
| 39 | 39 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | { |
| 55 | 55 | self::getInstance()->loaded = true; |
| 56 | 56 | |
| 57 | - if(null !== $name){ |
|
| 57 | + if (null !== $name) { |
|
| 58 | 58 | return self::getInstance()->loadByName($name); |
| 59 | 59 | } |
| 60 | 60 | |
@@ -62,15 +62,15 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | $requestMethod = (isset($_REQUEST['REQUEST_METHOD'])) ? $_REQUEST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']; |
| 64 | 64 | |
| 65 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 65 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 66 | 66 | self::getInstance()->currentRoute = $route; |
| 67 | 67 | self::getInstance()->currentRoute['name'] = $r; |
| 68 | 68 | |
| 69 | - try{ |
|
| 69 | + try { |
|
| 70 | 70 | self::getInstance()->checkMethod($route, $requestMethod); |
| 71 | 71 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
| 72 | 72 | return self::getInstance(); |
| 73 | - }catch(\Exception $er){ |
|
| 73 | + }catch (\Exception $er) { |
|
| 74 | 74 | continue; |
| 75 | 75 | } |
| 76 | 76 | } |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | |
| 85 | 85 | public static function run(?string $name = null): RouterInterface |
| 86 | 86 | { |
| 87 | - if(!self::getInstance()->loaded){ |
|
| 87 | + if (!self::getInstance()->loaded) { |
|
| 88 | 88 | self::getInstance()->load($name); |
| 89 | 89 | } |
| 90 | 90 | |
@@ -94,10 +94,10 @@ discard block |
||
| 94 | 94 | |
| 95 | 95 | self::getInstance()->executeBefore(); |
| 96 | 96 | |
| 97 | - try{ |
|
| 97 | + try { |
|
| 98 | 98 | $action = unserialize(self::getInstance()->current()['action']); |
| 99 | - self::getInstance()->executeRouteAction( (is_string($action)) ? $action : $action->getClosure() ); |
|
| 100 | - }catch(\Exception $er){ |
|
| 99 | + self::getInstance()->executeRouteAction((is_string($action)) ? $action : $action->getClosure()); |
|
| 100 | + }catch (\Exception $er) { |
|
| 101 | 101 | self::getInstance()->error = $er; |
| 102 | 102 | } |
| 103 | 103 | |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | |
| 111 | 111 | private function checkError(): void |
| 112 | 112 | { |
| 113 | - if(isset($this->error)){ |
|
| 113 | + if (isset($this->error)) { |
|
| 114 | 114 | throw $this->error; |
| 115 | 115 | } |
| 116 | 116 | } |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | |
| 125 | 125 | public static function routes(?array $routes = null): array |
| 126 | 126 | { |
| 127 | - if(null !== $routes){ |
|
| 127 | + if (null !== $routes) { |
|
| 128 | 128 | self::getInstance()->setRoutes($routes); |
| 129 | 129 | self::getInstance()->sortRoutes(); |
| 130 | 130 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace HnrAzevedo\Router; |
| 6 | 6 | |
@@ -13,32 +13,32 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | public static function get(string $uri, $closure): RouterInterface |
| 15 | 15 | { |
| 16 | - return self::set('get',$uri,$closure); |
|
| 16 | + return self::set('get', $uri, $closure); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public static function post(string $uri, $closure): RouterInterface |
| 20 | 20 | { |
| 21 | - return self::set('post',$uri,$closure); |
|
| 21 | + return self::set('post', $uri, $closure); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | public static function ajax(string $uri, $closure): RouterInterface |
| 25 | 25 | { |
| 26 | - return self::set('ajax',$uri,$closure); |
|
| 26 | + return self::set('ajax', $uri, $closure); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | public static function delete(string $uri, $closure): RouterInterface |
| 30 | 30 | { |
| 31 | - return self::set('delete',$uri,$closure); |
|
| 31 | + return self::set('delete', $uri, $closure); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | public static function put(string $uri, $closure): RouterInterface |
| 35 | 35 | { |
| 36 | - return self::set('put',$uri,$closure); |
|
| 36 | + return self::set('put', $uri, $closure); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | public static function patch(string $uri, $closure): RouterInterface |
| 40 | 40 | { |
| 41 | - return self::set('patch',$uri,$closure); |
|
| 41 | + return self::set('patch', $uri, $closure); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | public static function match(string $method, string $uri, $closure): RouterInterface |
@@ -49,22 +49,22 @@ discard block |
||
| 49 | 49 | |
| 50 | 50 | public static function any(string $uri, $closure): RouterInterface |
| 51 | 51 | { |
| 52 | - return self::set('*',$uri,$closure); |
|
| 52 | + return self::set('*', $uri, $closure); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | private static $count = 0; |
| 56 | 56 | |
| 57 | 57 | private static function set(string $method, string $uri, $closure): RouterInterface |
| 58 | 58 | { |
| 59 | - $uri = (substr($uri,0,1) !=='/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
| 59 | + $uri = (substr($uri, 0, 1) !== '/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
| 60 | 60 | |
| 61 | - self::checkDuplicity($uri,$method); |
|
| 61 | + self::checkDuplicity($uri, $method); |
|
| 62 | 62 | |
| 63 | 63 | $routes = self::getInstance()->getRoutes(); |
| 64 | 64 | |
| 65 | 65 | $index = sha1(date('d/m/Y H:i:s').count($routes)); |
| 66 | 66 | |
| 67 | - while(array_key_exists($index,$routes)){ |
|
| 67 | + while (array_key_exists($index, $routes)) { |
|
| 68 | 68 | $index = sha1(date('d/m/Y H:i:s').rand(count($routes))); |
| 69 | 69 | } |
| 70 | 70 | |
@@ -88,8 +88,8 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | private static function checkDuplicity(string $uri, string $method): void |
| 90 | 90 | { |
| 91 | - foreach(self::getInstance()->getRoutes() as $route){ |
|
| 92 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 91 | + foreach (self::getInstance()->getRoutes() as $route) { |
|
| 92 | + if (md5($route['uri'].$route['method']) === md5($uri.$method)) { |
|
| 93 | 93 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
| 94 | 94 | } |
| 95 | 95 | } |