@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | { |
17 | 17 | $this->hasRouteName($name); |
18 | 18 | |
19 | - if(!$this->loaded){ |
|
19 | + if (!$this->loaded) { |
|
20 | 20 | $this->loadIn($name); |
21 | 21 | } |
22 | 22 | |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | |
26 | 26 | public function hasCurrentRoute(): void |
27 | 27 | { |
28 | - if(!isset($this->currentRoute)){ |
|
28 | + if (!isset($this->currentRoute)) { |
|
29 | 29 | throw new \RuntimeException('Route not yet loaded'); |
30 | 30 | } |
31 | 31 | } |
@@ -26,7 +26,7 @@ |
||
26 | 26 | /* Return current action route */ |
27 | 27 | $action = Router::currentAction(); |
28 | 28 | |
29 | -}catch(Exception $er){ |
|
29 | +} catch(Exception $er){ |
|
30 | 30 | |
31 | 31 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
32 | 32 |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | |
10 | 10 | /* NOTE: in case of error an exception is thrown */ |
11 | 11 | |
12 | -try{ |
|
12 | +try { |
|
13 | 13 | |
14 | 14 | Router::globalMiddlewares([ |
15 | 15 | 'Lasted'=> \HnrAzevedo\Router\Example\Middleware\Lasted::class |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | /* Return current action route */ |
27 | 27 | $action = Router::currentAction(); |
28 | 28 | |
29 | -}catch(Exception $er){ |
|
29 | +}catch (Exception $er) { |
|
30 | 30 | |
31 | 31 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
32 | 32 |
@@ -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; |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | self::getInstance()->checkMethod($route, $_SERVER['REQUEST_METHOD']); |
80 | 80 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
81 | 81 | return self::getInstance(); |
82 | - }catch(\Exception $er){ |
|
82 | + } catch(\Exception $er){ |
|
83 | 83 | continue; |
84 | 84 | } |
85 | 85 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | |
99 | 99 | try{ |
100 | 100 | self::getInstance()->executeRouteAction(); |
101 | - }catch(\Exception $er){ |
|
101 | + } catch(\Exception $er){ |
|
102 | 102 | self::getInstance()->error = $er; |
103 | 103 | } |
104 | 104 |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | $route = self::getInstance()->inSave(); |
26 | 26 | $route['name'] = $name; |
27 | 27 | self::getInstance()->routesName[$name] = $name; |
28 | - self::getInstance()->unsetRoute(array_key_last(self::getInstance()->getRoutes()))->updateRoute($route,$name); |
|
28 | + self::getInstance()->unsetRoute(array_key_last(self::getInstance()->getRoutes()))->updateRoute($route, $name); |
|
29 | 29 | return self::getInstance(); |
30 | 30 | } |
31 | 31 | |
@@ -64,15 +64,15 @@ discard block |
||
64 | 64 | self::getInstance()->loaded = true; |
65 | 65 | self::getInstance()->sortRoutes(); |
66 | 66 | |
67 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
67 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
68 | 68 | self::getInstance()->currentRoute = $route; |
69 | 69 | self::getInstance()->currentRoute['name'] = $r; |
70 | 70 | |
71 | - try{ |
|
71 | + try { |
|
72 | 72 | self::getInstance()->checkMethod($route, $_SERVER['REQUEST_METHOD']); |
73 | 73 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
74 | 74 | return self::getInstance(); |
75 | - }catch(\Exception $er){ |
|
75 | + }catch (\Exception $er) { |
|
76 | 76 | continue; |
77 | 77 | } |
78 | 78 | } |
@@ -83,15 +83,15 @@ discard block |
||
83 | 83 | |
84 | 84 | public static function run(): RouterInterface |
85 | 85 | { |
86 | - if(!self::getInstance()->loaded){ |
|
86 | + if (!self::getInstance()->loaded) { |
|
87 | 87 | self::getInstance()->load(); |
88 | 88 | } |
89 | 89 | |
90 | 90 | self::getInstance()->executeBefore(); |
91 | 91 | |
92 | - try{ |
|
92 | + try { |
|
93 | 93 | self::getInstance()->executeRouteAction(); |
94 | - }catch(\Exception $er){ |
|
94 | + }catch (\Exception $er) { |
|
95 | 95 | self::getInstance()->error = $er; |
96 | 96 | } |
97 | 97 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | |
105 | 105 | private function checkError(): void |
106 | 106 | { |
107 | - if(isset($this->error)){ |
|
107 | + if (isset($this->error)) { |
|
108 | 108 | throw $this->error; |
109 | 109 | } |
110 | 110 | } |
@@ -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,12 +10,12 @@ |
||
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 | 17 | var_dump('Lasted'); |
18 | - if(!isset($this->error)){ |
|
18 | + if (!isset($this->error)) { |
|
19 | 19 | throw new Exception("Access not belonged: {$this->error}"); |
20 | 20 | } |
21 | 21 |
@@ -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,12 +9,12 @@ |
||
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 | 16 | var_dump('Auth'); |
17 | - if(!array_key_exists('user',$_SESSION)){ |
|
17 | + if (!array_key_exists('user', $_SESSION)) { |
|
18 | 18 | $this->error = 'The user must be logged in to the system'; |
19 | 19 | } |
20 | 20 |
@@ -11,27 +11,27 @@ discard block |
||
11 | 11 | $staticRoutes = []; |
12 | 12 | $paramRoutes = []; |
13 | 13 | |
14 | - foreach($this->getRoutes() as $r => $route){ |
|
14 | + foreach ($this->getRoutes() as $r => $route) { |
|
15 | 15 | |
16 | 16 | $path = urldecode($route['uri']->getPath()); |
17 | 17 | |
18 | - if(strstr($path,'{')){ |
|
19 | - $paramRoutes[$this->getKeyArray(substr_count($path,'/') + substr_count($path,'{'),$paramRoutes)] = $route; |
|
18 | + if (strstr($path, '{')) { |
|
19 | + $paramRoutes[$this->getKeyArray(substr_count($path, '/')+substr_count($path, '{'), $paramRoutes)] = $route; |
|
20 | 20 | continue; |
21 | 21 | } |
22 | 22 | |
23 | - $staticRoutes[$this->getKeyArray(substr_count($path,'/'),$staticRoutes)] = $route; |
|
23 | + $staticRoutes[$this->getKeyArray(substr_count($path, '/'), $staticRoutes)] = $route; |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | rsort($paramRoutes); |
27 | 27 | rsort($staticRoutes); |
28 | 28 | |
29 | - $this->orderRoutes(array_merge($staticRoutes,$paramRoutes)); |
|
29 | + $this->orderRoutes(array_merge($staticRoutes, $paramRoutes)); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | private function getKeyArray(int $index, array $array): int |
33 | 33 | { |
34 | - while(array_key_exists($index,$array)){ |
|
34 | + while (array_key_exists($index, $array)) { |
|
35 | 35 | $index++; |
36 | 36 | } |
37 | 37 | return $index; |
@@ -40,8 +40,8 @@ discard block |
||
40 | 40 | private function orderRoutes(array $routes):void |
41 | 41 | { |
42 | 42 | $kRoutes = $routes; |
43 | - foreach($routes as $r => $route){ |
|
44 | - if(array_key_exists('name',$route)){ |
|
43 | + foreach ($routes as $r => $route) { |
|
44 | + if (array_key_exists('name', $route)) { |
|
45 | 45 | unset($kRoutes[$r]); |
46 | 46 | $kRoutes["'{$route['name']}'"] = $route; |
47 | 47 | } |
@@ -45,7 +45,7 @@ |
||
45 | 45 | |
46 | 46 | protected function setRoutes(array $routes): void |
47 | 47 | { |
48 | - $this->routes = $routes; |
|
48 | + $this->routes = $routes; |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | protected function getGroup(): ?string |
@@ -10,28 +10,28 @@ discard block |
||
10 | 10 | |
11 | 11 | protected function hasRouteName(string $name): void |
12 | 12 | { |
13 | - if(!isset($this->routesName[$name])){ |
|
13 | + if (!isset($this->routesName[$name])) { |
|
14 | 14 | throw new \RuntimeException("There is no route named with {$name}"); |
15 | 15 | } |
16 | 16 | } |
17 | 17 | |
18 | 18 | protected function isInNameGroup(): void |
19 | 19 | { |
20 | - if(!is_null($this->getGroup())){ |
|
20 | + if (!is_null($this->getGroup())) { |
|
21 | 21 | throw new \RuntimeException("It is not allowed to assign names to groups"); |
22 | 22 | } |
23 | 23 | } |
24 | 24 | |
25 | 25 | protected function isInPseudGroup(): void |
26 | 26 | { |
27 | - if(!is_null($this->getGroup())){ |
|
27 | + if (!is_null($this->getGroup())) { |
|
28 | 28 | throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup"); |
29 | 29 | } |
30 | 30 | } |
31 | 31 | |
32 | 32 | protected function existRouteName(string $name): void |
33 | 33 | { |
34 | - if(isset($this->routesName[$name])){ |
|
34 | + if (isset($this->routesName[$name])) { |
|
35 | 35 | throw new \RuntimeException("There is already a route named with {$name}"); |
36 | 36 | } |
37 | 37 | } |
@@ -39,12 +39,12 @@ discard block |
||
39 | 39 | protected function checkMethod(array $route, $method): void |
40 | 40 | { |
41 | 41 | $hasMethod = false; |
42 | - foreach(explode('|',$route['method']) as $routeMethod){ |
|
43 | - if(@preg_match("/{$routeMethod}/",$method) !== 0 || $method === '*'){ |
|
42 | + foreach (explode('|', $route['method']) as $routeMethod) { |
|
43 | + if (@preg_match("/{$routeMethod}/", $method) !== 0 || $method === '*') { |
|
44 | 44 | $hasMethod = true; |
45 | 45 | } |
46 | 46 | } |
47 | - if(!$hasMethod){ |
|
47 | + if (!$hasMethod) { |
|
48 | 48 | throw new \Exception('This route is not released for the accessed method'); |
49 | 49 | } |
50 | 50 |