@@ -8,23 +8,23 @@ |
||
8 | 8 | use Psr\Http\Server\MiddlewareInterface; |
9 | 9 | use Psr\Http\Server\RequestHandlerInterface; |
10 | 10 | |
11 | -class Middleware implements MiddlewareInterface{ |
|
11 | +class Middleware implements MiddlewareInterface { |
|
12 | 12 | protected static array $data = []; |
13 | 13 | protected static Response $response; |
14 | 14 | |
15 | 15 | public function __get($key) |
16 | 16 | { |
17 | - return (array_key_exists($key,self::$data)) ? self::$data[$key] : null; |
|
17 | + return (array_key_exists($key, self::$data)) ? self::$data[$key] : null; |
|
18 | 18 | } |
19 | 19 | |
20 | - public function __set($key,$value) |
|
20 | + public function __set($key, $value) |
|
21 | 21 | { |
22 | 22 | self::$data[$key] = $value; |
23 | 23 | } |
24 | 24 | |
25 | 25 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
26 | 26 | { |
27 | - if(!isset(self::$response)){ |
|
27 | + if (!isset(self::$response)) { |
|
28 | 28 | self::$response = new Response(); |
29 | 29 | } |
30 | 30 | return self::$response; |
@@ -8,8 +8,8 @@ |
||
8 | 8 | use Psr\Http\Server\RequestHandlerInterface; |
9 | 9 | |
10 | 10 | /** |
11 | - * @property string $error |
|
12 | - */ |
|
11 | + * @property string $error |
|
12 | + */ |
|
13 | 13 | class Lasted extends Middleware{ |
14 | 14 | |
15 | 15 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
@@ -10,11 +10,11 @@ |
||
10 | 10 | /** |
11 | 11 | * @property string $error |
12 | 12 | */ |
13 | -class Lasted extends Middleware{ |
|
13 | +class Lasted extends Middleware { |
|
14 | 14 | |
15 | 15 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
16 | 16 | { |
17 | - if(!isset($this->error)){ |
|
17 | + if (!isset($this->error)) { |
|
18 | 18 | throw new Exception("Access not belonged: {$this->error}"); |
19 | 19 | } |
20 | 20 |
@@ -7,8 +7,8 @@ |
||
7 | 7 | use Psr\Http\Server\RequestHandlerInterface; |
8 | 8 | |
9 | 9 | /** |
10 | - * @property string $error |
|
11 | - */ |
|
10 | + * @property string $error |
|
11 | + */ |
|
12 | 12 | class Auth extends Middleware{ |
13 | 13 | |
14 | 14 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
@@ -9,11 +9,11 @@ |
||
9 | 9 | /** |
10 | 10 | * @property string $error |
11 | 11 | */ |
12 | -class Auth extends Middleware{ |
|
12 | +class Auth extends Middleware { |
|
13 | 13 | |
14 | 14 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
15 | 15 | { |
16 | - if(!array_key_exists('user',$_SESSION)){ |
|
16 | + if (!array_key_exists('user', $_SESSION)) { |
|
17 | 17 | $this->error = 'The user must be logged in to the system'; |
18 | 18 | } |
19 | 19 |
@@ -77,7 +77,7 @@ |
||
77 | 77 | |
78 | 78 | runMiddlewares($serverRequest); |
79 | 79 | |
80 | -}catch(Exception $er){ |
|
80 | +} catch(Exception $er){ |
|
81 | 81 | |
82 | 82 | die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}."); |
83 | 83 |
@@ -14,15 +14,15 @@ discard block |
||
14 | 14 | use HnrAzevedo\Router\Router; |
15 | 15 | use Psr\Http\Server\MiddlewareInterface; |
16 | 16 | |
17 | -try{ |
|
17 | +try { |
|
18 | 18 | $requestMethod = (isset($_REQUEST['REQUEST_METHOD'])) ? $_REQUEST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']; |
19 | 19 | |
20 | 20 | $serverRequest = (new Factory())->createServerRequest($requestMethod, new Uri($_SERVER['REQUEST_URI'])); |
21 | 21 | |
22 | - class App implements MiddlewareInterface{ |
|
22 | + class App implements MiddlewareInterface { |
|
23 | 23 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
24 | 24 | { |
25 | - if(empty($request->getAttribute('route'))) |
|
25 | + if (empty($request->getAttribute('route'))) |
|
26 | 26 | { |
27 | 27 | throw new Exception('Page not found', 404); |
28 | 28 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | } |
34 | 34 | } |
35 | 35 | |
36 | - define('GLOBAL_MIDDLEWARES',[ |
|
36 | + define('GLOBAL_MIDDLEWARES', [ |
|
37 | 37 | Router::class, |
38 | 38 | App::class |
39 | 39 | ]); |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | |
70 | 70 | function runMiddlewares($serverRequest) |
71 | 71 | { |
72 | - nextExample(new class implements RequestHandlerInterface{ |
|
72 | + nextExample(new class implements RequestHandlerInterface { |
|
73 | 73 | public function handle(ServerRequestInterface $request): ResponseInterface |
74 | 74 | { |
75 | 75 | return (new Factory())->createResponse(200); |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | |
80 | 80 | runMiddlewares($serverRequest); |
81 | 81 | |
82 | -}catch(Exception $er){ |
|
82 | +}catch (Exception $er) { |
|
83 | 83 | |
84 | 84 | die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}."); |
85 | 85 |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | |
8 | 8 | use HnrAzevedo\Router\Router; |
9 | 9 | |
10 | -try{ |
|
10 | +try { |
|
11 | 11 | |
12 | 12 | Router::defineHost('https://localhost'); |
13 | 13 | |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | */ |
29 | 29 | $action = Router::currentAction(); |
30 | 30 | |
31 | -}catch(Exception $er){ |
|
31 | +}catch (Exception $er) { |
|
32 | 32 | |
33 | 33 | die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}."); |
34 | 34 |
@@ -28,7 +28,7 @@ |
||
28 | 28 | */ |
29 | 29 | $action = Router::currentAction(); |
30 | 30 | |
31 | -}catch(Exception $er){ |
|
31 | +} catch(Exception $er){ |
|
32 | 32 | |
33 | 33 | die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}."); |
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 | |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | |
55 | 55 | protected function setRoutes(array $routes): void |
56 | 56 | { |
57 | - $this->routes = $routes; |
|
57 | + $this->routes = $routes; |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | protected function getGroup(): ?string |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace HnrAzevedo\Router; |
6 | 6 |
@@ -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 |
@@ -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 | } |