@@ -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 | } |
@@ -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,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 |
@@ -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 |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | |
16 | 16 | protected function getState(string $state, bool $except = false) |
17 | 17 | { |
18 | - if($state === 'before'){ |
|
18 | + if ($state === 'before') { |
|
19 | 19 | return ($except) ? $this->beforeExcepts : $this->beforeAll; |
20 | 20 | } |
21 | 21 | |
@@ -24,8 +24,8 @@ discard block |
||
24 | 24 | |
25 | 25 | protected function setState(string $state, $settable, bool $except = false): bool |
26 | 26 | { |
27 | - if($state === 'before'){ |
|
28 | - if($except){ |
|
27 | + if ($state === 'before') { |
|
28 | + if ($except) { |
|
29 | 29 | $this->beforeExcepts = $settable; |
30 | 30 | return true; |
31 | 31 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | return true; |
35 | 35 | } |
36 | 36 | |
37 | - if($except){ |
|
37 | + if ($except) { |
|
38 | 38 | $this->afterExcepts = $settable; |
39 | 39 | return true; |
40 | 40 | } |
@@ -45,24 +45,24 @@ discard block |
||
45 | 45 | |
46 | 46 | public static function before($closure): RouterInterface |
47 | 47 | { |
48 | - return self::addInRoute('before',$closure); |
|
48 | + return self::addInRoute('before', $closure); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | public static function after($closure): RouterInterface |
52 | 52 | { |
53 | - return self::addInRoute('after',$closure); |
|
53 | + return self::addInRoute('after', $closure); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | public static function beforeAll($closure, $excepts): RouterInterface |
57 | 57 | { |
58 | - self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [ $excepts ] ,true); |
|
58 | + self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [$excepts], true); |
|
59 | 59 | self::getInstance()->setState('before', $closure, false); |
60 | 60 | return self::getInstance(); |
61 | 61 | } |
62 | 62 | |
63 | 63 | public static function afterAll($closure, $excepts): RouterInterface |
64 | 64 | { |
65 | - self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [ $excepts ] ,true); |
|
65 | + self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [$excepts], true); |
|
66 | 66 | self::getInstance()->setState('after', $closure, false); |
67 | 67 | return self::getInstance(); |
68 | 68 | } |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | |
80 | 80 | protected function executeRouteAction(): bool |
81 | 81 | { |
82 | - if(is_callable($this->current()['action'])){ |
|
82 | + if (is_callable($this->current()['action'])) { |
|
83 | 83 | call_user_func_array($this->current()['action'], $_REQUEST); |
84 | 84 | return true; |
85 | 85 | } |
@@ -91,12 +91,12 @@ discard block |
||
91 | 91 | private static function addInRoutes(string $state, $closure, $excepts): RouterInterface |
92 | 92 | { |
93 | 93 | self::getInstance()->isInPseudGroup(); |
94 | - $excepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
94 | + $excepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
95 | 95 | $group = self::getInstance()->inSave()['group']; |
96 | 96 | |
97 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
98 | - if($route['group'] === $group && !in_array($r,$excepts)){ |
|
99 | - self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
97 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
98 | + if ($route['group'] === $group && !in_array($r, $excepts)) { |
|
99 | + self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
100 | 100 | } |
101 | 101 | } |
102 | 102 | |
@@ -106,14 +106,14 @@ discard block |
||
106 | 106 | private static function addInRoute(string $state, $closure): RouterInterface |
107 | 107 | { |
108 | 108 | $route = self::getInstance()->inSave(); |
109 | - $route[$state] = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
110 | - self::updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
109 | + $route[$state] = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
110 | + self::updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
111 | 111 | return self::getInstance(); |
112 | 112 | } |
113 | 113 | |
114 | 114 | protected function executeBefore(): void |
115 | 115 | { |
116 | - if(!in_array($this->currentName(), (array) $this->getState('before', true))){ |
|
116 | + if (!in_array($this->currentName(), (array) $this->getState('before', true))) { |
|
117 | 117 | ($this->getState('before', false))(); |
118 | 118 | } |
119 | 119 | |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | |
123 | 123 | protected function executeAfter(): void |
124 | 124 | { |
125 | - if(!in_array($this->currentName(), (array) $this->getState('after', true))){ |
|
125 | + if (!in_array($this->currentName(), (array) $this->getState('after', true))) { |
|
126 | 126 | ($this->getState('after', false))(); |
127 | 127 | } |
128 | 128 | |
@@ -131,8 +131,8 @@ discard block |
||
131 | 131 | |
132 | 132 | private function executeState(string $stage): void |
133 | 133 | { |
134 | - foreach($this->current()[$stage] as $state){ |
|
135 | - if(is_callable($state)){ |
|
134 | + foreach ($this->current()[$stage] as $state) { |
|
135 | + if (is_callable($state)) { |
|
136 | 136 | $state(); |
137 | 137 | continue; |
138 | 138 | } |
@@ -143,31 +143,31 @@ discard block |
||
143 | 143 | |
144 | 144 | private function executeController(string $controllerMeth): void |
145 | 145 | { |
146 | - if(count(explode('@',$controllerMeth)) !== 2){ |
|
146 | + if (count(explode('@', $controllerMeth)) !== 2) { |
|
147 | 147 | $path = urldecode($this->current()['uri']->getPath()); |
148 | 148 | throw new \RuntimeException("Misconfigured route action ({$path})"); |
149 | 149 | } |
150 | 150 | |
151 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
152 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
151 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
152 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
153 | 153 | |
154 | 154 | $this->checkControllerMeth($controllerMeth); |
155 | 155 | |
156 | - call_user_func_array([(new $controller()),$method], $_REQUEST); |
|
156 | + call_user_func_array([(new $controller()), $method], $_REQUEST); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | private function checkControllerMeth(string $controllerMeth): void |
160 | 160 | { |
161 | - $routeURI = str_replace(['{:','{','}'],'',urldecode($this->current()['uri']->getPath())); |
|
161 | + $routeURI = str_replace(['{:', '{', '}'], '', urldecode($this->current()['uri']->getPath())); |
|
162 | 162 | |
163 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
164 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
163 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
164 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
165 | 165 | |
166 | - if(!class_exists($controller)){ |
|
166 | + if (!class_exists($controller)) { |
|
167 | 167 | throw new \RuntimeException("Controller not found in route URI {$routeURI}"); |
168 | 168 | } |
169 | 169 | |
170 | - if(!method_exists($controller, $method)){ |
|
170 | + if (!method_exists($controller, $method)) { |
|
171 | 171 | throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}"); |
172 | 172 | } |
173 | 173 |