@@ -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 | |
@@ -22,8 +22,8 @@ discard block  | 
                                                    ||
| 22 | 22 | |
| 23 | 23 | public static function globalMiddlewares(array $middlewares): RouterInterface  | 
                                                        
| 24 | 24 |      { | 
                                                        
| 25 | -        foreach($middlewares as $middleware){ | 
                                                        |
| 26 | -            if(!class_exists($middleware)){ | 
                                                        |
| 25 | +        foreach ($middlewares as $middleware) { | 
                                                        |
| 26 | +            if (!class_exists($middleware)) { | 
                                                        |
| 27 | 27 |                  throw new \RuntimeException("Middleware class {$middleware} not exists"); | 
                                                        
| 28 | 28 | }  | 
                                                        
| 29 | 29 | }  | 
                                                        
@@ -39,8 +39,8 @@ discard block  | 
                                                    ||
| 39 | 39 | public static function middleware(array $middlewares): RouterInterface  | 
                                                        
| 40 | 40 |      { | 
                                                        
| 41 | 41 | $route = self::getInstance()->inSave();  | 
                                                        
| 42 | - $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares;  | 
                                                        |
| 43 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->getRoutes()));  | 
                                                        |
| 42 | + $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'], $middlewares) : $middlewares;  | 
                                                        |
| 43 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes()));  | 
                                                        |
| 44 | 44 | return self::getInstance();  | 
                                                        
| 45 | 45 | }  | 
                                                        
| 46 | 46 | |
@@ -48,8 +48,8 @@ discard block  | 
                                                    ||
| 48 | 48 |      { | 
                                                        
| 49 | 49 | $excepts = (is_array($excepts)) ? $excepts : [];  | 
                                                        
| 50 | 50 | $group = self::getInstance()->inSave()['group'];  | 
                                                        
| 51 | -        foreach(self::getInstance()->getRoutes() as $r => $route){ | 
                                                        |
| 52 | -            if($route['group'] !== $group || in_array($route['name'], $excepts)){ | 
                                                        |
| 51 | +        foreach (self::getInstance()->getRoutes() as $r => $route) { | 
                                                        |
| 52 | +            if ($route['group'] !== $group || in_array($route['name'], $excepts)) { | 
                                                        |
| 53 | 53 | continue;  | 
                                                        
| 54 | 54 | }  | 
                                                        
| 55 | 55 | |
@@ -61,7 +61,7 @@ discard block  | 
                                                    ||
| 61 | 61 | |
| 62 | 62 | private static function existMiddleware(string $name): void  | 
                                                        
| 63 | 63 |      { | 
                                                        
| 64 | -        if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){ | 
                                                        |
| 64 | +        if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) { | 
                                                        |
| 65 | 65 |              throw new \RuntimeException("Middleware {$name} does not exist"); | 
                                                        
| 66 | 66 | }  | 
                                                        
| 67 | 67 | }  | 
                                                        
@@ -70,9 +70,9 @@ discard block  | 
                                                    ||
| 70 | 70 |      { | 
                                                        
| 71 | 71 | $factory = new Factory();  | 
                                                        
| 72 | 72 | |
| 73 | - $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], $this->current()['uri'],['route' => $this->current()]) : $this->serverRequest;  | 
                                                        |
| 73 | + $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], $this->current()['uri'], ['route' => $this->current()]) : $this->serverRequest;  | 
                                                        |
