@@ -2,7 +2,7 @@ |
||
2 | 2 | |
3 | 3 | namespace Example\Controllers; |
4 | 4 | |
5 | -class User{ |
|
5 | +class User { |
|
6 | 6 | |
7 | 7 | public function requireLogin($param, $param2): void |
8 | 8 | { |
@@ -4,28 +4,28 @@ discard block |
||
4 | 4 | |
5 | 5 | |
6 | 6 | |
7 | -Router::beforeAll(function(){ |
|
7 | +Router::beforeAll(function() { |
|
8 | 8 | echo '<br><b>beforeAll</b><br>'; |
9 | 9 | },['testes']); |
10 | 10 | |
11 | -Router::afterAll(function(){ |
|
11 | +Router::afterAll(function() { |
|
12 | 12 | echo '<br><b>afterAll</b><br>'; |
13 | 13 | },['testes']); |
14 | 14 | |
15 | 15 | |
16 | -Router::match('GET|POST|AJAX','/{1parameter}/{otherparameter}', function($parameter, $otherparameter){ |
|
16 | +Router::match('GET|POST|AJAX', '/{1parameter}/{otherparameter}', function($parameter, $otherparameter) { |
|
17 | 17 | echo "Parameter 1:{$parameter}, Parameter 2:{$otherparameter}."; |
18 | 18 | }); |
19 | 19 | |
20 | -Router::get('/{2controller}/{method}','{controller}:{method}'); |
|
20 | +Router::get('/{2controller}/{method}', '{controller}:{method}'); |
|
21 | 21 | |
22 | -Router::get('/3my-account','Controller\\User:my_account')->before(function(){ |
|
22 | +Router::get('/3my-account', 'Controller\\User:my_account')->before(function() { |
|
23 | 23 | echo '1'; |
24 | 24 | }); |
25 | 25 | |
26 | -Router::get('/4my-account/teste/teste','Controller\\User:my_account')->name('2'); |
|
26 | +Router::get('/4my-account/teste/teste', 'Controller\\User:my_account')->name('2'); |
|
27 | 27 | |
28 | -Router::get('/5my-account/{:teste}/{teste2}',function($teste, $teste2){ |
|
28 | +Router::get('/5my-account/{:teste}/{teste2}', function($teste, $teste2) { |
|
29 | 29 | var_dump($teste); |
30 | 30 | var_dump($teste2); |
31 | 31 | })->where([ |
@@ -34,14 +34,14 @@ discard block |
||
34 | 34 | ]); |
35 | 35 | |
36 | 36 | |
37 | -Router::get('/6my-accounttttt/{param1}/{param2}','\Example\Controllers\User@requireLogin'); |
|
37 | +Router::get('/6my-accounttttt/{param1}/{param2}', '\Example\Controllers\User@requireLogin'); |
|
38 | 38 | //->middleware(['\Example\Middleware\Auth::class','Lasted']); |
39 | 39 | |
40 | -Router::get('/5my-account/{:teste}','Controller\\User:my_account')->where([ |
|
40 | +Router::get('/5my-account/{:teste}', 'Controller\\User:my_account')->where([ |
|
41 | 41 | 'teste'=>'[a-zA-Z]{1,10}' |
42 | 42 | ]); |
43 | 43 | |
44 | 44 | |
45 | -Router::any('/8',function(){ |
|
45 | +Router::any('/8', function() { |
|
46 | 46 | // |
47 | 47 | })->name('index'); |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Router; |
4 | 4 | |
5 | -trait PrioritizeTrait{ |
|
5 | +trait PrioritizeTrait { |
|
6 | 6 | |
7 | 7 | protected array $routes = []; |
8 | 8 | |
@@ -11,27 +11,27 @@ discard block |
||
11 | 11 | $staticRoutes = []; |
12 | 12 | $paramRoutes = []; |
13 | 13 | |
14 | - foreach($this->routes as $r => $route){ |
|
14 | + foreach ($this->routes 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 | } |
@@ -11,8 +11,8 @@ discard block |
||
11 | 11 | public static function where(array $wheres): Router |
12 | 12 | { |
13 | 13 | $route = self::getInstance()->inSave(); |
14 | - $route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$wheres) : $wheres; |
|
15 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->routes)); |
|
14 | + $route['where'] = (is_array($route['where'])) ? array_merge($route['where'], $wheres) : $wheres; |
|
15 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->routes)); |
|
16 | 16 | return self::getInstance(); |
17 | 17 | } |
18 | 18 | |
@@ -22,36 +22,36 @@ discard block |
||
22 | 22 | |
23 | 23 | $this->parameters = []; |
24 | 24 | |
25 | - $uriPath .= (substr($uriPath,strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
25 | + $uriPath .= (substr($uriPath, strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
26 | 26 | |
27 | - $routePath = explode('/',urldecode($route['uri']->getPath())); |
|
27 | + $routePath = explode('/', urldecode($route['uri']->getPath())); |
|
28 | 28 | unset($routePath[0]); |
29 | - $uriPath = explode('/',urldecode($uriPath)); |
|
29 | + $uriPath = explode('/', urldecode($uriPath)); |
|
30 | 30 | unset($uriPath[0]); |
31 | 31 | |
32 | 32 | $corretRoute = true; |
33 | - foreach($routePath as $r => $routeFrag){ |
|
33 | + foreach ($routePath as $r => $routeFrag) { |
|
34 | 34 | $where = is_array($route['where']) ? $route['where'] : []; |
35 | 35 | $routeFrag = $this->replaceParam($where, $routeFrag, $uriPath[$r]); |
36 | 36 | |
37 | - if($routeFrag !== $uriPath[$r]){ |
|
37 | + if ($routeFrag !== $uriPath[$r]) { |
|
38 | 38 | $corretRoute = false; |
39 | 39 | } |
40 | 40 | } |
41 | 41 | |
42 | - if(!$corretRoute){ |
|
42 | + if (!$corretRoute) { |
|
43 | 43 | throw new \Exception('continue'); |
44 | 44 | } |
45 | 45 | |
46 | - $_REQUEST = array_merge($_REQUEST,$this->parameters); |
|
46 | + $_REQUEST = array_merge($_REQUEST, $this->parameters); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | private function replaceParam(array $where, string $ref, string $value): string |
50 | 50 | { |
51 | - if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) { |
|
52 | - $this->parameters[str_replace(['{:','{','}'],'',$ref)] = $value; |
|
51 | + if (((substr($ref, 0, 1) === '{') && (substr($ref, strlen($ref)-1) === '}'))) { |
|
52 | + $this->parameters[str_replace(['{:', '{', '}'], '', $ref)] = $value; |
|
53 | 53 | |
54 | - if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){ |
|
54 | + if (array_key_exists(str_replace(['{:', '{', '}'], '', $ref), $where)) { |
|
55 | 55 | $this->matchParam($where, $ref, $value); |
56 | 56 | } |
57 | 57 | return $value; |
@@ -61,18 +61,18 @@ discard block |
||
61 | 61 | |
62 | 62 | private function checkCount(string $routePath, string $uriPath): void |
63 | 63 | { |
64 | - $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); |
|
65 | - $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); |
|
64 | + $countRequest = substr_count($uriPath, '/')-substr_count($routePath, '{:'); |
|
65 | + $countRoute = substr_count($routePath, '/')-substr_count($routePath, '{:'); |
|
66 | 66 | |
67 | - if($countRequest !== $countRoute){ |
|
67 | + if ($countRequest !== $countRoute) { |
|
68 | 68 | throw new \Exception('continue'); |
69 | 69 | } |
70 | 70 | } |
71 | 71 | |
72 | 72 | private function matchParam(array $where, string $ref, string $value): void |
73 | 73 | { |
74 | - if(substr($ref,0,2) === '{:' || $value !== ''){ |
|
75 | - if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){ |
|
74 | + if (substr($ref, 0, 2) === '{:' || $value !== '') { |
|
75 | + if (!preg_match("/^{$where[str_replace(['{:', '{', '}'], '', $ref)]}$/", $value)) { |
|
76 | 76 | throw new \Exception('continue'); |
77 | 77 | } |
78 | 78 | } |
@@ -13,24 +13,24 @@ discard block |
||
13 | 13 | |
14 | 14 | public static function before($closure): RouterInterface |
15 | 15 | { |
16 | - return self::addInRoute('before',$closure); |
|
16 | + return self::addInRoute('before', $closure); |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | public static function after($closure): RouterInterface |
20 | 20 | { |
21 | - return self::addInRoute('after',$closure); |
|
21 | + return self::addInRoute('after', $closure); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | public static function beforeAll($closure, $excepts): RouterInterface |
25 | 25 | { |
26 | - self::getInstance()->beforeExcepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
26 | + self::getInstance()->beforeExcepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
27 | 27 | self::getInstance()->beforeAll = $closure; |
28 | 28 | return self::getInstance(); |
29 | 29 | } |
30 | 30 | |
31 | 31 | public static function afterAll($closure, $excepts): RouterInterface |
32 | 32 | { |
33 | - self::getInstance()->afterExcepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
33 | + self::getInstance()->afterExcepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
34 | 34 | self::getInstance()->afterAll = $closure; |
35 | 35 | return self::getInstance(); |
36 | 36 | } |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | |
48 | 48 | protected function executeRouteAction(): bool |
49 | 49 | { |
50 | - if(is_callable($this->current()['action'])){ |
|
50 | + if (is_callable($this->current()['action'])) { |
|
51 | 51 | call_user_func_array($this->current()['action'], $_REQUEST); |
52 | 52 | return true; |
53 | 53 | } |
@@ -59,12 +59,12 @@ discard block |
||
59 | 59 | private static function addInRoutes(string $state, $closure, $excepts): RouterInterface |
60 | 60 | { |
61 | 61 | self::getInstance()->isInPseudGroup(); |
62 | - $excepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
62 | + $excepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
63 | 63 | $group = self::getInstance()->inSave()['group']; |
64 | 64 | |
65 | - foreach(self::getInstance()->routes as $r => $route){ |
|
66 | - if($route['group'] === $group && !in_array($r,$excepts)){ |
|
67 | - self::getInstance()->routes[$r][$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
65 | + foreach (self::getInstance()->routes as $r => $route) { |
|
66 | + if ($route['group'] === $group && !in_array($r, $excepts)) { |
|
67 | + self::getInstance()->routes[$r][$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
68 | 68 | } |
69 | 69 | } |
70 | 70 | |
@@ -74,14 +74,14 @@ discard block |
||
74 | 74 | private static function addInRoute(string $state, $closure): RouterInterface |
75 | 75 | { |
76 | 76 | $route = self::getInstance()->inSave(); |
77 | - $route[$state] = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
78 | - self::updateRoute($route,array_key_last(self::getInstance()->routes)); |
|
77 | + $route[$state] = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
78 | + self::updateRoute($route, array_key_last(self::getInstance()->routes)); |
|
79 | 79 | return self::getInstance(); |
80 | 80 | } |
81 | 81 | |
82 | 82 | protected function executeBefore(): void |
83 | 83 | { |
84 | - if(!in_array($this->currentName(),$this->beforeExcepts)){ |
|
84 | + if (!in_array($this->currentName(), $this->beforeExcepts)) { |
|
85 | 85 | ($this->beforeAll)(); |
86 | 86 | } |
87 | 87 | |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | |
91 | 91 | protected function executeAfter(): void |
92 | 92 | { |
93 | - if(!in_array($this->currentName(),$this->afterExcepts)){ |
|
93 | + if (!in_array($this->currentName(), $this->afterExcepts)) { |
|
94 | 94 | ($this->afterAll)(); |
95 | 95 | } |
96 | 96 | |
@@ -99,8 +99,8 @@ discard block |
||
99 | 99 | |
100 | 100 | private function executeState(string $stage): void |
101 | 101 | { |
102 | - foreach($this->current()[$stage] as $state){ |
|
103 | - if(is_callable($state)){ |
|
102 | + foreach ($this->current()[$stage] as $state) { |
|
103 | + if (is_callable($state)) { |
|
104 | 104 | $state(); |
105 | 105 | continue; |
106 | 106 | } |
@@ -111,30 +111,30 @@ discard block |
||
111 | 111 | |
112 | 112 | private function executeController(string $controllerMeth): void |
113 | 113 | { |
114 | - if(count(explode('@',$controllerMeth)) !== 2){ |
|
114 | + if (count(explode('@', $controllerMeth)) !== 2) { |
|
115 | 115 | throw new \RuntimeException('Misconfigured route action'); |
116 | 116 | } |
117 | 117 | |
118 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
119 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
118 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
119 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
120 | 120 | |
121 | 121 | $this->checkControllerMeth($controllerMeth); |
122 | 122 | |
123 | - call_user_func_array([(new $controller()),$method], $_REQUEST); |
|
123 | + call_user_func_array([(new $controller()), $method], $_REQUEST); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | private function checkControllerMeth(string $controllerMeth): void |
127 | 127 | { |
128 | - $routeURI = str_replace(['{:','{','}'],'',urldecode($this->current()['uri']->getPath())); |
|
128 | + $routeURI = str_replace(['{:', '{', '}'], '', urldecode($this->current()['uri']->getPath())); |
|
129 | 129 | |
130 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
131 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
130 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
131 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
132 | 132 | |
133 | - if(!class_exists($controller)){ |
|
133 | + if (!class_exists($controller)) { |
|
134 | 134 | throw new \RuntimeException("Controller not found in route URI {$routeURI}"); |
135 | 135 | } |
136 | 136 | |
137 | - if(!method_exists($controller, $method)){ |
|
137 | + if (!method_exists($controller, $method)) { |
|
138 | 138 | throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}"); |
139 | 139 | } |
140 | 140 |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | $route = self::getInstance()->inSave(); |
33 | 33 | $route['name'] = $name; |
34 | 34 | self::getInstance()->routesName[$name] = $name; |
35 | - self::getInstance()->unsetRoute(array_key_last(self::getInstance()->routes))->updateRoute($route,$name); |
|
35 | + self::getInstance()->unsetRoute(array_key_last(self::getInstance()->routes))->updateRoute($route, $name); |
|
36 | 36 | return self::getInstance(); |
37 | 37 | } |
38 | 38 | |
@@ -71,15 +71,15 @@ discard block |
||
71 | 71 | self::getInstance()->loaded = true; |
72 | 72 | self::getInstance()->sortRoutes(); |
73 | 73 | |
74 | - foreach(self::getInstance()->routes as $r => $route){ |
|
74 | + foreach (self::getInstance()->routes as $r => $route) { |
|
75 | 75 | self::getInstance()->currentRoute = $route; |
76 | 76 | self::getInstance()->currentRoute['name'] = $r; |
77 | 77 | |
78 | - try{ |
|
78 | + try { |
|
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 | } |
@@ -90,15 +90,15 @@ discard block |
||
90 | 90 | |
91 | 91 | public static function run(): RouterInterface |
92 | 92 | { |
93 | - if(!self::getInstance()->loaded){ |
|
93 | + if (!self::getInstance()->loaded) { |
|
94 | 94 | self::getInstance()->load(); |
95 | 95 | } |
96 | 96 | |
97 | 97 | self::getInstance()->executeBefore(); |
98 | 98 | |
99 | - try{ |
|
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 | |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | |
112 | 112 | private function checkError(): void |
113 | 113 | { |
114 | - if(isset($this->error)){ |
|
114 | + if (isset($this->error)) { |
|
115 | 115 | throw $this->error; |
116 | 116 | } |
117 | 117 | } |
@@ -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 |
@@ -57,10 +57,10 @@ discard block |
||
57 | 57 | |
58 | 58 | self::checkDuplicity($uri,$method); |
59 | 59 | |
60 | - self::getInstance()->routes[] = [ |
|
61 | - 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
|
62 | - 'action' => $closure, |
|
63 | - 'method' => strtoupper($method), |
|
60 | + self::getInstance()->routes[] = [ |
|
61 | + 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
|
62 | + 'action' => $closure, |
|
63 | + 'method' => strtoupper($method), |
|
64 | 64 | 'middlewares' => null, |
65 | 65 | 'where' => null, |
66 | 66 | 'before' => [], |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | private static function checkDuplicity(string $uri, string $method): void |
76 | 76 | { |
77 | 77 | foreach(self::getInstance()->routes as $route){ |
78 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
78 | + if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
79 | 79 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
80 | 80 | } |
81 | 81 | } |