@@ -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,12 +17,12 @@ discard block |
||
17 | 17 | |
18 | 18 | protected function getState(string $state, bool $except = false) |
19 | 19 | { |
20 | - $this->beforeAll = (!isset($this->beforeAll)) ? function () { |
|
20 | + $this->beforeAll = (!isset($this->beforeAll)) ? function() { |
|
21 | 21 | } : $this->beforeAll; |
22 | - $this->afterAll = (!isset($this->afterAll)) ? function () { |
|
22 | + $this->afterAll = (!isset($this->afterAll)) ? function() { |
|
23 | 23 | } : $this->afterAll; |
24 | 24 | |
25 | - if($state === 'before') { |
|
25 | + if ($state === 'before') { |
|
26 | 26 | return ($except) ? $this->beforeExcepts : $this->beforeAll; |
27 | 27 | } |
28 | 28 | |
@@ -31,8 +31,8 @@ discard block |
||
31 | 31 | |
32 | 32 | protected function setState(string $state, $settable, bool $except = false): bool |
33 | 33 | { |
34 | - if($state === 'before') { |
|
35 | - if($except) { |
|
34 | + if ($state === 'before') { |
|
35 | + if ($except) { |
|
36 | 36 | $this->beforeExcepts = $settable; |
37 | 37 | return true; |
38 | 38 | } |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | return true; |
42 | 42 | } |
43 | 43 | |
44 | - if($except) { |
|
44 | + if ($except) { |
|
45 | 45 | $this->afterExcepts = $settable; |
46 | 46 | return true; |
47 | 47 | } |
@@ -86,14 +86,14 @@ discard block |
||
86 | 86 | |
87 | 87 | protected function executeRouteAction($action): bool |
88 | 88 | { |
89 | - if(is_callable($action)) { |
|
89 | + if (is_callable($action)) { |
|
90 | 90 | |
91 | 91 | $params = []; |
92 | 92 | $closure = (get_class($action) !== 'Closure') ? $action->getClosure() : $action; |
93 | - $ReflectionMethod = new \ReflectionFunction($closure); |
|
93 | + $ReflectionMethod = new \ReflectionFunction($closure); |
|
94 | 94 | |
95 | - foreach($ReflectionMethod->getParameters() as $param){ |
|
96 | - if(!isset($_REQUEST[$param->name])) continue; |
|
95 | + foreach ($ReflectionMethod->getParameters() as $param) { |
|
96 | + if (!isset($_REQUEST[$param->name])) continue; |
|
97 | 97 | $params[$param->name] = $_REQUEST[$param->name]; |
98 | 98 | } |
99 | 99 | |
@@ -111,9 +111,9 @@ discard block |
||
111 | 111 | $excepts = (is_array($excepts)) ? $excepts : []; |
112 | 112 | $group = self::getInstance()->inSave()['group']; |
113 | 113 | |
114 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
115 | - if($route['group'] === $group && !in_array($r, $excepts)) { |
|
116 | - $route[$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
114 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
115 | + if ($route['group'] === $group && !in_array($r, $excepts)) { |
|
116 | + $route[$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
117 | 117 | self::getInstance()->updateRoute($route, $r); |
118 | 118 | } |
119 | 119 | } |
@@ -124,14 +124,14 @@ discard block |
||
124 | 124 | private static function addInRoute(string $state, $closure): RouterInterface |
125 | 125 | { |
126 | 126 | $route = self::getInstance()->inSave(); |
127 | - $route[$state] = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
127 | + $route[$state] = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
128 | 128 | self::updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
129 | 129 | return self::getInstance(); |
130 | 130 | } |
131 | 131 | |
132 | 132 | protected function executeBefore(): void |
133 | 133 | { |
134 | - if(!in_array($this->currentName(), (array) $this->getState('before', true))) { |
|
134 | + if (!in_array($this->currentName(), (array) $this->getState('before', true))) { |
|
135 | 135 | ($this->getState('before', false))(); |
136 | 136 | } |
137 | 137 | |
@@ -142,15 +142,15 @@ discard block |
||
142 | 142 | { |
143 | 143 | $this->executeState('after'); |
144 | 144 | |
145 | - if(!in_array($this->currentName(), (array) $this->getState('after', true))) { |
|
145 | + if (!in_array($this->currentName(), (array) $this->getState('after', true))) { |
|
146 | 146 | ($this->getState('after', false))(); |
147 | 147 | } |
148 | 148 | } |
149 | 149 | |
150 | 150 | private function executeState(string $stage): void |
151 | 151 | { |
152 | - foreach($this->current()[$stage] as $state){ |
|
153 | - if(is_callable($state)) { |
|
152 | + foreach ($this->current()[$stage] as $state) { |
|
153 | + if (is_callable($state)) { |
|
154 | 154 | $state(); |
155 | 155 | continue; |
156 | 156 | } |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | |
162 | 162 | private function executeController(string $controllerMeth): void |
163 | 163 | { |
164 | - if(count(explode('@', $controllerMeth)) !== 2) { |
|
164 | + if (count(explode('@', $controllerMeth)) !== 2) { |
|
165 | 165 | $path = urldecode(unserialize($this->current()['uri'])->getPath()); |
166 | 166 | throw new \RuntimeException("Misconfigured route action ({$path})"); |
167 | 167 | } |
@@ -173,28 +173,28 @@ discard block |
||
173 | 173 | |
174 | 174 | $params = []; |
175 | 175 | |
176 | - $ReflectionMethod = new \ReflectionMethod(new $controller(), $method); |
|
176 | + $ReflectionMethod = new \ReflectionMethod(new $controller(), $method); |
|
177 | 177 | |
178 | - foreach($ReflectionMethod->getParameters() as $param){ |
|
179 | - if(!isset($_REQUEST[$param->name])) continue; |
|
178 | + foreach ($ReflectionMethod->getParameters() as $param) { |
|
179 | + if (!isset($_REQUEST[$param->name])) continue; |
|
180 | 180 | $params[$param->name] = $_REQUEST[$param->name]; |
181 | 181 | } |
182 | 182 | |
183 | - call_user_func_array([(new $controller()),$method], $params); |
|
183 | + call_user_func_array([(new $controller()), $method], $params); |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | private function checkControllerMeth(string $controllerMeth): void |
187 | 187 | { |
188 | - $routeURI = str_replace(['{?','{','}'], '', urldecode(unserialize($this->current()['uri'])->getPath())); |
|
188 | + $routeURI = str_replace(['{?', '{', '}'], '', urldecode(unserialize($this->current()['uri'])->getPath())); |
|
189 | 189 | |
190 | 190 | $controller = (string) explode('@', $controllerMeth)[0]; |
191 | 191 | $method = (string) explode('@', $controllerMeth)[1]; |
192 | 192 | |
193 | - if(!class_exists($controller)) { |
|
193 | + if (!class_exists($controller)) { |
|
194 | 194 | throw new \RuntimeException("Controller not found in route URI {$routeURI}"); |
195 | 195 | } |
196 | 196 | |
197 | - if(!method_exists($controller, $method)) { |
|
197 | + if (!method_exists($controller, $method)) { |
|
198 | 198 | throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}"); |
199 | 199 | } |
200 | 200 |
@@ -93,7 +93,9 @@ discard block |
||
93 | 93 | $ReflectionMethod = new \ReflectionFunction($closure); |
94 | 94 | |
95 | 95 | foreach($ReflectionMethod->getParameters() as $param){ |
96 | - if(!isset($_REQUEST[$param->name])) continue; |
|
96 | + if(!isset($_REQUEST[$param->name])) { |
|
97 | + continue; |
|
98 | + } |
|
97 | 99 | $params[$param->name] = $_REQUEST[$param->name]; |
98 | 100 | } |
99 | 101 | |
@@ -176,7 +178,9 @@ discard block |
||
176 | 178 | $ReflectionMethod = new \ReflectionMethod(new $controller(), $method); |
177 | 179 | |
178 | 180 | foreach($ReflectionMethod->getParameters() as $param){ |
179 | - if(!isset($_REQUEST[$param->name])) continue; |
|
181 | + if(!isset($_REQUEST[$param->name])) { |
|
182 | + continue; |
|
183 | + } |
|
180 | 184 | $params[$param->name] = $_REQUEST[$param->name]; |
181 | 185 | } |
182 | 186 |
@@ -12,8 +12,8 @@ |
||
12 | 12 | private array $methods, |
13 | 13 | private string $name, |
14 | 14 | private ?array $where, |
15 | - private ?string|Closure $after, |
|
16 | - private ?string|Closure $before, |
|
15 | + private ?string | Closure $after, |
|
16 | + private ?string | Closure $before, |
|
17 | 17 | private ?array $attributes, |
18 | 18 | private ?array $middleware |
19 | 19 | ) { |
@@ -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 | |
@@ -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,16 +49,16 @@ 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 | |
@@ -67,12 +67,12 @@ discard block |
||
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 | 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 | |
@@ -56,7 +56,7 @@ discard block |
||
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 | 61 | self::checkDuplicity($uri, $method); |
62 | 62 | |
@@ -64,7 +64,7 @@ discard block |
||
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 | |
@@ -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 | } |
@@ -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 | } |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | |
82 | 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 | |
@@ -98,14 +98,14 @@ discard block |
||
98 | 98 | |
99 | 99 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
100 | 100 | { |
101 | - if(!$this->getInstance()->loaded()) { |
|
101 | + if (!$this->getInstance()->loaded()) { |
|
102 | 102 | $this->getInstance()->load(); |
103 | 103 | } |
104 | 104 | |
105 | 105 | $route = $this->getInstance()->current(); |
106 | - if(!empty($route)) { |
|
106 | + if (!empty($route)) { |
|
107 | 107 | |
108 | - $route['action'] = function () { |
|
108 | + $route['action'] = function() { |
|
109 | 109 | $this->getInstance()->executeBefore(); |
110 | 110 | $this->getInstance()->executeRouteAction(unserialize($this->getInstance()->current()['action'])); |
111 | 111 | $this->getInstance()->executeAfter(); |
@@ -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,23 +19,23 @@ 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 | |
@@ -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; |
@@ -35,7 +35,9 @@ discard block |
||
35 | 35 | { |
36 | 36 | try{ |
37 | 37 | foreach ($method->getAttributes() as $attr) { |
38 | - if($attr->getName() != 'HnrAzevedo\Router\RouteAttribute') continue; |
|
38 | + if($attr->getName() != 'HnrAzevedo\Router\RouteAttribute') { |
|
39 | + continue; |
|
40 | + } |
|
39 | 41 | |
40 | 42 | $args = $attr->getArguments(); |
41 | 43 | |
@@ -54,7 +56,7 @@ discard block |
||
54 | 56 | ->attrWhere($args) |
55 | 57 | ->attrMiddleware($args); |
56 | 58 | } |
57 | - }catch(Exception $er){ |
|
59 | + } catch(Exception $er){ |
|
58 | 60 | throw new Exception('Failed to add route via attribute: '.$er->getMessage()); |
59 | 61 | } |
60 | 62 | } |
@@ -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 | { |
21 | 21 | $this->hasRouteName($name); |
22 | 22 | |
23 | - if(!$this->loaded()) { |
|
23 | + if (!$this->loaded()) { |
|
24 | 24 | $this->loadIn($name); |
25 | 25 | } |
26 | 26 | |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | |
30 | 30 | public function hasCurrentRoute(): void |
31 | 31 | { |
32 | - if(!isset($this->currentRoute)) { |
|
32 | + if (!isset($this->currentRoute)) { |
|
33 | 33 | throw new \RuntimeException('Route not yet loaded'); |
34 | 34 | } |
35 | 35 | } |
@@ -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,19 +20,19 @@ 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 | |
@@ -48,7 +48,7 @@ discard block |
||
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 | } |