| 74 | 74 | |
| 75 | -        foreach ($this->current()['middlewares'] as $middleware){ | 
                                                        |
| 75 | +        foreach ($this->current()['middlewares'] as $middleware) { | 
                                                        |
| 76 | 76 | $this->currentMiddlewares[] = (class_exists($middleware)) ? new $middleware() : new $this->globalMiddlewares[$middleware]();  | 
                                                        
| 77 | 77 | }  | 
                                                        
| 78 | 78 | |
@@ -87,20 +87,20 @@ discard block  | 
                                                    ||
| 87 | 87 | |
| 88 | 88 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface  | 
                                                        
| 89 | 89 |      { | 
                                                        
| 90 | -        if(!$this->getInstance()->loaded()){ | 
                                                        |
| 90 | +        if (!$this->getInstance()->loaded()) { | 
                                                        |
| 91 | 91 | $this->getInstance()->load();  | 
                                                        
| 92 | 92 | }  | 
                                                        
| 93 | 93 | |
| 94 | 94 | $route = $this->getInstance()->current();  | 
                                                        
| 95 | -        if(!empty($route)){ | 
                                                        |
| 96 | -            $route['action'] = function(){ | 
                                                        |
| 95 | +        if (!empty($route)) { | 
                                                        |
| 96 | +            $route['action'] = function() { | 
                                                        |
| 97 | 97 | $this->getInstance()->executeBefore();  | 
                                                        
| 98 | 98 | $this->getInstance()->executeRouteAction($this->getInstance()->current()['action']);  | 
                                                        
| 99 | 99 | $this->getInstance()->executeAfter();  | 
                                                        
| 100 | 100 | };  | 
                                                        
| 101 | 101 | }  | 
                                                        
| 102 | 102 | |
| 103 | -        return $this->next($handler)->handle($request->withAttribute('route',$route)); | 
                                                        |
| 103 | +        return $this->next($handler)->handle($request->withAttribute('route', $route)); | 
                                                        |
| 104 | 104 | }  | 
                                                        
| 105 | 105 | |
| 106 | 106 | private function next(RequestHandlerInterface $defaultHandler): RequestHandlerInterface  | 
                                                        
@@ -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 | |
@@ -23,8 +23,8 @@ discard block  | 
                                                    ||
| 23 | 23 | $excepts = (is_array($excepts)) ? $excepts : [];  | 
                                                        
| 24 | 24 | |
| 25 | 25 | $group = self::getInstance()->inSave()['group'];  | 
                                                        
| 26 | -        foreach(self::getInstance()->getRoutes() as $r => $route){ | 
                                                        |
| 27 | -            if($route['group'] !== $group || in_array($route['name'], $excepts)){ | 
                                                        |
| 26 | +        foreach (self::getInstance()->getRoutes() as $r => $route) { | 
                                                        |
| 27 | +            if ($route['group'] !== $group || in_array($route['name'], $excepts)) { | 
                                                        |
| 28 | 28 | continue;  | 
                                                        
| 29 | 29 | }  | 
                                                        
| 30 | 30 | |
@@ -49,30 +49,30 @@ discard block  | 
                                                    ||
| 49 | 49 | unset($uriPath[0]);  | 
                                                        
| 50 | 50 | |
| 51 | 51 | $corretRoute = true;  | 
                                                        
| 52 | -        foreach ($routePath as $r => $routeFrag){ | 
                                                        |
| 52 | +        foreach ($routePath as $r => $routeFrag) { | 
                                                        |
| 53 | 53 | $where = is_array($route['where']) ? $route['where'] : [];  | 
                                                        
| 54 | 54 | $routeFrag = $this->replaceParam($where, $routeFrag, $uriPath[$r]);  | 
                                                        
| 55 | 55 | |
| 56 | -            if($routeFrag !== $uriPath[$r]){ | 
                                                        |
| 56 | +            if ($routeFrag !== $uriPath[$r]) { | 
                                                        |
| 57 | 57 | $corretRoute = false;  | 
                                                        
| 58 | 58 | }  | 
                                                        
| 59 | 59 | }  | 
                                                        
| 60 | 60 | |
| 61 | -        if(!$corretRoute){ | 
                                                        |
| 61 | +        if (!$corretRoute) { | 
                                                        |
| 62 | 62 |              throw new \Exception('continue'); | 
                                                        
| 63 | 63 | }  | 
                                                        
| 64 | 64 | |
| 65 | - $_REQUEST = array_merge($_REQUEST,$this->parameters);  | 
                                                        |
| 65 | + $_REQUEST = array_merge($_REQUEST, $this->parameters);  | 
                                                        |
| 66 | 66 | }  | 
                                                        
| 67 | 67 | |
| 68 | 68 | private function replaceParam(array $where, string $ref, string $value): string  | 
                                                        
| 69 | 69 |      { | 
                                                        
| 70 | -        if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) { | 
                                                        |
| 71 | -            $this->parameters[str_replace(['{:','{','}'],'',$ref)] = $value; | 
                                                        |
| 70 | +        if (((substr($ref, 0, 1) === '{') && (substr($ref, strlen($ref)-1) === '}'))) { | 
                                                        |
| 71 | +            $this->parameters[str_replace(['{:', '{', '}'], '', $ref)] = $value; | 
                                                        |
| 72 | 72 | |
| 73 | - $this->checkValueRequire($ref,$value);  | 
                                                        |
| 73 | + $this->checkValueRequire($ref, $value);  | 
                                                        |
| 74 | 74 | |
| 75 | -            if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){ | 
                                                        |
| 75 | +            if (array_key_exists(str_replace(['{:', '{', '}'], '', $ref), $where)) { | 
                                                        |
| 76 | 76 | $this->matchParam($where, $ref, $value);  | 
                                                        
| 77 | 77 | }  | 
                                                        
| 78 | 78 | |
@@ -83,25 +83,25 @@ discard block  | 
                                                    ||
| 83 | 83 | |
| 84 | 84 | private function checkValueRequire(string $ref, string $value): void  | 
                                                        
| 85 | 85 |      { | 
                                                        
| 86 | -        if(substr($ref,0,2) !== '{:' && strlen($value) === 0){ | 
                                                        |
| 86 | +        if (substr($ref, 0, 2) !== '{:' && strlen($value) === 0) { | 
                                                        |
| 87 | 87 |              throw new \Exception('continue'); | 
                                                        
| 88 | 88 | }  | 
                                                        
| 89 | 89 | }  | 
                                                        
| 90 | 90 | |
| 91 | 91 | private function checkCount(string $routePath, string $uriPath): void  | 
                                                        
| 92 | 92 |      { | 
                                                        
| 93 | -        $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); | 
                                                        |
| 94 | -        $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); | 
                                                        |
| 93 | +        $countRequest = substr_count($uriPath, '/')-substr_count($routePath, '{:'); | 
                                                        |
| 94 | +        $countRoute = substr_count($routePath, '/')-substr_count($routePath, '{:'); | 
                                                        |
| 95 | 95 | |
| 96 | -        if($countRequest !== $countRoute){ | 
                                                        |
| 96 | +        if ($countRequest !== $countRoute) { | 
                                                        |
| 97 | 97 |              throw new \Exception('continue'); | 
                                                        
| 98 | 98 | }  | 
                                                        
| 99 | 99 | }  | 
                                                        
| 100 | 100 | |
| 101 | 101 | private function matchParam(array $where, string $ref, string $value): void  | 
                                                        
| 102 | 102 |      { | 
                                                        
| 103 | -        if(substr($ref,0,2) === '{' || $value !== ''){ | 
                                                        |
| 104 | -            if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){ | 
                                                        |
| 103 | +        if (substr($ref, 0, 2) === '{' || $value !== '') { | 
                                                        |
| 104 | +            if (!preg_match("/^{$where[str_replace(['{:', '{', '}'], '', $ref)]}$/", $value)) { | 
                                                        |
| 105 | 105 |                  throw new \Exception('continue'); | 
                                                        
| 106 | 106 | }  | 
                                                        
| 107 | 107 | }  | 
                                                        
@@ -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 | |
@@ -17,7 +17,7 @@ discard block  | 
                                                    ||
| 17 | 17 | |
| 18 | 18 | protected function getState(string $state, bool $except = false)  | 
                                                        
| 19 | 19 |      { | 
                                                        
| 20 | -        if($state === 'before'){ | 
                                                        |
| 20 | +        if ($state === 'before') { | 
                                                        |
| 21 | 21 | return ($except) ? $this->beforeExcepts : $this->beforeAll;  | 
                                                        
| 22 | 22 | }  | 
                                                        
| 23 | 23 | |
@@ -26,8 +26,8 @@ discard block  | 
                                                    ||
| 26 | 26 | |
| 27 | 27 | protected function setState(string $state, $settable, bool $except = false): bool  | 
                                                        
| 28 | 28 |      { | 
                                                        
| 29 | -        if($state === 'before'){ | 
                                                        |
| 30 | -            if($except){ | 
                                                        |
| 29 | +        if ($state === 'before') { | 
                                                        |
| 30 | +            if ($except) { | 
                                                        |
| 31 | 31 | $this->beforeExcepts = $settable;  | 
                                                        
| 32 | 32 | return true;  | 
                                                        
| 33 | 33 | }  | 
                                                        
@@ -36,7 +36,7 @@ discard block  | 
                                                    ||
| 36 | 36 | return true;  | 
                                                        
| 37 | 37 | }  | 
                                                        
| 38 | 38 | |
| 39 | -        if($except){ | 
                                                        |
| 39 | +        if ($except) { | 
                                                        |
| 40 | 40 | $this->afterExcepts = $settable;  | 
                                                        
| 41 | 41 | return true;  | 
                                                        
| 42 | 42 | }  | 
                                                        
@@ -57,14 +57,14 @@ discard block  | 
                                                    ||
| 57 | 57 | |
| 58 | 58 | public static function beforeAll($closure, ?array $excepts = null): RouterInterface  | 
                                                        
| 59 | 59 |      { | 
                                                        
| 60 | -        self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [] ,true); | 
                                                        |
| 60 | +        self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [], true); | 
                                                        |
| 61 | 61 |          self::getInstance()->setState('before', $closure, false); | 
                                                        
| 62 | 62 | return self::getInstance();  | 
                                                        
| 63 | 63 | }  | 
                                                        
| 64 | 64 | |
| 65 | 65 | public static function afterAll($closure, ?array $excepts = null): RouterInterface  | 
                                                        
| 66 | 66 |      { | 
                                                        
| 67 | -        self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [] ,true); | 
                                                        |
| 67 | +        self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [], true); | 
                                                        |
| 68 | 68 |          self::getInstance()->setState('after', $closure, false); | 
                                                        
| 69 | 69 | return self::getInstance();  | 
                                                        
| 70 | 70 | }  | 
                                                        
@@ -81,7 +81,7 @@ discard block  | 
                                                    ||
| 81 | 81 | |
| 82 | 82 | protected function executeRouteAction($action): bool  | 
                                                        
| 83 | 83 |      { | 
                                                        
| 84 | -        if(is_callable($action)){         | 
                                                        |
| 84 | +        if (is_callable($action)) {         | 
                                                        |
| 85 | 85 | call_user_func_array($action, $_REQUEST);  | 
                                                        
| 86 | 86 | return true;  | 
                                                        
| 87 | 87 | }  | 
                                                        
@@ -96,9 +96,9 @@ discard block  | 
                                                    ||
| 96 | 96 | $excepts = (is_array($excepts)) ? $excepts : [];  | 
                                                        
| 97 | 97 | $group = self::getInstance()->inSave()['group'];  | 
                                                        
| 98 | 98 | |
| 99 | -        foreach(self::getInstance()->getRoutes() as $r => $route){ | 
                                                        |
| 100 | -            if($route['group'] === $group && !in_array($r, $excepts)){ | 
                                                        |
| 101 | - self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]);  | 
                                                        |
| 99 | +        foreach (self::getInstance()->getRoutes() as $r => $route) { | 
                                                        |
| 100 | +            if ($route['group'] === $group && !in_array($r, $excepts)) { | 
                                                        |
| 101 | + self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]);  | 
                                                        |
| 102 | 102 | }  | 
                                                        
| 103 | 103 | }  | 
                                                        
| 104 | 104 | |
@@ -108,14 +108,14 @@ discard block  | 
                                                    ||
| 108 | 108 | private static function addInRoute(string $state, $closure): RouterInterface  | 
                                                        
| 109 | 109 |      { | 
                                                        
| 110 | 110 | $route = self::getInstance()->inSave();  | 
                                                        
| 111 | - $route[$state] = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]);  | 
                                                        |
| 112 | - self::updateRoute($route,array_key_last(self::getInstance()->getRoutes()));  | 
                                                        |
| 111 | + $route[$state] = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]);  | 
                                                        |
| 112 | + self::updateRoute($route, array_key_last(self::getInstance()->getRoutes()));  | 
                                                        |
| 113 | 113 | return self::getInstance();  | 
                                                        
| 114 | 114 | }  | 
                                                        
| 115 | 115 | |
| 116 | 116 | protected function executeBefore(): void  | 
                                                        
| 117 | 117 |      { | 
                                                        
| 118 | -        if(!in_array($this->currentName(), (array) $this->getState('before', true))){ | 
                                                        |
| 118 | +        if (!in_array($this->currentName(), (array) $this->getState('before', true))) { | 
                                                        |
| 119 | 119 |              ($this->getState('before', false))(); | 
                                                        
| 120 | 120 | }  | 
                                                        
| 121 | 121 | |
@@ -126,15 +126,15 @@ discard block  | 
                                                    ||
| 126 | 126 |      { | 
                                                        
| 127 | 127 |          $this->executeState('after'); | 
                                                        
| 128 | 128 | |
| 129 | -        if(!in_array($this->currentName(), (array) $this->getState('after', true))){ | 
                                                        |
| 129 | +        if (!in_array($this->currentName(), (array) $this->getState('after', true))) { | 
                                                        |
| 130 | 130 |              ($this->getState('after', false))(); | 
                                                        
| 131 | 131 | }  | 
                                                        
| 132 | 132 | }  | 
                                                        
| 133 | 133 | |
| 134 | 134 | private function executeState(string $stage): void  | 
                                                        
| 135 | 135 |      { | 
                                                        
| 136 | -        foreach($this->current()[$stage] as $state){ | 
                                                        |
| 137 | -            if(is_callable($state)){ | 
                                                        |
| 136 | +        foreach ($this->current()[$stage] as $state) { | 
                                                        |
| 137 | +            if (is_callable($state)) { | 
                                                        |
| 138 | 138 | $state();  | 
                                                        
| 139 | 139 | continue;  | 
                                                        
| 140 | 140 | }  | 
                                                        
@@ -145,31 +145,31 @@ discard block  | 
                                                    ||
| 145 | 145 | |
| 146 | 146 | private function executeController(string $controllerMeth): void  | 
                                                        
| 147 | 147 |      { | 
                                                        
| 148 | -        if(count(explode('@',$controllerMeth)) !== 2){ | 
                                                        |
| 148 | +        if (count(explode('@', $controllerMeth)) !== 2) { | 
                                                        |
| 149 | 149 | $path = urldecode($this->current()['uri']->getPath());  | 
                                                        
| 150 | 150 |              throw new \RuntimeException("Misconfigured route action ({$path})"); | 
                                                        
| 151 | 151 | }  | 
                                                        
| 152 | 152 | |
| 153 | -        $controller = (string) explode('@',$controllerMeth)[0]; | 
                                                        |
| 154 | -        $method = (string) explode('@',$controllerMeth)[1]; | 
                                                        |
| 153 | +        $controller = (string) explode('@', $controllerMeth)[0]; | 
                                                        |
| 154 | +        $method = (string) explode('@', $controllerMeth)[1]; | 
                                                        |
| 155 | 155 | |
| 156 | 156 | $this->checkControllerMeth($controllerMeth);  | 
                                                        
| 157 | 157 | |
| 158 | - call_user_func_array([(new $controller()),$method], $_REQUEST);  | 
                                                        |
| 158 | + call_user_func_array([(new $controller()), $method], $_REQUEST);  | 
                                                        |
| 159 | 159 | }  | 
                                                        
| 160 | 160 | |
| 161 | 161 | private function checkControllerMeth(string $controllerMeth): void  | 
                                                        
| 162 | 162 |      { | 
                                                        
| 163 | -        $routeURI = str_replace(['{:','{','}'],'',urldecode($this->current()['uri']->getPath())); | 
                                                        |
| 163 | +        $routeURI = str_replace(['{:', '{', '}'], '', urldecode($this->current()['uri']->getPath())); | 
                                                        |
| 164 | 164 | |
| 165 | -        $controller = (string) explode('@',$controllerMeth)[0]; | 
                                                        |
| 166 | -        $method = (string) explode('@',$controllerMeth)[1]; | 
                                                        |
| 165 | +        $controller = (string) explode('@', $controllerMeth)[0]; | 
                                                        |
| 166 | +        $method = (string) explode('@', $controllerMeth)[1]; | 
                                                        |
| 167 | 167 | |
| 168 | -        if(!class_exists($controller)){ | 
                                                        |
| 168 | +        if (!class_exists($controller)) { | 
                                                        |
| 169 | 169 |              throw new \RuntimeException("Controller not found in route URI {$routeURI}"); | 
                                                        
| 170 | 170 | }  | 
                                                        
| 171 | 171 | |
| 172 | -        if(!method_exists($controller, $method)){ | 
                                                        |
| 172 | +        if (!method_exists($controller, $method)) { | 
                                                        |
| 173 | 173 |              throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}"); | 
                                                        
| 174 | 174 | }  | 
                                                        
| 175 | 175 | |