@@ -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 |
@@ -64,9 +64,9 @@ discard block |
||
64 | 64 | } |
65 | 65 | |
66 | 66 | $routes[$index] = [ |
67 | - 'uri' => new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri), |
|
68 | - 'action' => $closure, |
|
69 | - 'method' => strtoupper($method), |
|
67 | + 'uri' => new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri), |
|
68 | + 'action' => $closure, |
|
69 | + 'method' => strtoupper($method), |
|
70 | 70 | 'middlewares' => null, |
71 | 71 | 'where' => null, |
72 | 72 | 'before' => [], |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | private static function checkDuplicity(string $uri, string $method): void |
84 | 84 | { |
85 | 85 | foreach(self::getInstance()->getRoutes() as $route){ |
86 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
86 | + if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
87 | 87 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
88 | 88 | } |
89 | 89 | } |
@@ -10,32 +10,32 @@ discard block |
||
10 | 10 | |
11 | 11 | public static function get(string $uri, $closure): RouterInterface |
12 | 12 | { |
13 | - return self::set('get',$uri,$closure); |
|
13 | + return self::set('get', $uri, $closure); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | public static function post(string $uri, $closure): RouterInterface |
17 | 17 | { |
18 | - return self::set('post',$uri,$closure); |
|
18 | + return self::set('post', $uri, $closure); |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | public static function ajax(string $uri, $closure): RouterInterface |
22 | 22 | { |
23 | - return self::set('ajax',$uri,$closure); |
|
23 | + return self::set('ajax', $uri, $closure); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | public static function delete(string $uri, $closure): RouterInterface |
27 | 27 | { |
28 | - return self::set('delete',$uri,$closure); |
|
28 | + return self::set('delete', $uri, $closure); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | public static function put(string $uri, $closure): RouterInterface |
32 | 32 | { |
33 | - return self::set('put',$uri,$closure); |
|
33 | + return self::set('put', $uri, $closure); |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | public static function patch(string $uri, $closure): RouterInterface |
37 | 37 | { |
38 | - return self::set('patch',$uri,$closure); |
|
38 | + return self::set('patch', $uri, $closure); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | public static function match(string $method, string $uri, $closure): RouterInterface |
@@ -46,20 +46,20 @@ discard block |
||
46 | 46 | |
47 | 47 | public static function any(string $uri, $closure): RouterInterface |
48 | 48 | { |
49 | - return self::set('*',$uri,$closure); |
|
49 | + return self::set('*', $uri, $closure); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | private static function set(string $method, string $uri, $closure): RouterInterface |
53 | 53 | { |
54 | - $uri = (substr($uri,0,1) !=='/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
54 | + $uri = (substr($uri, 0, 1) !== '/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
55 | 55 | |
56 | - self::checkDuplicity($uri,$method); |
|
56 | + self::checkDuplicity($uri, $method); |
|
57 | 57 | |
58 | 58 | $routes = self::getInstance()->getRoutes(); |
59 | 59 | |
60 | 60 | $index = sha1(date('d/m/Y H:i:s').count($routes)); |
61 | 61 | |
62 | - while(array_key_exists($index,$routes)){ |
|
62 | + while (array_key_exists($index, $routes)) { |
|
63 | 63 | $index = sha1(date('d/m/Y H:i:s').rand(count($routes))); |
64 | 64 | } |
65 | 65 | |
@@ -82,8 +82,8 @@ discard block |
||
82 | 82 | |
83 | 83 | private static function checkDuplicity(string $uri, string $method): void |
84 | 84 | { |
85 | - foreach(self::getInstance()->getRoutes() as $route){ |
|
86 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
85 | + foreach (self::getInstance()->getRoutes() as $route) { |
|
86 | + if (md5($route['uri'].$route['method']) === md5($uri.$method)) { |
|
87 | 87 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
88 | 88 | } |
89 | 89 | } |
@@ -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()->getRoutes())); |
|
14 | + $route['where'] = (is_array($route['where'])) ? array_merge($route['where'], $wheres) : $wheres; |
|
15 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
16 | 16 | return self::getInstance(); |
17 | 17 | } |
18 | 18 | |
@@ -22,38 +22,38 @@ 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 | - $this->checkValueRequire($ref,$value); |
|
54 | + $this->checkValueRequire($ref, $value); |
|
55 | 55 | |
56 | - if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){ |
|
56 | + if (array_key_exists(str_replace(['{:', '{', '}'], '', $ref), $where)) { |
|
57 | 57 | $this->matchParam($where, $ref, $value); |
58 | 58 | } |
59 | 59 | |
@@ -64,25 +64,25 @@ discard block |
||
64 | 64 | |
65 | 65 | private function checkValueRequire(string $ref, string $value): void |
66 | 66 | { |
67 | - if(substr($ref,0,2) !== '{:' && strlen($value) === 0){ |
|
67 | + if (substr($ref, 0, 2) !== '{:' && strlen($value) === 0) { |
|
68 | 68 | throw new \Exception('continue'); |
69 | 69 | } |
70 | 70 | } |
71 | 71 | |
72 | 72 | private function checkCount(string $routePath, string $uriPath): void |
73 | 73 | { |
74 | - $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); |
|
75 | - $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); |
|
74 | + $countRequest = substr_count($uriPath, '/')-substr_count($routePath, '{:'); |
|
75 | + $countRoute = substr_count($routePath, '/')-substr_count($routePath, '{:'); |
|
76 | 76 | |
77 | - if($countRequest !== $countRoute){ |
|
77 | + if ($countRequest !== $countRoute) { |
|
78 | 78 | throw new \Exception('continue'); |
79 | 79 | } |
80 | 80 | } |
81 | 81 | |
82 | 82 | private function matchParam(array $where, string $ref, string $value): void |
83 | 83 | { |
84 | - if(substr($ref,0,2) === '{' || $value !== ''){ |
|
85 | - if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){ |
|
84 | + if (substr($ref, 0, 2) === '{' || $value !== '') { |
|
85 | + if (!preg_match("/^{$where[str_replace(['{:', '{', '}'], '', $ref)]}$/", $value)) { |
|
86 | 86 | throw new \Exception('continue'); |
87 | 87 | } |
88 | 88 | } |
@@ -2,7 +2,7 @@ |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Router\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 | echo $teste.'-'.$teste2; |
30 | 30 | })->where([ |
31 | 31 | 'teste'=>'[a-zA-Z]{1,10}', |
@@ -33,14 +33,14 @@ discard block |
||
33 | 33 | ]); |
34 | 34 | |
35 | 35 | |
36 | -Router::get('/6my-accounttttt/{param1}/{param2}','HnrAzevedo\Router\Example\Controllers\User@requireLogin'); |
|
36 | +Router::get('/6my-accounttttt/{param1}/{param2}', 'HnrAzevedo\Router\Example\Controllers\User@requireLogin'); |
|
37 | 37 | //->middleware(['\Example\Middleware\Auth::class','Lasted']); |
38 | 38 | |
39 | -Router::get('/7my-account/{:teste}','Controller\\User:my_account')->where([ |
|
39 | +Router::get('/7my-account/{:teste}', 'Controller\\User:my_account')->where([ |
|
40 | 40 | 'teste'=>'[a-zA-Z]{1,10}' |
41 | 41 | ]); |
42 | 42 | |
43 | 43 | |
44 | -Router::any('/8',function(){ |
|
44 | +Router::any('/8', function() { |
|
45 | 45 | // |
46 | 46 | })->name('index'); |