@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | { |
| 17 | 17 | $this->hasRouteName($name); |
| 18 | 18 | |
| 19 | - if(!$this->loaded){ |
|
| 19 | + if (!$this->loaded) { |
|
| 20 | 20 | $this->loadIn($name); |
| 21 | 21 | } |
| 22 | 22 | |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | public function hasCurrentRoute(): void |
| 27 | 27 | { |
| 28 | - if(!isset($this->currentRoute)){ |
|
| 28 | + if (!isset($this->currentRoute)) { |
|
| 29 | 29 | throw new \RuntimeException('Route not yet loaded'); |
| 30 | 30 | } |
| 31 | 31 | } |
@@ -10,28 +10,28 @@ discard block |
||
| 10 | 10 | |
| 11 | 11 | protected function hasRouteName(string $name): void |
| 12 | 12 | { |
| 13 | - if(!isset($this->routesName[$name])){ |
|
| 13 | + if (!isset($this->routesName[$name])) { |
|
| 14 | 14 | throw new \RuntimeException("There is no route named with {$name}"); |
| 15 | 15 | } |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | protected function isInNameGroup(): void |
| 19 | 19 | { |
| 20 | - if(!is_null($this->group)){ |
|
| 20 | + if (!is_null($this->group)) { |
|
| 21 | 21 | throw new \RuntimeException("It is not allowed to assign names to groups"); |
| 22 | 22 | } |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | protected function isInPseudGroup(): void |
| 26 | 26 | { |
| 27 | - if(!is_null($this->group)){ |
|
| 27 | + if (!is_null($this->group)) { |
|
| 28 | 28 | throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup"); |
| 29 | 29 | } |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | protected function existRouteName(string $name): void |
| 33 | 33 | { |
| 34 | - if(isset($this->routesName[$name])){ |
|
| 34 | + if (isset($this->routesName[$name])) { |
|
| 35 | 35 | throw new \RuntimeException("There is already a route named with {$name}"); |
| 36 | 36 | } |
| 37 | 37 | } |
@@ -39,12 +39,12 @@ discard block |
||
| 39 | 39 | protected function checkMethod(array $route, $method): void |
| 40 | 40 | { |
| 41 | 41 | $hasMethod = false; |
| 42 | - foreach(explode('|',$route['method']) as $routeMethod){ |
|
| 43 | - if(@preg_match("/{$routeMethod}/",$method) == true || $method === '*'){ |
|
| 42 | + foreach (explode('|', $route['method']) as $routeMethod) { |
|
| 43 | + if (@preg_match("/{$routeMethod}/", $method) == true || $method === '*') { |
|
| 44 | 44 | $hasMethod = true; |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | - if(!$hasMethod){ |
|
| 47 | + if (!$hasMethod) { |
|
| 48 | 48 | throw new \Exception('This route is not released for the accessed method'); |
| 49 | 49 | } |
| 50 | 50 | |
@@ -57,10 +57,10 @@ discard block |
||
| 57 | 57 | |
| 58 | 58 | self::checkDuplicity($uri,$method); |
| 59 | 59 | |
| 60 | - self::getInstance()->routes[] = [ |
|
| 61 | - 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
|
| 62 | - 'action' => $closure, |
|
| 63 | - 'method' => strtoupper($method), |
|
| 60 | + self::getInstance()->routes[] = [ |
|
| 61 | + 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
|
| 62 | + 'action' => $closure, |
|
| 63 | + 'method' => strtoupper($method), |
|
| 64 | 64 | 'middlewares' => null, |
| 65 | 65 | 'where' => null, |
| 66 | 66 | 'before' => null, |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | private static function checkDuplicity(string $uri, string $method): void |
| 76 | 76 | { |
| 77 | 77 | foreach(self::getInstance()->routes as $route){ |
| 78 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 78 | + if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 79 | 79 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -12,32 +12,32 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | public static function get(string $uri, $closure): RouterInterface |
| 14 | 14 | { |
| 15 | - return self::set('get',$uri,$closure); |
|
| 15 | + return self::set('get', $uri, $closure); |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | public static function post(string $uri, $closure): RouterInterface |
| 19 | 19 | { |
| 20 | - return self::set('post',$uri,$closure); |
|
| 20 | + return self::set('post', $uri, $closure); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | public static function ajax(string $uri, $closure): RouterInterface |
| 24 | 24 | { |
| 25 | - return self::set('ajax',$uri,$closure); |
|
| 25 | + return self::set('ajax', $uri, $closure); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | public static function delete(string $uri, $closure): RouterInterface |
| 29 | 29 | { |
| 30 | - return self::set('delete',$uri,$closure); |
|
| 30 | + return self::set('delete', $uri, $closure); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | public static function put(string $uri, $closure): RouterInterface |
| 34 | 34 | { |
| 35 | - return self::set('put',$uri,$closure); |
|
| 35 | + return self::set('put', $uri, $closure); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public static function patch(string $uri, $closure): RouterInterface |
| 39 | 39 | { |
| 40 | - return self::set('patch',$uri,$closure); |
|
| 40 | + return self::set('patch', $uri, $closure); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | public static function match(string $method, string $uri, $closure): RouterInterface |
@@ -48,14 +48,14 @@ discard block |
||
| 48 | 48 | |
| 49 | 49 | public static function any(string $uri, $closure): RouterInterface |
| 50 | 50 | { |
| 51 | - return self::set('*',$uri,$closure); |
|
| 51 | + return self::set('*', $uri, $closure); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | private static function set(string $method, string $uri, $closure): RouterInterface |
| 55 | 55 | { |
| 56 | - $uri = (substr($uri,0,1) !=='/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
| 56 | + $uri = (substr($uri, 0, 1) !== '/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
| 57 | 57 | |
| 58 | - self::checkDuplicity($uri,$method); |
|
| 58 | + self::checkDuplicity($uri, $method); |
|
| 59 | 59 | |
| 60 | 60 | self::getInstance()->routes[] = [ |
| 61 | 61 | 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
@@ -74,8 +74,8 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | private static function checkDuplicity(string $uri, string $method): void |
| 76 | 76 | { |
| 77 | - foreach(self::getInstance()->routes as $route){ |
|
| 78 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 77 | + foreach (self::getInstance()->routes as $route) { |
|
| 78 | + if (md5($route['uri'].$route['method']) === md5($uri.$method)) { |
|
| 79 | 79 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | $route = self::getInstance()->inSave(); |
| 34 | 34 | $route['name'] = $name; |
| 35 | 35 | self::getInstance()->routesNames[$name] = $name; |
| 36 | - self::getInstance()->unsetRoute(count(self::getInstance()->routes)-1)->updateRoute($route,$name); |
|
| 36 | + self::getInstance()->unsetRoute(count(self::getInstance()->routes)-1)->updateRoute($route, $name); |
|
| 37 | 37 | return self::getInstance(); |
| 38 | 38 | } |
| 39 | 39 | |
@@ -71,16 +71,16 @@ discard block |
||
| 71 | 71 | { |
| 72 | 72 | self::getInstance()->loaded = true; |
| 73 | 73 | |
| 74 | - foreach(self::getInstance()->routes as $r => $route){ |
|
| 74 | + foreach (self::getInstance()->routes as $r => $route) { |
|
| 75 | 75 | self::getInstance()->currentRoute = $route; |
| 76 | 76 | self::getInstance()->currentRoute['name'] = $r; |
| 77 | 77 | |
| 78 | - try{ |
|
| 78 | + try { |
|
| 79 | 79 | self::getInstance()->checkMethod($route, $_SERVER['REQUEST_METHOD']); |
| 80 | 80 | self::getInstance()->checkData($route['uri']->getPath(), $_SERVER['REQUEST_URI']); |
| 81 | 81 | self::getInstance()->sortRoutes(); |
| 82 | 82 | return self::getInstance(); |
| 83 | - }catch(\Exception $er){ |
|
| 83 | + }catch (\Exception $er) { |
|
| 84 | 84 | continue; |
| 85 | 85 | } |
| 86 | 86 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | |
| 92 | 92 | public static function run(): RouterInterface |
| 93 | 93 | { |
| 94 | - if(!self::getInstance()->loaded){ |
|
| 94 | + if (!self::getInstance()->loaded) { |
|
| 95 | 95 | self::getInstance()->load(); |
| 96 | 96 | } |
| 97 | 97 | |
@@ -80,7 +80,7 @@ |
||
| 80 | 80 | self::getInstance()->checkData($route['uri']->getPath(), $_SERVER['REQUEST_URI']); |
| 81 | 81 | self::getInstance()->sortRoutes(); |
| 82 | 82 | return self::getInstance(); |
| 83 | - }catch(\Exception $er){ |
|
| 83 | + } catch(\Exception $er){ |
|
| 84 | 84 | continue; |
| 85 | 85 | } |
| 86 | 86 | } |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace HnrAzevedo\Router; |
| 4 | 4 | |
| 5 | -trait PrioritizeTrait{ |
|
| 5 | +trait PrioritizeTrait { |
|
| 6 | 6 | |
| 7 | 7 | protected array $routes = []; |
| 8 | 8 | |
@@ -11,28 +11,28 @@ discard block |
||
| 11 | 11 | $staticRoutes = []; |
| 12 | 12 | $paramRoutes = []; |
| 13 | 13 | |
| 14 | - foreach($this->routes as $r => $route){ |
|
| 14 | + foreach ($this->routes as $r => $route) { |
|
| 15 | 15 | |
| 16 | 16 | $path = urldecode($route['uri']->getPath()); |
| 17 | 17 | |
| 18 | - if(strstr($path,'{')){ |
|
| 19 | - $paramRoutes[$this->getKeyArray(substr_count($path,'/') + substr_count($path,'{'),$paramRoutes)] = $route; |
|
| 18 | + if (strstr($path, '{')) { |
|
| 19 | + $paramRoutes[$this->getKeyArray(substr_count($path, '/')+substr_count($path, '{'), $paramRoutes)] = $route; |
|
| 20 | 20 | continue; |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | - $staticRoutes[$this->getKeyArray(substr_count($path,'/'),$staticRoutes)] = $route; |
|
| 23 | + $staticRoutes[$this->getKeyArray(substr_count($path, '/'), $staticRoutes)] = $route; |
|
| 24 | 24 | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | rsort($paramRoutes); |
| 28 | 28 | rsort($staticRoutes); |
| 29 | 29 | |
| 30 | - $this->orderRoutes(array_merge($staticRoutes,$paramRoutes)); |
|
| 30 | + $this->orderRoutes(array_merge($staticRoutes, $paramRoutes)); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | private function getKeyArray(int $index, array $array): int |
| 34 | 34 | { |
| 35 | - while(array_key_exists($index,$array)){ |
|
| 35 | + while (array_key_exists($index, $array)) { |
|
| 36 | 36 | $index++; |
| 37 | 37 | } |
| 38 | 38 | return $index; |
@@ -41,8 +41,8 @@ discard block |
||
| 41 | 41 | private function orderRoutes(array $routes):void |
| 42 | 42 | { |
| 43 | 43 | $kRoutes = $routes; |
| 44 | - foreach($routes as $r => $route){ |
|
| 45 | - if(array_key_exists('name',$route)){ |
|
| 44 | + foreach ($routes as $r => $route) { |
|
| 45 | + if (array_key_exists('name', $route)) { |
|
| 46 | 46 | unset($kRoutes[$r]); |
| 47 | 47 | $kRoutes[$route['name']] = $route; |
| 48 | 48 | } |
@@ -20,8 +20,8 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public static function globalMiddlewares(array $middlewares): RouterInterface |
| 22 | 22 | { |
| 23 | - foreach($middlewares as $middleware){ |
|
| 24 | - if(!class_exists($middleware)){ |
|
| 23 | + foreach ($middlewares as $middleware) { |
|
| 24 | + if (!class_exists($middleware)) { |
|
| 25 | 25 | throw new \RuntimeException("Middleware class {$middleware} not exists"); |
| 26 | 26 | } |
| 27 | 27 | } |
@@ -31,16 +31,16 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | public static function middleware($middlewares): RouterInterface |
| 33 | 33 | { |
| 34 | - $middlewares = (is_array($middlewares)) ? $middlewares : [ $middlewares ]; |
|
| 34 | + $middlewares = (is_array($middlewares)) ? $middlewares : [$middlewares]; |
|
| 35 | 35 | $route = self::getInstance()->inSave(); |
| 36 | - $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares; |
|
| 37 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->routes)); |
|
| 36 | + $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'], $middlewares) : $middlewares; |
|
| 37 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->routes)); |
|
| 38 | 38 | return self::getInstance(); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | private static function existMiddleware(string $name): void |
| 42 | 42 | { |
| 43 | - if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){ |
|
| 43 | + if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) { |
|
| 44 | 44 | throw new \RuntimeException("Middleware {$name} does not exist"); |
| 45 | 45 | } |
| 46 | 46 | } |
@@ -9,37 +9,37 @@ discard block |
||
| 9 | 9 | public static function where(array $wheres): Router |
| 10 | 10 | { |
| 11 | 11 | $route = self::getInstance()->inSabe(); |
| 12 | - $route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$wheres) : $wheres; |
|
| 13 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->routes)); |
|
| 12 | + $route['where'] = (is_array($route['where'])) ? array_merge($route['where'], $wheres) : $wheres; |
|
| 13 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->routes)); |
|
| 14 | 14 | return self::getInstance(); |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | protected function checkData(string $routePath, string $uriPath): void |
| 18 | 18 | { |
| 19 | - $routePath = explode('/',urldecode($routePath)); |
|
| 19 | + $routePath = explode('/', urldecode($routePath)); |
|
| 20 | 20 | unset($routePath[0]); |
| 21 | - $uriPath = explode('/',urldecode($uriPath)); |
|
| 21 | + $uriPath = explode('/', urldecode($uriPath)); |
|
| 22 | 22 | unset($uriPath[0]); |
| 23 | 23 | |
| 24 | 24 | $this->checkCount($routePath, $uriPath); |
| 25 | 25 | |
| 26 | 26 | $corretRoute = true; |
| 27 | - foreach($routePath as $r => $routeFrag){ |
|
| 27 | + foreach ($routePath as $r => $routeFrag) { |
|
| 28 | 28 | $routeFrag = $this->replaceParam($routeFrag, $uriPath[$r]); |
| 29 | 29 | |
| 30 | - if($routeFrag !== $uriPath[$r]){ |
|
| 30 | + if ($routeFrag !== $uriPath[$r]) { |
|
| 31 | 31 | $corretRoute = false; |
| 32 | 32 | } |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - if(!$corretRoute){ |
|
| 35 | + if (!$corretRoute) { |
|
| 36 | 36 | throw new \Exception('continue'); |
| 37 | 37 | } |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | private function replaceParam(string $ref, string $value): string |
| 41 | 41 | { |
| 42 | - if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) { |
|
| 42 | + if (((substr($ref, 0, 1) === '{') && (substr($ref, strlen($ref)-1) === '}'))) { |
|
| 43 | 43 | return $value; |
| 44 | 44 | } |
| 45 | 45 | return $ref; |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | |
| 48 | 48 | private function checkCount(array $routePath, array $uriPath): void |
| 49 | 49 | { |
| 50 | - if(count($routePath) !== count($uriPath)){ |
|
| 50 | + if (count($routePath) !== count($uriPath)) { |
|
| 51 | 51 | throw new \Exception('Continue'); |
| 52 | 52 | } |
| 53 | 53 | } |
@@ -11,24 +11,24 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | public static function before($closure): RouterInterface |
| 13 | 13 | { |
| 14 | - return self::addInRoute('before',$closure); |
|
| 14 | + return self::addInRoute('before', $closure); |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | public static function after($closure): RouterInterface |
| 18 | 18 | { |
| 19 | - return self::addInRoute('after',$closure); |
|
| 19 | + return self::addInRoute('after', $closure); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | public static function beforeAll($closure, $excepts): RouterInterface |
| 23 | 23 | { |
| 24 | - self::getInstance()->beforeExcepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
| 24 | + self::getInstance()->beforeExcepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
| 25 | 25 | self::getInstance()->beforeAll = $closure; |
| 26 | 26 | return self::getInstance(); |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | public static function afterAll($closure, $excepts): RouterInterface |
| 30 | 30 | { |
| 31 | - self::getInstance()->afterExcepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
| 31 | + self::getInstance()->afterExcepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
| 32 | 32 | self::getInstance()->afterAll = $closure; |
| 33 | 33 | return self::getInstance(); |
| 34 | 34 | } |
@@ -46,12 +46,12 @@ discard block |
||
| 46 | 46 | private static function addInRoutes(string $state, $closure, $excepts): RouterInterface |
| 47 | 47 | { |
| 48 | 48 | self::getInstance()->isInPseudGroup(); |
| 49 | - $excepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
| 49 | + $excepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
| 50 | 50 | $group = self::getInstance()->inSave()['group']; |
| 51 | 51 | |
| 52 | - foreach(self::getInstance()->routes as $r => $route){ |
|
| 53 | - if($route['group'] === $group && !in_array($r,$excepts)){ |
|
| 54 | - self::getInstance()->routes[$r][$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
| 52 | + foreach (self::getInstance()->routes as $r => $route) { |
|
| 53 | + if ($route['group'] === $group && !in_array($r, $excepts)) { |
|
| 54 | + self::getInstance()->routes[$r][$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | 57 | |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | private static function addInRoute(string $state, $closure): RouterInterface |
| 62 | 62 | { |
| 63 | 63 | $route = self::getInstance()->inSave(); |
| 64 | - $state = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
| 64 | + $state = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
| 65 | 65 | $route[$state] = $state; |
| 66 | - self::updateRoute($route,array_key_last(self::getInstance()->routes)); |
|
| 66 | + self::updateRoute($route, array_key_last(self::getInstance()->routes)); |
|
| 67 | 67 | return self::getInstance(); |
| 68 | 68 | } |
| 69 | 69 | |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | /* NOTE: in case of error an exception is thrown */ |
| 11 | 11 | |
| 12 | -try{ |
|
| 12 | +try { |
|
| 13 | 13 | |
| 14 | 14 | Router::globalMiddlewares([ |
| 15 | 15 | 'Lasted'=> \Example\Middleware\Lasted::class |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | /* Return current action route */ |
| 27 | 27 | $action = Router::currentAction(); |
| 28 | 28 | |
| 29 | -}catch(Exception $er){ |
|
| 29 | +}catch (Exception $er) { |
|
| 30 | 30 | |
| 31 | 31 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 32 | 32 | |
@@ -26,7 +26,7 @@ |
||
| 26 | 26 | /* Return current action route */ |
| 27 | 27 | $action = Router::currentAction(); |
| 28 | 28 | |
| 29 | -}catch(Exception $er){ |
|
| 29 | +} catch(Exception $er){ |
|
| 30 | 30 | |
| 31 | 31 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 32 | 32 | |