@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -class Router{ |
|
7 | +class Router { |
|
8 | 8 | use Helper, CheckTrait, CheckWhere, DefinitionsTrait, ExtraJobsTrait; |
9 | 9 | |
10 | 10 | private static $instance = null; |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | |
26 | 26 | public static function create(): Router |
27 | 27 | { |
28 | - if(!self::getInstance()->instanced){ |
|
28 | + if (!self::getInstance()->instanced) { |
|
29 | 29 | self::getInstance()->instanced = true; |
30 | 30 | } |
31 | 31 | |
@@ -38,14 +38,14 @@ discard block |
||
38 | 38 | return self::$instance; |
39 | 39 | } |
40 | 40 | |
41 | - public function set($url ,$walking , string $protocol): Router |
|
41 | + public function set($url, $walking, string $protocol): Router |
|
42 | 42 | { |
43 | 43 | $this->lastReturn = null; |
44 | 44 | |
45 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
45 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
46 | 46 | |
47 | - foreach($this->routers as $key => $value){ |
|
48 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
47 | + foreach ($this->routers as $key => $value) { |
|
48 | + if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) { |
|
49 | 49 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
50 | 50 | } |
51 | 51 | } |
@@ -70,9 +70,9 @@ discard block |
||
70 | 70 | return self::getInstance(); |
71 | 71 | } |
72 | 72 | |
73 | - public static function group(string $prefix,$callback): Router |
|
73 | + public static function group(string $prefix, $callback): Router |
|
74 | 74 | { |
75 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
75 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
76 | 76 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
77 | 77 | $callback(); |
78 | 78 | self::getInstance()->group = null; |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | |
84 | 84 | public static function where(): Router |
85 | 85 | { |
86 | - if(self::getInstance()->lastReturn){ |
|
86 | + if (self::getInstance()->lastReturn) { |
|
87 | 87 | throw new Exception("It is not possible to define parameter tests for groups of routes."); |
88 | 88 | } |
89 | 89 | |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | |
95 | 95 | public static function name(string $name): Router |
96 | 96 | { |
97 | - if(self::getInstance()->lastReturn){ |
|
97 | + if (self::getInstance()->lastReturn) { |
|
98 | 98 | throw new Exception("There is no reason to call a {$name} route group."); |
99 | 99 | } |
100 | 100 | |
@@ -102,11 +102,11 @@ discard block |
||
102 | 102 | |
103 | 103 | self::getInstance()->hasRouteName($name); |
104 | 104 | |
105 | - if(in_array($name,self::getInstance()->beforeExcepts)){ |
|
105 | + if (in_array($name, self::getInstance()->beforeExcepts)) { |
|
106 | 106 | $currentRoute['beforeAll'] = null; |
107 | 107 | } |
108 | 108 | |
109 | - if(in_array($name,self::getInstance()->afterExcepts)){ |
|
109 | + if (in_array($name, self::getInstance()->afterExcepts)) { |
|
110 | 110 | $currentRoute['afterAll'] = null; |
111 | 111 | } |
112 | 112 | |
@@ -119,15 +119,15 @@ discard block |
||
119 | 119 | |
120 | 120 | private function byName(?string $routName) |
121 | 121 | { |
122 | - if(!is_null($routName)){ |
|
122 | + if (!is_null($routName)) { |
|
123 | 123 | $currentProtocol = $this->getProtocol(); |
124 | 124 | |
125 | 125 | $this->checkName($routName); |
126 | 126 | |
127 | 127 | $route = $this->routers[$routName]; |
128 | 128 | |
129 | - if(!$this->checkProtocol($route['protocol'], $currentProtocol)){ |
|
130 | - throw new Exception('Page not found.',404); |
|
129 | + if (!$this->checkProtocol($route['protocol'], $currentProtocol)) { |
|
130 | + throw new Exception('Page not found.', 404); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | $this->checkFiltering($route); |
@@ -145,11 +145,11 @@ discard block |
||
145 | 145 | |
146 | 146 | $currentProtocol = $instance->getInstance()->getProtocol(); |
147 | 147 | |
148 | - foreach(array_reverse($instance->getInstance()->routers) as $r => $route){ |
|
148 | + foreach (array_reverse($instance->getInstance()->routers) as $r => $route) { |
|
149 | 149 | |
150 | 150 | $instance->getInstance()->currentRoute = $route; |
151 | 151 | |
152 | - if(!$instance->getInstance()->checkProtocol($route['protocol'], $currentProtocol)){ |
|
152 | + if (!$instance->getInstance()->checkProtocol($route['protocol'], $currentProtocol)) { |
|
153 | 153 | continue; |
154 | 154 | } |
155 | 155 | |
@@ -159,11 +159,11 @@ discard block |
||
159 | 159 | |
160 | 160 | |
161 | 161 | $routs = $instance->getInstance()->explodeRoutes( |
162 | - (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url'], |
|
163 | - (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI'] |
|
162 | + (substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url'], |
|
163 | + (substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI'] |
|
164 | 164 | ); |
165 | 165 | |
166 | - if(!$instance->getInstance()->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])){ |
|
166 | + if (!$instance->getInstance()->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])) { |
|
167 | 167 | continue; |
168 | 168 | } |
169 | 169 | |
@@ -176,18 +176,18 @@ discard block |
||
176 | 176 | |
177 | 177 | $instance->getInstance()->currentRoute = null; |
178 | 178 | |
179 | - throw new Exception('Page not found.',404); |
|
179 | + throw new Exception('Page not found.', 404); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | public function filter($filters): Router |
183 | 183 | { |
184 | - if($this->lastReturn !== null){ |
|
184 | + if ($this->lastReturn !== null) { |
|
185 | 185 | $currentGroup = end($this->routers)['group']; |
186 | 186 | |
187 | 187 | foreach ($this->routers as $key => $value) { |
188 | 188 | |
189 | - if($value['group'] === $currentGroup){ |
|
190 | - $currentRoute = $this->addFilter($this->routers[$key],$filters); |
|
189 | + if ($value['group'] === $currentGroup) { |
|
190 | + $currentRoute = $this->addFilter($this->routers[$key], $filters); |
|
191 | 191 | $this->routers[$key] = $currentRoute; |
192 | 192 | } |
193 | 193 | |
@@ -196,24 +196,24 @@ discard block |
||
196 | 196 | return $this; |
197 | 197 | } |
198 | 198 | |
199 | - $this->routers[count($this->routers)-1] = $this->addFilter(end($this->routers),$filters); |
|
199 | + $this->routers[count($this->routers)-1] = $this->addFilter(end($this->routers), $filters); |
|
200 | 200 | return $this; |
201 | 201 | } |
202 | 202 | |
203 | 203 | public static function addFilter(array $route, $filter): array |
204 | 204 | { |
205 | - if(is_null($route['filters'])){ |
|
205 | + if (is_null($route['filters'])) { |
|
206 | 206 | $route['filters'] = $filter; |
207 | 207 | return $route; |
208 | 208 | } |
209 | 209 | |
210 | 210 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
211 | 211 | |
212 | - if(is_array($route['filters'])){ |
|
212 | + if (is_array($route['filters'])) { |
|
213 | 213 | foreach ($route['filters'] as $key => $value) { |
214 | 214 | $filters[] = $value; |
215 | 215 | } |
216 | - }else{ |
|
216 | + }else { |
|
217 | 217 | $filters[] = $route['filters']; |
218 | 218 | } |
219 | 219 |
@@ -4,28 +4,28 @@ |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -trait ControllerTrait{ |
|
7 | +trait ControllerTrait { |
|
8 | 8 | |
9 | 9 | protected function checkControllsettable(string $controll) |
10 | 10 | { |
11 | - if(count(explode(':',$controll)) != 2){ |
|
11 | + if (count(explode(':', $controll)) != 2) { |
|
12 | 12 | throw new Exception("Controller {$controll} badly set."); |
13 | 13 | } |
14 | 14 | } |
15 | 15 | |
16 | 16 | protected function checkControllexist(string $controll) |
17 | 17 | { |
18 | - $controllname = ucfirst(explode(':',$controll)[0]); |
|
19 | - if(!class_exists($controllname)){ |
|
18 | + $controllname = ucfirst(explode(':', $controll)[0]); |
|
19 | + if (!class_exists($controllname)) { |
|
20 | 20 | throw new Exception("No controller {$controllname} found."); |
21 | 21 | } |
22 | 22 | } |
23 | 23 | |
24 | 24 | protected function checkControllmethod(string $controll) |
25 | 25 | { |
26 | - $controllname = ucfirst(explode(':',$controll)[0]); |
|
27 | - $method = explode(':',$controll)[1]; |
|
28 | - if(!method_exists($controllname, $method)){ |
|
26 | + $controllname = ucfirst(explode(':', $controll)[0]); |
|
27 | + $method = explode(':', $controll)[1]; |
|
28 | + if (!method_exists($controllname, $method)) { |
|
29 | 29 | throw new Exception("No method {$method} found for {$controllname}."); |
30 | 30 | } |
31 | 31 | } |
@@ -3,21 +3,21 @@ |
||
3 | 3 | use HnrAzevedo\Router\Router; |
4 | 4 | |
5 | 5 | /* Returning parameters passed via URL in anonymous functions */ |
6 | -Router::get('/{parameter}/{otherparameter}', function($parameter, $otherparameter){ |
|
6 | +Router::get('/{parameter}/{otherparameter}', function($parameter, $otherparameter) { |
|
7 | 7 | echo "Parameter 1:{$parameter}, Parameter 2:{$otherparameter}."; |
8 | 8 | }); |
9 | 9 | |
10 | 10 | /* Passing controller and/or method via parameter in URL */ |
11 | -Router::get('/{controller}/{method}','{controller}:{method}'); |
|
12 | -Router::get('/{controller}/{method}','{controller}:method'); |
|
11 | +Router::get('/{controller}/{method}', '{controller}:{method}'); |
|
12 | +Router::get('/{controller}/{method}', '{controller}:method'); |
|
13 | 13 | |
14 | 14 | /* Passing value via parameter */ |
15 | -Router::get('/my-account/{teste}','Controller\\User:my_account'); |
|
15 | +Router::get('/my-account/{teste}', 'Controller\\User:my_account'); |
|
16 | 16 | |
17 | 17 | /* Filter example */ |
18 | -Router::get('/my-account','Controller\\User:my_account')->filter('Filter\\User:user_in'); |
|
18 | +Router::get('/my-account', 'Controller\\User:my_account')->filter('Filter\\User:user_in'); |
|
19 | 19 | |
20 | 20 | /* Accessed by all protocols */ |
21 | -Router::any('/',function(){ |
|
21 | +Router::any('/', function() { |
|
22 | 22 | // |
23 | 23 | }); |