@@ -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 | |
@@ -23,8 +23,8 @@ discard block |
||
23 | 23 | $excepts = (is_array($excepts)) ? $excepts : []; |
24 | 24 | |
25 | 25 | $group = self::getInstance()->inSave()['group']; |
26 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
27 | - if($route['group'] !== $group || in_array($route['name'], $excepts)){ |
|
26 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
27 | + if ($route['group'] !== $group || in_array($route['name'], $excepts)) { |
|
28 | 28 | continue; |
29 | 29 | } |
30 | 30 | |
@@ -49,30 +49,30 @@ discard block |
||
49 | 49 | unset($uriPath[0]); |
50 | 50 | |
51 | 51 | $corretRoute = true; |
52 | - foreach ($routePath as $r => $routeFrag){ |
|
52 | + foreach ($routePath as $r => $routeFrag) { |
|
53 | 53 | $where = is_array($route['where']) ? $route['where'] : []; |
54 | 54 | $routeFrag = $this->replaceParam($where, $routeFrag, $uriPath[$r]); |
55 | 55 | |
56 | - if($routeFrag !== $uriPath[$r]){ |
|
56 | + if ($routeFrag !== $uriPath[$r]) { |
|
57 | 57 | $corretRoute = false; |
58 | 58 | } |
59 | 59 | } |
60 | 60 | |
61 | - if(!$corretRoute){ |
|
61 | + if (!$corretRoute) { |
|
62 | 62 | throw new \Exception('continue'); |
63 | 63 | } |
64 | 64 | |
65 | - $_REQUEST = array_merge($_REQUEST,$this->parameters); |
|
65 | + $_REQUEST = array_merge($_REQUEST, $this->parameters); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | private function replaceParam(array $where, string $ref, string $value): string |
69 | 69 | { |
70 | - if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) { |
|
71 | - $this->parameters[str_replace(['{:','{','}'],'',$ref)] = $value; |
|
70 | + if (((substr($ref, 0, 1) === '{') && (substr($ref, strlen($ref)-1) === '}'))) { |
|
71 | + $this->parameters[str_replace(['{:', '{', '}'], '', $ref)] = $value; |
|
72 | 72 | |
73 | - $this->checkValueRequire($ref,$value); |
|
73 | + $this->checkValueRequire($ref, $value); |
|
74 | 74 | |
75 | - if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){ |
|
75 | + if (array_key_exists(str_replace(['{:', '{', '}'], '', $ref), $where)) { |
|
76 | 76 | $this->matchParam($where, $ref, $value); |
77 | 77 | } |
78 | 78 | |
@@ -83,25 +83,25 @@ discard block |
||
83 | 83 | |
84 | 84 | private function checkValueRequire(string $ref, string $value): void |
85 | 85 | { |
86 | - if(substr($ref,0,2) !== '{:' && strlen($value) === 0){ |
|
86 | + if (substr($ref, 0, 2) !== '{:' && strlen($value) === 0) { |
|
87 | 87 | throw new \Exception('continue'); |
88 | 88 | } |
89 | 89 | } |
90 | 90 | |
91 | 91 | private function checkCount(string $routePath, string $uriPath): void |
92 | 92 | { |
93 | - $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); |
|
94 | - $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); |
|
93 | + $countRequest = substr_count($uriPath, '/')-substr_count($routePath, '{:'); |
|
94 | + $countRoute = substr_count($routePath, '/')-substr_count($routePath, '{:'); |
|
95 | 95 | |
96 | - if($countRequest !== $countRoute){ |
|
96 | + if ($countRequest !== $countRoute) { |
|
97 | 97 | throw new \Exception('continue'); |
98 | 98 | } |
99 | 99 | } |
100 | 100 | |
101 | 101 | private function matchParam(array $where, string $ref, string $value): void |
102 | 102 | { |
103 | - if(substr($ref,0,2) === '{' || $value !== ''){ |
|
104 | - if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){ |
|
103 | + if (substr($ref, 0, 2) === '{' || $value !== '') { |
|
104 | + if (!preg_match("/^{$where[str_replace(['{:', '{', '}'], '', $ref)]}$/", $value)) { |
|
105 | 105 | throw new \Exception('continue'); |
106 | 106 | } |
107 | 107 | } |
@@ -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,7 +12,7 @@ discard block |
||
12 | 12 | |
13 | 13 | protected function sorted(?bool $sorted = null): bool |
14 | 14 | { |
15 | - if(null !== $sorted){ |
|
15 | + if (null !== $sorted) { |
|
16 | 16 | $this->sorted = $sorted; |
17 | 17 | } |
18 | 18 | return $this->sorted; |
@@ -20,35 +20,35 @@ discard block |
||
20 | 20 | |
21 | 21 | protected function sortRoutes(): bool |
22 | 22 | { |
23 | - if($this->sorted()){ |
|
23 | + if ($this->sorted()) { |
|
24 | 24 | return true; |
25 | 25 | } |
26 | 26 | |
27 | 27 | $staticRoutes = []; |
28 | 28 | $paramRoutes = []; |
29 | 29 | |
30 | - foreach($this->getRoutes() as $r => $route){ |
|
30 | + foreach ($this->getRoutes() as $r => $route) { |
|
31 | 31 | |
32 | 32 | $path = urldecode(unserialize($route['uri'])->getPath()); |
33 | 33 | |
34 | - if(strstr($path,'{')){ |
|
35 | - $paramRoutes[$this->getKeyArray(substr_count($path,'/') + substr_count($path,'{'),$paramRoutes)] = $route; |
|
34 | + if (strstr($path, '{')) { |
|
35 | + $paramRoutes[$this->getKeyArray(substr_count($path, '/')+substr_count($path, '{'), $paramRoutes)] = $route; |
|
36 | 36 | continue; |
37 | 37 | } |
38 | 38 | |
39 | - $staticRoutes[$this->getKeyArray(substr_count($path,'/'),$staticRoutes)] = $route; |
|
39 | + $staticRoutes[$this->getKeyArray(substr_count($path, '/'), $staticRoutes)] = $route; |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | rsort($paramRoutes); |
43 | 43 | rsort($staticRoutes); |
44 | 44 | |
45 | - $this->orderRoutes(array_merge($staticRoutes,$paramRoutes)); |
|
45 | + $this->orderRoutes(array_merge($staticRoutes, $paramRoutes)); |
|
46 | 46 | return $this->sorted(true); |
47 | 47 | } |
48 | 48 | |
49 | 49 | private function getKeyArray(int $index, array $array): int |
50 | 50 | { |
51 | - while(array_key_exists($index,$array)){ |
|
51 | + while (array_key_exists($index, $array)) { |
|
52 | 52 | $index++; |
53 | 53 | } |
54 | 54 | return $index; |
@@ -57,8 +57,8 @@ discard block |
||
57 | 57 | private function orderRoutes(array $routes):void |
58 | 58 | { |
59 | 59 | $kRoutes = $routes; |
60 | - foreach($routes as $r => $route){ |
|
61 | - if(array_key_exists('name',$route)){ |
|
60 | + foreach ($routes as $r => $route) { |
|
61 | + if (array_key_exists('name', $route)) { |
|
62 | 62 | unset($kRoutes[$r]); |
63 | 63 | $kRoutes["'{$route['name']}'"] = $route; |
64 | 64 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | self::getInstance()->checkMethod($route, $requestMethod); |
71 | 71 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
72 | 72 | return self::getInstance(); |
73 | - }catch(\Exception $er){ |
|
73 | + } catch(\Exception $er){ |
|
74 | 74 | continue; |
75 | 75 | } |
76 | 76 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | try{ |
98 | 98 | $action = unserialize(self::getInstance()->current()['action']); |
99 | 99 | self::getInstance()->executeRouteAction( (is_string($action)) ? $action : $action->getClosure() ); |
100 | - }catch(\Exception $er){ |
|
100 | + } catch(\Exception $er){ |
|
101 | 101 | self::getInstance()->error = $er; |
102 | 102 | } |
103 | 103 |
@@ -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 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | { |
35 | 35 | $route = self::getInstance()->inSave(); |
36 | 36 | |
37 | - if(null === $value){ |
|
37 | + if (null === $value) { |
|
38 | 38 | return (isset($route['attributes'][$name])) ? $route['attributes'][$name] : null; |
39 | 39 | } |
40 | 40 | |
@@ -47,8 +47,8 @@ discard block |
||
47 | 47 | public static function groupAttribute(string $name, $value = null, ?array $excepts = []): Router |
48 | 48 | { |
49 | 49 | $group = self::getInstance()->inSave()['group']; |
50 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
51 | - if($route['group'] !== $group || in_array($route['name'], $excepts)){ |
|
50 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
51 | + if ($route['group'] !== $group || in_array($route['name'], $excepts)) { |
|
52 | 52 | continue; |
53 | 53 | } |
54 | 54 | |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | public static function group(string $prefix, \Closure $closure): Router |
62 | 62 | { |
63 | 63 | $id = sha1(date('d/m/Y h:m:i')); |
64 | - while(array_key_exists($id, self::getInstance()->groupsName)){ |
|
64 | + while (array_key_exists($id, self::getInstance()->groupsName)) { |
|
65 | 65 | $id = sha1(date('d/m/Y h:m:i').$id); |
66 | 66 | } |
67 | 67 | |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | { |
83 | 83 | self::getInstance()->loaded = true; |
84 | 84 | |
85 | - if(null !== $name){ |
|
85 | + if (null !== $name) { |
|
86 | 86 | return self::getInstance()->loadByName($name); |
87 | 87 | } |
88 | 88 | |
@@ -90,15 +90,15 @@ discard block |
||
90 | 90 | |
91 | 91 | $requestMethod = (isset($_REQUEST['REQUEST_METHOD'])) ? $_REQUEST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']; |
92 | 92 | |
93 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
93 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
94 | 94 | self::getInstance()->currentRoute = $route; |
95 | 95 | self::getInstance()->currentRoute['name'] = $r; |
96 | 96 | |
97 | - try{ |
|
97 | + try { |
|
98 | 98 | self::getInstance()->checkMethod($route, $requestMethod); |
99 | 99 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
100 | 100 | return self::getInstance(); |
101 | - }catch(\Exception $er){ |
|
101 | + }catch (\Exception $er) { |
|
102 | 102 | continue; |
103 | 103 | } |
104 | 104 | } |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | |
113 | 113 | public static function run(?string $name = null): RouterInterface |
114 | 114 | { |
115 | - if(!self::getInstance()->loaded){ |
|
115 | + if (!self::getInstance()->loaded) { |
|
116 | 116 | self::getInstance()->load($name); |
117 | 117 | } |
118 | 118 | |
@@ -122,10 +122,10 @@ discard block |
||
122 | 122 | |
123 | 123 | self::getInstance()->executeBefore(); |
124 | 124 | |
125 | - try{ |
|
125 | + try { |
|
126 | 126 | $action = unserialize(self::getInstance()->current()['action']); |
127 | - self::getInstance()->executeRouteAction( (is_string($action)) ? $action : $action->getClosure() ); |
|
128 | - }catch(\Exception $er){ |
|
127 | + self::getInstance()->executeRouteAction((is_string($action)) ? $action : $action->getClosure()); |
|
128 | + }catch (\Exception $er) { |
|
129 | 129 | self::getInstance()->error = $er; |
130 | 130 | } |
131 | 131 | |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | |
139 | 139 | private function checkError(): void |
140 | 140 | { |
141 | - if(isset($this->error)){ |
|
141 | + if (isset($this->error)) { |
|
142 | 142 | throw $this->error; |
143 | 143 | } |
144 | 144 | } |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | |
153 | 153 | public static function routes(?array $routes = null): array |
154 | 154 | { |
155 | - if(null !== $routes){ |
|
155 | + if (null !== $routes) { |
|
156 | 156 | self::getInstance()->setRoutes($routes); |
157 | 157 | self::getInstance()->sortRoutes(); |
158 | 158 | } |
@@ -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 | } |
@@ -41,8 +41,8 @@ discard block |
||
41 | 41 | public static function middleware(array $middlewares): RouterInterface |
42 | 42 | { |
43 | 43 | $route = self::getInstance()->inSave(); |
44 | - $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares; |
|
45 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
44 | + $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'], $middlewares) : $middlewares; |
|
45 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
46 | 46 | return self::getInstance(); |
47 | 47 | } |
48 | 48 | |
@@ -50,8 +50,8 @@ discard block |
||
50 | 50 | { |
51 | 51 | $excepts = (is_array($excepts)) ? $excepts : []; |
52 | 52 | $group = self::getInstance()->inSave()['group']; |
53 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
54 | - if($route['group'] !== $group || in_array($route['name'], $excepts)){ |
|
53 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
54 | + if ($route['group'] !== $group || in_array($route['name'], $excepts)) { |
|
55 | 55 | continue; |
56 | 56 | } |
57 | 57 | |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | |
64 | 64 | private static function existMiddleware(string $name): void |
65 | 65 | { |
66 | - if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){ |
|
66 | + if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) { |
|
67 | 67 | throw new \RuntimeException("Middleware {$name} does not exist"); |
68 | 68 | } |
69 | 69 | } |
@@ -72,9 +72,9 @@ discard block |
||
72 | 72 | { |
73 | 73 | $factory = new Factory(); |
74 | 74 | |
75 | - $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], unserialize($this->current()['uri']),['route' => $this->current()]) : $this->serverRequest; |
|
75 | + $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], unserialize($this->current()['uri']), ['route' => $this->current()]) : $this->serverRequest; |
|
76 | 76 | |
77 | - foreach ($this->current()['middlewares'] as $middleware){ |
|
77 | + foreach ($this->current()['middlewares'] as $middleware) { |
|
78 | 78 | $this->currentMiddlewares[] = (class_exists($middleware)) ? new $middleware() : new $this->globalMiddlewares[$middleware](); |
79 | 79 | } |
80 | 80 | |
@@ -89,20 +89,20 @@ discard block |
||
89 | 89 | |
90 | 90 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
91 | 91 | { |
92 | - if(!$this->getInstance()->loaded()){ |
|
92 | + if (!$this->getInstance()->loaded()) { |
|
93 | 93 | $this->getInstance()->load(); |
94 | 94 | } |
95 | 95 | |
96 | 96 | $route = $this->getInstance()->current(); |
97 | - if(!empty($route)){ |
|
98 | - $route['action'] = function(){ |
|
97 | + if (!empty($route)) { |
|
98 | + $route['action'] = function() { |
|
99 | 99 | $this->getInstance()->executeBefore(); |
100 | 100 | $this->getInstance()->executeRouteAction($this->getInstance()->current()['action']); |
101 | 101 | $this->getInstance()->executeAfter(); |
102 | 102 | }; |
103 | 103 | } |
104 | 104 | |
105 | - return $this->next($handler)->handle($request->withAttribute('route',$route)); |
|
105 | + return $this->next($handler)->handle($request->withAttribute('route', $route)); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | private function next(RequestHandlerInterface $defaultHandler): RequestHandlerInterface |
@@ -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' => [], |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | private static function checkDuplicity(string $uri, string $method): void |
90 | 90 | { |
91 | 91 | foreach(self::getInstance()->getRoutes() as $route){ |
92 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
92 | + if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
93 | 93 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
94 | 94 | } |
95 | 95 | } |
@@ -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 | |
@@ -88,8 +88,8 @@ discard block |
||
88 | 88 | |
89 | 89 | private static function checkDuplicity(string $uri, string $method): void |
90 | 90 | { |
91 | - foreach(self::getInstance()->getRoutes() as $route){ |
|
92 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
91 | + foreach (self::getInstance()->getRoutes() as $route) { |
|
92 | + if (md5($route['uri'].$route['method']) === md5($uri.$method)) { |
|
93 | 93 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
94 | 94 | } |
95 | 95 | } |
@@ -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 | } |
@@ -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, array_values($_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], array_values($_REQUEST)); |
|
162 | + call_user_func_array([(new $controller()), $method], array_values($_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 |