@@ -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 | |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | { |
| 20 | 20 | $this->hasRouteName($name); |
| 21 | 21 | |
| 22 | - if(!$this->loaded()){ |
|
| 22 | + if (!$this->loaded()) { |
|
| 23 | 23 | $this->loadIn($name); |
| 24 | 24 | } |
| 25 | 25 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | |
| 29 | 29 | public function hasCurrentRoute(): void |
| 30 | 30 | { |
| 31 | - if(!isset($this->currentRoute)){ |
|
| 31 | + if (!isset($this->currentRoute)) { |
|
| 32 | 32 | throw new \RuntimeException('Route not yet loaded'); |
| 33 | 33 | } |
| 34 | 34 | } |
@@ -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 | } |
@@ -70,7 +70,7 @@ discard block |
||
| 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 | } |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | try{ |
| 98 | 98 | $action = unserialize(self::getInstance()->current()['action']); |
| 99 | 99 | self::getInstance()->executeRouteAction( (is_string($action)) ? $action : $action->getClosure() ); |
| 100 | - }catch(\Exception $er){ |
|
| 100 | + } catch(\Exception $er){ |
|
| 101 | 101 | self::getInstance()->error = $er; |
| 102 | 102 | } |
| 103 | 103 | |
@@ -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 | |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | { |
| 35 | 35 | $route = self::getInstance()->inSave(); |
| 36 | 36 | |
| 37 | - if(null === $value){ |
|
| 37 | + if (null === $value) { |
|
| 38 | 38 | return (isset($route['attributes'][$name])) ? $route['attributes'][$name] : null; |
| 39 | 39 | } |
| 40 | 40 | |
@@ -47,8 +47,8 @@ discard block |
||
| 47 | 47 | public static function groupAttribute(string $name, $value = null, ?array $excepts = []): Router |
| 48 | 48 | { |
| 49 | 49 | $group = self::getInstance()->inSave()['group']; |
| 50 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 51 | - if($route['group'] !== $group || in_array($route['name'], $excepts)){ |
|
| 50 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 51 | + if ($route['group'] !== $group || in_array($route['name'], $excepts)) { |
|
| 52 | 52 | continue; |
| 53 | 53 | } |
| 54 | 54 | |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | public static function group(string $prefix, \Closure $closure): Router |
| 62 | 62 | { |
| 63 | 63 | $id = sha1(date('d/m/Y h:m:i')); |
| 64 | - while(array_key_exists($id, self::getInstance()->groupsName)){ |
|
| 64 | + while (array_key_exists($id, self::getInstance()->groupsName)) { |
|
| 65 | 65 | $id = sha1(date('d/m/Y h:m:i').$id); |
| 66 | 66 | } |
| 67 | 67 | |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | { |
| 83 | 83 | self::getInstance()->loaded = true; |
| 84 | 84 | |
| 85 | - if(null !== $name){ |
|
| 85 | + if (null !== $name) { |
|
| 86 | 86 | return self::getInstance()->loadByName($name); |
| 87 | 87 | } |
| 88 | 88 | |
@@ -90,15 +90,15 @@ discard block |
||
| 90 | 90 | |
| 91 | 91 | $requestMethod = (isset($_REQUEST['REQUEST_METHOD'])) ? $_REQUEST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']; |
| 92 | 92 | |
| 93 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 93 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 94 | 94 | self::getInstance()->currentRoute = $route; |
| 95 | 95 | self::getInstance()->currentRoute['name'] = $r; |
| 96 | 96 | |
| 97 | - try{ |
|
| 97 | + try { |
|
| 98 | 98 | self::getInstance()->checkMethod($route, $requestMethod); |
| 99 | 99 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
| 100 | 100 | return self::getInstance(); |
| 101 | - }catch(\Exception $er){ |
|
| 101 | + }catch (\Exception $er) { |
|
| 102 | 102 | continue; |
| 103 | 103 | } |
| 104 | 104 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | |
| 113 | 113 | public static function run(?string $name = null): RouterInterface |
| 114 | 114 | { |
| 115 | - if(!self::getInstance()->loaded){ |
|
| 115 | + if (!self::getInstance()->loaded) { |
|
| 116 | 116 | self::getInstance()->load($name); |
| 117 | 117 | } |
| 118 | 118 | |
@@ -122,10 +122,10 @@ discard block |
||
| 122 | 122 | |
| 123 | 123 | self::getInstance()->executeBefore(); |
| 124 | 124 | |
| 125 | - try{ |
|
| 125 | + try { |
|
| 126 | 126 | $action = unserialize(self::getInstance()->current()['action']); |
| 127 | - self::getInstance()->executeRouteAction( (is_string($action)) ? $action : $action->getClosure() ); |
|
| 128 | - }catch(\Exception $er){ |
|
| 127 | + self::getInstance()->executeRouteAction((is_string($action)) ? $action : $action->getClosure()); |
|
| 128 | + }catch (\Exception $er) { |
|
| 129 | 129 | self::getInstance()->error = $er; |
| 130 | 130 | } |
| 131 | 131 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | |
| 139 | 139 | private function checkError(): void |
| 140 | 140 | { |
| 141 | - if(isset($this->error)){ |
|
| 141 | + if (isset($this->error)) { |
|
| 142 | 142 | throw $this->error; |
| 143 | 143 | } |
| 144 | 144 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | |
| 153 | 153 | public static function routes(?array $routes = null): array |
| 154 | 154 | { |
| 155 | - if(null !== $routes){ |
|
| 155 | + if (null !== $routes) { |
|
| 156 | 156 | self::getInstance()->setRoutes($routes); |
| 157 | 157 | self::getInstance()->sortRoutes(); |
| 158 | 158 | } |
@@ -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,28 +12,28 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | protected function hasRouteName(string $name): void |
| 14 | 14 | { |
| 15 | - if(!isset($this->routesName[$name])){ |
|
| 15 | + if (!isset($this->routesName[$name])) { |
|
| 16 | 16 | throw new \RuntimeException("There is no route named with {$name}"); |
| 17 | 17 | } |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | protected function isInNameGroup(): void |
| 21 | 21 | { |
| 22 | - if(!is_null($this->getGroup())){ |
|
| 22 | + if (!is_null($this->getGroup())) { |
|
| 23 | 23 | throw new \RuntimeException("It is not allowed to assign names to groups"); |
| 24 | 24 | } |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | protected function isInPseudGroup(): void |
| 28 | 28 | { |
| 29 | - if(!is_null($this->getGroup())){ |
|
| 29 | + if (!is_null($this->getGroup())) { |
|
| 30 | 30 | throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup"); |
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | protected function existRouteName(string $name): void |
| 35 | 35 | { |
| 36 | - if(isset($this->routesName[$name])){ |
|
| 36 | + if (isset($this->routesName[$name])) { |
|
| 37 | 37 | throw new \RuntimeException("There is already a route named with {$name}"); |
| 38 | 38 | } |
| 39 | 39 | } |
@@ -41,19 +41,19 @@ discard block |
||
| 41 | 41 | protected function checkMethod(array $route, $method): void |
| 42 | 42 | { |
| 43 | 43 | $hasMethod = false; |
| 44 | - foreach(explode('|',$route['method']) as $routeMethod){ |
|
| 45 | - if(@preg_match("/{$routeMethod}/",$method) !== 0 || $method === '*'){ |
|
| 44 | + foreach (explode('|', $route['method']) as $routeMethod) { |
|
| 45 | + if (@preg_match("/{$routeMethod}/", $method) !== 0 || $method === '*') { |
|
| 46 | 46 | $hasMethod = true; |
| 47 | 47 | } |
| 48 | 48 | } |
| 49 | - if(!$hasMethod){ |
|
| 49 | + if (!$hasMethod) { |
|
| 50 | 50 | throw new \Exception('This route is not released for the accessed method'); |
| 51 | 51 | } |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | protected function throwCallable($value): void |
| 55 | 55 | { |
| 56 | - if(is_callable($value)){ |
|
| 56 | + if (is_callable($value)) { |
|
| 57 | 57 | throw new \Exception('Passing functions as attributes is not allowed'); |
| 58 | 58 | } |
| 59 | 59 | } |
@@ -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 | |
@@ -19,30 +19,30 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | public static function loadPipeline(): void |
| 21 | 21 | { |
| 22 | - foreach(self::getInstance()->pipeline as $line){ |
|
| 22 | + foreach (self::getInstance()->pipeline as $line) { |
|
| 23 | 23 | self::getInstance()->loadLine(new ReflectionObject(new $line())); |
| 24 | 24 | } |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | private function loadLine(ReflectionObject $reflection): void |
| 28 | 28 | { |
| 29 | - foreach($reflection->getMethods() as $method){ |
|
| 29 | + foreach ($reflection->getMethods() as $method) { |
|
| 30 | 30 | $this->loadMethod($method); |
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | private function loadMethod(ReflectionMethod $method): void |
| 35 | 35 | { |
| 36 | - try{ |
|
| 36 | + try { |
|
| 37 | 37 | foreach ($method->getAttributes() as $attr) { |
| 38 | - if($attr->getName() != 'HnrAzevedo\Router\RouteAttribute') continue; |
|
| 38 | + if ($attr->getName() != 'HnrAzevedo\Router\RouteAttribute') continue; |
|
| 39 | 39 | |
| 40 | 40 | $args = $attr->getArguments(); |
| 41 | 41 | |
| 42 | 42 | $this->checkArgs($attr->getArguments()); |
| 43 | 43 | |
| 44 | 44 | self::set( |
| 45 | - strtolower(implode('|',$args['methods'])), |
|
| 45 | + strtolower(implode('|', $args['methods'])), |
|
| 46 | 46 | $args['uri'], |
| 47 | 47 | $method->class.'@'.$method->name |
| 48 | 48 | ); |
@@ -54,14 +54,14 @@ discard block |
||
| 54 | 54 | ->attrWhere($args) |
| 55 | 55 | ->attrMiddleware($args); |
| 56 | 56 | } |
| 57 | - }catch(Exception $er){ |
|
| 57 | + }catch (Exception $er) { |
|
| 58 | 58 | throw new Exception('Failed to add route via attribute: '.$er->getMessage()); |
| 59 | 59 | } |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | private function checkArgs(array $args): self |
| 63 | 63 | { |
| 64 | - if(!array_key_exists('uri',$args) || !array_key_exists('methods',$args)){ |
|
| 64 | + if (!array_key_exists('uri', $args) || !array_key_exists('methods', $args)) { |
|
| 65 | 65 | throw new Exception('Misconfigured route attribute'); |
| 66 | 66 | } |
| 67 | 67 | return $this; |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | |
| 70 | 70 | private function attrName(array $attr): self |
| 71 | 71 | { |
| 72 | - if(array_key_exists('name',$attr)){ |
|
| 72 | + if (array_key_exists('name', $attr)) { |
|
| 73 | 73 | $this->name($attr['name']); |
| 74 | 74 | } |
| 75 | 75 | return $this; |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | |
| 78 | 78 | private function attrBefore(array $attr): self |
| 79 | 79 | { |
| 80 | - if(array_key_exists('before',$attr)){ |
|
| 80 | + if (array_key_exists('before', $attr)) { |
|
| 81 | 81 | $this->before($attr['before']); |
| 82 | 82 | } |
| 83 | 83 | return $this; |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | |
| 86 | 86 | private function attrAfter(array $attr): self |
| 87 | 87 | { |
| 88 | - if(array_key_exists('after',$attr)){ |
|
| 88 | + if (array_key_exists('after', $attr)) { |
|
| 89 | 89 | $this->after($attr['after']); |
| 90 | 90 | } |
| 91 | 91 | return $this; |
@@ -93,8 +93,8 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | private function attrAttributes(array $attr): self |
| 95 | 95 | { |
| 96 | - if(array_key_exists('attributes',$attr)){ |
|
| 97 | - foreach($attr['attributes'] as $attribute){ |
|
| 96 | + if (array_key_exists('attributes', $attr)) { |
|
| 97 | + foreach ($attr['attributes'] as $attribute) { |
|
| 98 | 98 | $this->attribute($attribute[0], $attribute[1]); |
| 99 | 99 | } |
| 100 | 100 | } |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | |
| 104 | 104 | private function attrWhere(array $attr): self |
| 105 | 105 | { |
| 106 | - if(array_key_exists('where',$attr)){ |
|
| 106 | + if (array_key_exists('where', $attr)) { |
|
| 107 | 107 | $this->where($attr['where']); |
| 108 | 108 | } |
| 109 | 109 | return $this; |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | |
| 112 | 112 | private function attrMiddleware(array $attr): self |
| 113 | 113 | { |
| 114 | - if(array_key_exists('middleware',$attr)){ |
|
| 114 | + if (array_key_exists('middleware', $attr)) { |
|
| 115 | 115 | $this->middleware($attr['middleware']); |
| 116 | 116 | } |
| 117 | 117 | return $this; |
@@ -69,9 +69,9 @@ discard block |
||
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | $routes[strval($index)] = [ |
| 72 | - 'uri' => serialize(new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri)), |
|
| 73 | - 'action' => (is_callable($closure)) ? serialize(new SerializableClosure($closure)) : serialize($closure), |
|
| 74 | - 'method' => strtoupper($method), |
|
| 72 | + 'uri' => serialize(new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri)), |
|
| 73 | + 'action' => (is_callable($closure)) ? serialize(new SerializableClosure($closure)) : serialize($closure), |
|
| 74 | + 'method' => strtoupper($method), |
|
| 75 | 75 | 'middlewares' => [], |
| 76 | 76 | 'where' => [], |
| 77 | 77 | 'before' => [], |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | private static function checkDuplicity(string $uri, string $method): void |
| 91 | 91 | { |
| 92 | 92 | foreach(self::getInstance()->getRoutes() as $route){ |
| 93 | - if( md5(strtoupper(unserialize($route['uri'])->getPath().$route['method'])) === md5(strtoupper($uri.$method)) ){ |
|
| 93 | + if( md5(strtoupper(unserialize($route['uri'])->getPath().$route['method'])) === md5(strtoupper($uri.$method)) ){ |
|
| 94 | 94 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
| 95 | 95 | } |
| 96 | 96 | } |
@@ -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 = count($routes); |
| 66 | 66 | |
| 67 | - while(array_key_exists($index, $routes)){ |
|
| 67 | + while (array_key_exists($index, $routes)) { |
|
| 68 | 68 | $index++; |
| 69 | 69 | } |
| 70 | 70 | |
@@ -89,8 +89,8 @@ discard block |
||
| 89 | 89 | |
| 90 | 90 | private static function checkDuplicity(string $uri, string $method): void |
| 91 | 91 | { |
| 92 | - foreach(self::getInstance()->getRoutes() as $route){ |
|
| 93 | - if( md5(strtoupper(unserialize($route['uri'])->getPath().$route['method'])) === md5(strtoupper($uri.$method)) ){ |
|
| 92 | + foreach (self::getInstance()->getRoutes() as $route) { |
|
| 93 | + if (md5(strtoupper(unserialize($route['uri'])->getPath().$route['method'])) === md5(strtoupper($uri.$method))) { |
|
| 94 | 94 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
| 95 | 95 | } |
| 96 | 96 | } |
@@ -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 | |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | $this->beforeAll = (!isset($this->beforeAll)) ? function() {} : $this->beforeAll; |
| 21 | 21 | $this->afterAll = (!isset($this->afterAll)) ? function() {} : $this->afterAll; |
| 22 | 22 | |
| 23 | - if($state === 'before'){ |
|
| 23 | + if ($state === 'before') { |
|
| 24 | 24 | return ($except) ? $this->beforeExcepts : $this->beforeAll; |
| 25 | 25 | } |
| 26 | 26 | |
@@ -29,8 +29,8 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | protected function setState(string $state, $settable, bool $except = false): bool |
| 31 | 31 | { |
| 32 | - if($state === 'before'){ |
|
| 33 | - if($except){ |
|
| 32 | + if ($state === 'before') { |
|
| 33 | + if ($except) { |
|
| 34 | 34 | $this->beforeExcepts = $settable; |
| 35 | 35 | return true; |
| 36 | 36 | } |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | return true; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - if($except){ |
|
| 42 | + if ($except) { |
|
| 43 | 43 | $this->afterExcepts = $settable; |
| 44 | 44 | return true; |
| 45 | 45 | } |
@@ -60,14 +60,14 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | public static function beforeAll($closure, ?array $excepts = null): RouterInterface |
| 62 | 62 | { |
| 63 | - self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [] ,true); |
|
| 63 | + self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [], true); |
|
| 64 | 64 | self::getInstance()->setState('before', $closure, false); |
| 65 | 65 | return self::getInstance(); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | public static function afterAll($closure, ?array $excepts = null): RouterInterface |
| 69 | 69 | { |
| 70 | - self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [] ,true); |
|
| 70 | + self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [], true); |
|
| 71 | 71 | self::getInstance()->setState('after', $closure, false); |
| 72 | 72 | return self::getInstance(); |
| 73 | 73 | } |
@@ -84,14 +84,14 @@ discard block |
||
| 84 | 84 | |
| 85 | 85 | protected function executeRouteAction($action): bool |
| 86 | 86 | { |
| 87 | - if(is_callable($action)){ |
|
| 87 | + if (is_callable($action)) { |
|
| 88 | 88 | |
| 89 | 89 | $params = []; |
| 90 | 90 | $closure = (get_class($action) !== 'Closure') ? $action->getClosure() : $action; |
| 91 | - $ReflectionMethod = new \ReflectionFunction ($closure); |
|
| 91 | + $ReflectionMethod = new \ReflectionFunction($closure); |
|
| 92 | 92 | |
| 93 | - foreach($ReflectionMethod->getParameters() as $param){ |
|
| 94 | - if(!isset($_REQUEST[$param->name])) continue; |
|
| 93 | + foreach ($ReflectionMethod->getParameters() as $param) { |
|
| 94 | + if (!isset($_REQUEST[$param->name])) continue; |
|
| 95 | 95 | $params[$param->name] = $_REQUEST[$param->name]; |
| 96 | 96 | } |
| 97 | 97 | |
@@ -109,9 +109,9 @@ discard block |
||
| 109 | 109 | $excepts = (is_array($excepts)) ? $excepts : []; |
| 110 | 110 | $group = self::getInstance()->inSave()['group']; |
| 111 | 111 | |
| 112 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 113 | - if($route['group'] === $group && !in_array($r, $excepts)){ |
|
| 114 | - $route[$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
| 112 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 113 | + if ($route['group'] === $group && !in_array($r, $excepts)) { |
|
| 114 | + $route[$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
| 115 | 115 | self::getInstance()->updateRoute($route, $r); |
| 116 | 116 | } |
| 117 | 117 | } |
@@ -122,14 +122,14 @@ discard block |
||
| 122 | 122 | private static function addInRoute(string $state, $closure): RouterInterface |
| 123 | 123 | { |
| 124 | 124 | $route = self::getInstance()->inSave(); |
| 125 | - $route[$state] = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
| 126 | - self::updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
| 125 | + $route[$state] = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
| 126 | + self::updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
| 127 | 127 | return self::getInstance(); |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | protected function executeBefore(): void |
| 131 | 131 | { |
| 132 | - if(!in_array($this->currentName(), (array) $this->getState('before', true))){ |
|
| 132 | + if (!in_array($this->currentName(), (array) $this->getState('before', true))) { |
|
| 133 | 133 | ($this->getState('before', false))(); |
| 134 | 134 | } |
| 135 | 135 | |
@@ -140,15 +140,15 @@ discard block |
||
| 140 | 140 | { |
| 141 | 141 | $this->executeState('after'); |
| 142 | 142 | |
| 143 | - if(!in_array($this->currentName(), (array) $this->getState('after', true))){ |
|
| 143 | + if (!in_array($this->currentName(), (array) $this->getState('after', true))) { |
|
| 144 | 144 | ($this->getState('after', false))(); |
| 145 | 145 | } |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | private function executeState(string $stage): void |
| 149 | 149 | { |
| 150 | - foreach($this->current()[$stage] as $state){ |
|
| 151 | - if(is_callable($state)){ |
|
| 150 | + foreach ($this->current()[$stage] as $state) { |
|
| 151 | + if (is_callable($state)) { |
|
| 152 | 152 | $state(); |
| 153 | 153 | continue; |
| 154 | 154 | } |
@@ -159,40 +159,40 @@ discard block |
||
| 159 | 159 | |
| 160 | 160 | private function executeController(string $controllerMeth): void |
| 161 | 161 | { |
| 162 | - if(count(explode('@',$controllerMeth)) !== 2){ |
|
| 162 | + if (count(explode('@', $controllerMeth)) !== 2) { |
|
| 163 | 163 | $path = urldecode(unserialize($this->current()['uri'])->getPath()); |
| 164 | 164 | throw new \RuntimeException("Misconfigured route action ({$path})"); |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
| 168 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
| 167 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
| 168 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
| 169 | 169 | |
| 170 | 170 | $this->checkControllerMeth($controllerMeth); |
| 171 | 171 | |
| 172 | 172 | $params = []; |
| 173 | 173 | |
| 174 | - $ReflectionMethod = new \ReflectionMethod(new $controller(), $method); |
|
| 174 | + $ReflectionMethod = new \ReflectionMethod(new $controller(), $method); |
|
| 175 | 175 | |
| 176 | - foreach($ReflectionMethod->getParameters() as $param){ |
|
| 177 | - if(!isset($_REQUEST[$param->name])) continue; |
|
| 176 | + foreach ($ReflectionMethod->getParameters() as $param) { |
|
| 177 | + if (!isset($_REQUEST[$param->name])) continue; |
|
| 178 | 178 | $params[$param->name] = $_REQUEST[$param->name]; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - call_user_func_array([(new $controller()),$method], $params); |
|
| 181 | + call_user_func_array([(new $controller()), $method], $params); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | private function checkControllerMeth(string $controllerMeth): void |
| 185 | 185 | { |
| 186 | - $routeURI = str_replace(['{?','{','}'],'',urldecode(unserialize($this->current()['uri'])->getPath())); |
|
| 186 | + $routeURI = str_replace(['{?', '{', '}'], '', urldecode(unserialize($this->current()['uri'])->getPath())); |
|
| 187 | 187 | |
| 188 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
| 189 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
| 188 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
| 189 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
| 190 | 190 | |
| 191 | - if(!class_exists($controller)){ |
|
| 191 | + if (!class_exists($controller)) { |
|
| 192 | 192 | throw new \RuntimeException("Controller not found in route URI {$routeURI}"); |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | - if(!method_exists($controller, $method)){ |
|
| 195 | + if (!method_exists($controller, $method)) { |
|
| 196 | 196 | throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}"); |
| 197 | 197 | } |
| 198 | 198 | |
@@ -91,7 +91,9 @@ discard block |
||
| 91 | 91 | $ReflectionMethod = new \ReflectionFunction ($closure); |
| 92 | 92 | |
| 93 | 93 | foreach($ReflectionMethod->getParameters() as $param){ |
| 94 | - if(!isset($_REQUEST[$param->name])) continue; |
|
| 94 | + if(!isset($_REQUEST[$param->name])) { |
|
| 95 | + continue; |
|
| 96 | + } |
|
| 95 | 97 | $params[$param->name] = $_REQUEST[$param->name]; |
| 96 | 98 | } |
| 97 | 99 | |
@@ -174,7 +176,9 @@ discard block |
||
| 174 | 176 | $ReflectionMethod = new \ReflectionMethod(new $controller(), $method); |
| 175 | 177 | |
| 176 | 178 | foreach($ReflectionMethod->getParameters() as $param){ |
| 177 | - if(!isset($_REQUEST[$param->name])) continue; |
|
| 179 | + if(!isset($_REQUEST[$param->name])) { |
|
| 180 | + continue; |
|
| 181 | + } |
|
| 178 | 182 | $params[$param->name] = $_REQUEST[$param->name]; |
| 179 | 183 | } |
| 180 | 184 | |
@@ -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,9 +22,9 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | public static function globalMiddlewares(?array $middlewares = null): array |
| 24 | 24 | { |
| 25 | - if(null !== $middlewares){ |
|
| 26 | - foreach($middlewares as $middleware){ |
|
| 27 | - if(!class_exists($middleware)){ |
|
| 25 | + if (null !== $middlewares) { |
|
| 26 | + foreach ($middlewares as $middleware) { |
|
| 27 | + if (!class_exists($middleware)) { |
|
| 28 | 28 | throw new \RuntimeException("Middleware class {$middleware} not exists"); |
| 29 | 29 | } |
| 30 | 30 | } |
@@ -46,8 +46,8 @@ discard block |
||
| 46 | 46 | public static function middleware(array $middlewares): RouterInterface |
| 47 | 47 | { |
| 48 | 48 | $route = self::getInstance()->inSave(); |
| 49 | - $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares; |
|
| 50 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
| 49 | + $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'], $middlewares) : $middlewares; |
|
| 50 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
| 51 | 51 | return self::getInstance(); |
| 52 | 52 | } |
| 53 | 53 | |
@@ -55,8 +55,8 @@ discard block |
||
| 55 | 55 | { |
| 56 | 56 | $excepts = (is_array($excepts)) ? $excepts : []; |
| 57 | 57 | $group = self::getInstance()->inSave()['group']; |
| 58 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 59 | - if($route['group'] !== $group || in_array($route['name'], $excepts)){ |
|
| 58 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 59 | + if ($route['group'] !== $group || in_array($route['name'], $excepts)) { |
|
| 60 | 60 | continue; |
| 61 | 61 | } |
| 62 | 62 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | private static function existMiddleware(string $name): void |
| 70 | 70 | { |
| 71 | - if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){ |
|
| 71 | + if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) { |
|
| 72 | 72 | throw new \RuntimeException("Middleware {$name} does not exist"); |
| 73 | 73 | } |
| 74 | 74 | } |
@@ -79,9 +79,9 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | $requestMethod = (isset($_REQUEST['REQUEST_METHOD'])) ? $_REQUEST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']; |
| 81 | 81 | |
| 82 | - $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($requestMethod, unserialize($this->current()['uri']),['route' => $this->current()]) : $this->serverRequest; |
|
| 82 | + $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($requestMethod, unserialize($this->current()['uri']), ['route' => $this->current()]) : $this->serverRequest; |
|
| 83 | 83 | |
| 84 | - foreach ($this->current()['middlewares'] as $middleware){ |
|
| 84 | + foreach ($this->current()['middlewares'] as $middleware) { |
|
| 85 | 85 | $this->currentMiddlewares[] = (class_exists($middleware)) ? new $middleware() : new $this->globalMiddlewares[$middleware](); |
| 86 | 86 | } |
| 87 | 87 | |
@@ -96,21 +96,21 @@ discard block |
||
| 96 | 96 | |
| 97 | 97 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 98 | 98 | { |
| 99 | - if(!$this->getInstance()->loaded()){ |
|
| 99 | + if (!$this->getInstance()->loaded()) { |
|
| 100 | 100 | $this->getInstance()->load(); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | $route = $this->getInstance()->current(); |
| 104 | - if(!empty($route)){ |
|
| 104 | + if (!empty($route)) { |
|
| 105 | 105 | |
| 106 | - $route['action'] = function(){ |
|
| 106 | + $route['action'] = function() { |
|
| 107 | 107 | $this->getInstance()->executeBefore(); |
| 108 | 108 | $this->getInstance()->executeRouteAction(unserialize($this->getInstance()->current()['action'])); |
| 109 | 109 | $this->getInstance()->executeAfter(); |
| 110 | 110 | }; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - return $this->next($handler)->handle($request->withAttribute('route',$route)); |
|
| 113 | + return $this->next($handler)->handle($request->withAttribute('route', $route)); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | 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 | } |