@@ -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 | } |
@@ -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 | |
@@ -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; |
@@ -7,12 +7,12 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Defines a route for the GET method |
9 | 9 | */ |
10 | -Router::get('/foo','HnrAzevedo\Router\Example\Controllers\Controller@method'); |
|
10 | +Router::get('/foo', 'HnrAzevedo\Router\Example\Controllers\Controller@method'); |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * Defines a route to the GET method with an anonymous function |
14 | 14 | */ |
15 | -Router::get('/bar', function(){ |
|
15 | +Router::get('/bar', function() { |
|
16 | 16 | // |
17 | 17 | echo '/bar executed!'; |
18 | 18 | }); |
@@ -20,14 +20,14 @@ discard block |
||
20 | 20 | /** |
21 | 21 | * Defines a route to the POST method with an anonymous function |
22 | 22 | */ |
23 | -Router::post('/bar', function(){ |
|
23 | +Router::post('/bar', function() { |
|
24 | 24 | // |
25 | 25 | }); |
26 | 26 | |
27 | 27 | /** |
28 | 28 | * Defining route for any method |
29 | 29 | */ |
30 | -Router::any('/any', function(){ |
|
30 | +Router::any('/any', function() { |
|
31 | 31 | // |
32 | 32 | }); |
33 | 33 | |
@@ -37,14 +37,14 @@ discard block |
||
37 | 37 | * @param string $route |
38 | 38 | * @param \Closure|string $action |
39 | 39 | */ |
40 | -Router::match('GET|post', '/get_post', function(){ |
|
40 | +Router::match('GET|post', '/get_post', function() { |
|
41 | 41 | // |
42 | 42 | }); |
43 | 43 | |
44 | 44 | /** |
45 | 45 | * Defines the route name |
46 | 46 | */ |
47 | -Router::get('/named', function(){ |
|
47 | +Router::get('/named', function() { |
|
48 | 48 | // |
49 | 49 | })->name('baz'); |
50 | 50 | |
@@ -56,32 +56,32 @@ discard block |
||
56 | 56 | /** |
57 | 57 | * Run before all requests, regardless of error occurrences |
58 | 58 | */ |
59 | -Router::beforeAll(function(){ |
|
59 | +Router::beforeAll(function() { |
|
60 | 60 | // |
61 | 61 | }, null); |
62 | 62 | |
63 | 63 | /** |
64 | 64 | * Run after all requests, regardless of error occurrences |
65 | 65 | */ |
66 | -Router::afterAll(function(){ |
|
66 | +Router::afterAll(function() { |
|
67 | 67 | // |
68 | 68 | }, null); |
69 | 69 | |
70 | 70 | /** |
71 | 71 | * Executes after executing the triggered route action |
72 | 72 | */ |
73 | -Router::get('/after', function(){ |
|
73 | +Router::get('/after', function() { |
|
74 | 74 | // |
75 | -})->after(function(){ |
|
75 | +})->after(function() { |
|
76 | 76 | // |
77 | 77 | }); |
78 | 78 | |
79 | 79 | /** |
80 | 80 | * Executes before executing the triggered route action |
81 | 81 | */ |
82 | -Router::get('/before', function(){ |
|
82 | +Router::get('/before', function() { |
|
83 | 83 | // |
84 | -})->before(function(){ |
|
84 | +})->before(function() { |
|
85 | 85 | // |
86 | 86 | }); |
87 | 87 | /** |
@@ -102,21 +102,21 @@ discard block |
||
102 | 102 | /** |
103 | 103 | * Example of route definition with parameters |
104 | 104 | */ |
105 | -Router::get('/passingParameters/{param}', function($param){ |
|
105 | +Router::get('/passingParameters/{param}', function($param) { |
|
106 | 106 | echo $param; |
107 | 107 | }); |
108 | 108 | |
109 | 109 | /** |
110 | 110 | * Example of setting an optional parameter |
111 | 111 | */ |
112 | -Router::get('/passingParameters/{?optionalParam}', function($optionalParam){ |
|
112 | +Router::get('/passingParameters/{?optionalParam}', function($optionalParam) { |
|
113 | 113 | echo $optionalParam; |
114 | 114 | }); |
115 | 115 | |
116 | 116 | /** |
117 | 117 | * Example of regular expression test for parameters |
118 | 118 | */ |
119 | -Router::get('/testingParameters/{param}', function($param){ |
|
119 | +Router::get('/testingParameters/{param}', function($param) { |
|
120 | 120 | echo $param; |
121 | 121 | })->where([ |
122 | 122 | 'param'=>'[0-9]{1,11}' |
@@ -132,16 +132,16 @@ discard block |
||
132 | 132 | * @param string $prefix |
133 | 133 | * @param \Closure $routeDefinitions |
134 | 134 | */ |
135 | -Router::group('/admin', function(){ |
|
136 | - Router::get('/users/{teste}', function($teste){ |
|
135 | +Router::group('/admin', function() { |
|
136 | + Router::get('/users/{teste}', function($teste) { |
|
137 | 137 | echo $teste; |
138 | 138 | })->where([ |
139 | 139 | 'teste'=>'[a-zA-Z]*' |
140 | 140 | ]); |
141 | 141 | }); |
142 | 142 | |
143 | -Router::group('/admin2', function(){ |
|
144 | - Router::get('/users/{teste}', function($teste){ |
|
143 | +Router::group('/admin2', function() { |
|
144 | + Router::get('/users/{teste}', function($teste) { |
|
145 | 145 | echo $teste; |
146 | 146 | })->name('teste'); |
147 | 147 | }) |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | */ |
153 | 153 | ->groupWhere([ |
154 | 154 | 'teste'=>'[a-zA-Z]*' |
155 | -],[]); |
|
155 | +], []); |
|
156 | 156 | |
157 | 157 | |
158 | 158 | |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | /** |
170 | 170 | * Defining route middleware - implements Psr\Http\Server\MiddlewareInterface |
171 | 171 | */ |
172 | -Router::get('/passingParameters/{?optionalParam}', function($optionalParam = null){ |
|
172 | +Router::get('/passingParameters/{?optionalParam}', function($optionalParam = null) { |
|
173 | 173 | echo $optionalParam; |
174 | 174 | })->middleware([ |
175 | 175 | HnrAzevedo\Router\Example\Middleware\Auth::class |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | /** |
179 | 179 | * Defining middleware by nickname |
180 | 180 | */ |
181 | -Router::get('/lasted', function(){ |
|
181 | +Router::get('/lasted', function() { |
|
182 | 182 | // |
183 | 183 | })->middleware([ |
184 | 184 | 'Lasted' |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | * Defining multiple middlewares |
189 | 189 | * NOTE: Importantly, the execution follows the same order of definition |
190 | 190 | */ |
191 | -Router::get('/middlewares', function(){ |
|
191 | +Router::get('/middlewares', function() { |
|
192 | 192 | // |
193 | 193 | })->middleware([ |
194 | 194 | HnrAzevedo\Router\Example\Middleware\Auth::class, |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | |
8 | 8 | use HnrAzevedo\Router\Router; |
9 | 9 | |
10 | -try{ |
|
10 | +try { |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * Add to composer.json autoload |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | */ |
36 | 36 | $action = Router::currentAction(); |
37 | 37 | |
38 | -}catch(Exception $er){ |
|
38 | +}catch (Exception $er) { |
|
39 | 39 | |
40 | 40 | die("Code Error: {$er->getCode()}".PHP_EOL."Line: {$er->getLine()}".PHP_EOL."File: {$er->getFile()}".PHP_EOL."Message: {$er->getMessage()}."); |
41 | 41 |
@@ -35,7 +35,7 @@ |
||
35 | 35 | */ |
36 | 36 | $action = Router::currentAction(); |
37 | 37 | |
38 | -}catch(Exception $er){ |
|
38 | +} catch(Exception $er){ |
|
39 | 39 | |
40 | 40 | die("Code Error: {$er->getCode()}".PHP_EOL."Line: {$er->getLine()}".PHP_EOL."File: {$er->getFile()}".PHP_EOL."Message: {$er->getMessage()}."); |
41 | 41 |
@@ -4,7 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use HnrAzevedo\Router\RouteAttribute; |
6 | 6 | |
7 | -class ControllerAttribute{ |
|
7 | +class ControllerAttribute { |
|
8 | 8 | |
9 | 9 | #[RouteAttribute( |
10 | 10 | uri:'/fooo/{param}', |
@@ -2,7 +2,7 @@ |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Router\Example\Controllers; |
4 | 4 | |
5 | -class Controller{ |
|
5 | +class Controller { |
|
6 | 6 | |
7 | 7 | public function method(): void |
8 | 8 | { |
@@ -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 |