@@ -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,7 +84,7 @@ 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 | call_user_func_array($action, $_REQUEST); |
89 | 89 | return true; |
90 | 90 | } |
@@ -99,9 +99,9 @@ discard block |
||
99 | 99 | $excepts = (is_array($excepts)) ? $excepts : []; |
100 | 100 | $group = self::getInstance()->inSave()['group']; |
101 | 101 | |
102 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
103 | - if($route['group'] === $group && !in_array($r, $excepts)){ |
|
104 | - $route[$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
102 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
103 | + if ($route['group'] === $group && !in_array($r, $excepts)) { |
|
104 | + $route[$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
105 | 105 | self::getInstance()->updateRoute($route, $r); |
106 | 106 | } |
107 | 107 | } |
@@ -112,14 +112,14 @@ discard block |
||
112 | 112 | private static function addInRoute(string $state, $closure): RouterInterface |
113 | 113 | { |
114 | 114 | $route = self::getInstance()->inSave(); |
115 | - $route[$state] = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
116 | - self::updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
115 | + $route[$state] = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
116 | + self::updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
117 | 117 | return self::getInstance(); |
118 | 118 | } |
119 | 119 | |
120 | 120 | protected function executeBefore(): void |
121 | 121 | { |
122 | - if(!in_array($this->currentName(), (array) $this->getState('before', true))){ |
|
122 | + if (!in_array($this->currentName(), (array) $this->getState('before', true))) { |
|
123 | 123 | ($this->getState('before', false))(); |
124 | 124 | } |
125 | 125 | |
@@ -130,15 +130,15 @@ discard block |
||
130 | 130 | { |
131 | 131 | $this->executeState('after'); |
132 | 132 | |
133 | - if(!in_array($this->currentName(), (array) $this->getState('after', true))){ |
|
133 | + if (!in_array($this->currentName(), (array) $this->getState('after', true))) { |
|
134 | 134 | ($this->getState('after', false))(); |
135 | 135 | } |
136 | 136 | } |
137 | 137 | |
138 | 138 | private function executeState(string $stage): void |
139 | 139 | { |
140 | - foreach($this->current()[$stage] as $state){ |
|
141 | - if(is_callable($state)){ |
|
140 | + foreach ($this->current()[$stage] as $state) { |
|
141 | + if (is_callable($state)) { |
|
142 | 142 | $state(); |
143 | 143 | continue; |
144 | 144 | } |
@@ -149,31 +149,31 @@ discard block |
||
149 | 149 | |
150 | 150 | private function executeController(string $controllerMeth): void |
151 | 151 | { |
152 | - if(count(explode('@',$controllerMeth)) !== 2){ |
|
152 | + if (count(explode('@', $controllerMeth)) !== 2) { |
|
153 | 153 | $path = urldecode($this->current()['uri']->getPath()); |
154 | 154 | throw new \RuntimeException("Misconfigured route action ({$path})"); |
155 | 155 | } |
156 | 156 | |
157 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
158 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
157 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
158 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
159 | 159 | |
160 | 160 | $this->checkControllerMeth($controllerMeth); |
161 | 161 | |
162 | - call_user_func_array([(new $controller()),$method], $_REQUEST); |
|
162 | + call_user_func_array([(new $controller()), $method], $_REQUEST); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | private function checkControllerMeth(string $controllerMeth): void |
166 | 166 | { |
167 | - $routeURI = str_replace(['{:','{','}'],'',urldecode(unserialize($this->current()['uri'])->getPath())); |
|
167 | + $routeURI = str_replace(['{:', '{', '}'], '', urldecode(unserialize($this->current()['uri'])->getPath())); |
|
168 | 168 | |
169 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
170 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
169 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
170 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
171 | 171 | |
172 | - if(!class_exists($controller)){ |
|
172 | + if (!class_exists($controller)) { |
|
173 | 173 | throw new \RuntimeException("Controller not found in route URI {$routeURI}"); |
174 | 174 | } |
175 | 175 | |
176 | - if(!method_exists($controller, $method)){ |
|
176 | + if (!method_exists($controller, $method)) { |
|
177 | 177 | throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}"); |
178 | 178 | } |
179 | 179 |