@@ -61,25 +61,25 @@ discard block |
||
61 | 61 | |
62 | 62 | public function set($url ,$walking , string $protocol): Router |
63 | 63 | { |
64 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
64 | + $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
65 | 65 | |
66 | - foreach($this->routers as $key => $value){ |
|
67 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
66 | + foreach($this->routers as $key => $value){ |
|
67 | + if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
68 | 68 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
69 | 69 | } |
70 | 70 | } |
71 | 71 | |
72 | 72 | $this->check_typeRole($walking); |
73 | 73 | |
74 | - $route = [ |
|
75 | - 'url' => $this->prefix.$url, |
|
76 | - 'role' => $walking, |
|
77 | - 'protocol' => $protocol, |
|
78 | - 'filters' => null, |
|
74 | + $route = [ |
|
75 | + 'url' => $this->prefix.$url, |
|
76 | + 'role' => $walking, |
|
77 | + 'protocol' => $protocol, |
|
78 | + 'filters' => null, |
|
79 | 79 | 'group' => self::getInstance()->group |
80 | - ]; |
|
80 | + ]; |
|
81 | 81 | |
82 | - $this->routers[] = $route; |
|
82 | + $this->routers[] = $route; |
|
83 | 83 | |
84 | 84 | return self::getInstance(); |
85 | 85 | } |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | { |
141 | 141 | $this->byName($route_name); |
142 | 142 | |
143 | - $currentProtocol = $this->getProtocol(); |
|
143 | + $currentProtocol = $this->getProtocol(); |
|
144 | 144 | |
145 | 145 | foreach(array_reverse($this->routers) as $r => $route){ |
146 | 146 | |
@@ -156,19 +156,19 @@ discard block |
||
156 | 156 | |
157 | 157 | $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
158 | 158 | |
159 | - if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){ |
|
159 | + if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){ |
|
160 | 160 | continue; |
161 | 161 | } |
162 | 162 | |
163 | 163 | $this->check_filtering($route); |
164 | 164 | |
165 | 165 | $this->toHiking($route['role']); |
166 | - return true; |
|
166 | + return true; |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | unset($_SESSION['route']); |
170 | 170 | |
171 | - throw new Exception('Page not found.',404); |
|
171 | + throw new Exception('Page not found.',404); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | public static function filter($filters): Router |
@@ -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; |
9 | 9 | |
10 | 10 | private static $instance = null; |
@@ -59,12 +59,12 @@ discard block |
||
59 | 59 | return self::getInstance()->set($uri, $walking, $protocol); |
60 | 60 | } |
61 | 61 | |
62 | - public function set($url ,$walking , string $protocol): Router |
|
62 | + public function set($url, $walking, string $protocol): Router |
|
63 | 63 | { |
64 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
64 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
65 | 65 | |
66 | - foreach($this->routers as $key => $value){ |
|
67 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
66 | + foreach ($this->routers as $key => $value) { |
|
67 | + if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) { |
|
68 | 68 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
69 | 69 | } |
70 | 70 | } |
@@ -84,9 +84,9 @@ discard block |
||
84 | 84 | return self::getInstance(); |
85 | 85 | } |
86 | 86 | |
87 | - public static function group(string $prefix,$callback): Router |
|
87 | + public static function group(string $prefix, $callback): Router |
|
88 | 88 | { |
89 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
89 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
90 | 90 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
91 | 91 | $callback(); |
92 | 92 | self::getInstance()->group = null; |
@@ -97,14 +97,14 @@ discard block |
||
97 | 97 | |
98 | 98 | public static function name(string $name): Router |
99 | 99 | { |
100 | - if(self::getInstance()->lastReturn){ |
|
100 | + if (self::getInstance()->lastReturn) { |
|
101 | 101 | throw new Exception("There is no reason to call a {$name} route group."); |
102 | 102 | } |
103 | 103 | |
104 | 104 | $currentRoute = end(self::getInstance()->routers); |
105 | 105 | |
106 | - foreach(self::getInstance()->routers as $key => $value){ |
|
107 | - if(array_key_exists($name, self::getInstance()->routers)){ |
|
106 | + foreach (self::getInstance()->routers as $key => $value) { |
|
107 | + if (array_key_exists($name, self::getInstance()->routers)) { |
|
108 | 108 | throw new Exception("There is already a route with the name {$name} configured."); |
109 | 109 | } |
110 | 110 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | |
121 | 121 | private function byName(?string $route_name) |
122 | 122 | { |
123 | - if(!is_null($route_name)){ |
|
123 | + if (!is_null($route_name)) { |
|
124 | 124 | $currentProtocol = $this->getProtocol(); |
125 | 125 | |
126 | 126 | $this->check_name($route_name); |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | |
143 | 143 | $currentProtocol = $this->getProtocol(); |
144 | 144 | |
145 | - foreach(array_reverse($this->routers) as $r => $route){ |
|
145 | + foreach (array_reverse($this->routers) as $r => $route) { |
|
146 | 146 | |
147 | 147 | $_SESSION['route'] = serialize($route); |
148 | 148 | |
@@ -150,13 +150,13 @@ discard block |
||
150 | 150 | |
151 | 151 | $this->hasProtocol($route, $currentProtocol); |
152 | 152 | |
153 | - $route_loop = $this->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']); |
|
153 | + $route_loop = $this->explodeRoute((substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url']); |
|
154 | 154 | |
155 | 155 | $_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : ''; |
156 | 156 | |
157 | - $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
|
157 | + $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']); |
|
158 | 158 | |
159 | - if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){ |
|
159 | + if ($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)) { |
|
160 | 160 | continue; |
161 | 161 | } |
162 | 162 | |
@@ -168,18 +168,18 @@ discard block |
||
168 | 168 | |
169 | 169 | unset($_SESSION['route']); |
170 | 170 | |
171 | - throw new Exception('Page not found.',404); |
|
171 | + throw new Exception('Page not found.', 404); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | public static function filter($filters): Router |
175 | 175 | { |
176 | - if(self::getInstance()->lastReturn !== null){ |
|
176 | + if (self::getInstance()->lastReturn !== null) { |
|
177 | 177 | $currentGroup = end(self::getInstance()->routers)['group']; |
178 | 178 | |
179 | 179 | foreach (self::getInstance()->routers as $key => $value) { |
180 | 180 | |
181 | - if($value['group'] === $currentGroup){ |
|
182 | - $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
|
181 | + if ($value['group'] === $currentGroup) { |
|
182 | + $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters); |
|
183 | 183 | self::getInstance()->routers[$key] = $currentRoute; |
184 | 184 | } |
185 | 185 | |
@@ -190,24 +190,24 @@ discard block |
||
190 | 190 | return self::getInstance(); |
191 | 191 | } |
192 | 192 | |
193 | - self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
|
193 | + self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters); |
|
194 | 194 | return self::getInstance(); |
195 | 195 | } |
196 | 196 | |
197 | 197 | public static function addFilter(array $route, $filter): array |
198 | 198 | { |
199 | - if(is_null($route['filters'])){ |
|
199 | + if (is_null($route['filters'])) { |
|
200 | 200 | $route['filters'] = $filter; |
201 | 201 | return $route; |
202 | 202 | } |
203 | 203 | |
204 | 204 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
205 | 205 | |
206 | - if(is_array($route['filters'])){ |
|
206 | + if (is_array($route['filters'])) { |
|
207 | 207 | foreach ($route['filters'] as $key => $value) { |
208 | 208 | $filters[] = $value; |
209 | 209 | } |
210 | - }else{ |
|
210 | + }else { |
|
211 | 211 | $filters[] = $route['filters']; |
212 | 212 | } |
213 | 213 |