Total Complexity | 54 |
Total Lines | 357 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 0 |
Complex classes like Router often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Router |
||
26 | { |
||
27 | /** |
||
28 | * Router::$uri |
||
29 | * |
||
30 | * @var Message\Uri |
||
31 | */ |
||
32 | protected $uri; |
||
33 | |||
34 | /** |
||
35 | * Router::$addresses |
||
36 | * |
||
37 | * @var Router\Addresses |
||
38 | */ |
||
39 | protected $addresses; |
||
40 | |||
41 | // ------------------------------------------------------------------------ |
||
42 | |||
43 | /** |
||
44 | * Router::getUri |
||
45 | * |
||
46 | * Gets routed Uri. |
||
47 | * |
||
48 | * @return Message\Uri |
||
49 | */ |
||
50 | public function getUri() |
||
51 | { |
||
52 | return $this->uri; |
||
53 | } |
||
54 | |||
55 | // ------------------------------------------------------------------------ |
||
56 | |||
57 | /** |
||
58 | * Router::getAddresses |
||
59 | * |
||
60 | * @return \O2System\Kernel\Http\Router\Addresses |
||
61 | */ |
||
62 | public function getAddresses() |
||
63 | { |
||
64 | return $this->addresses; |
||
65 | } |
||
66 | |||
67 | // ------------------------------------------------------------------------ |
||
68 | |||
69 | /** |
||
70 | * Router::setAddresses |
||
71 | * |
||
72 | * Sets router addresses. |
||
73 | * |
||
74 | * @param \O2System\Kernel\Http\Router\Addresses $addresses |
||
75 | * |
||
76 | * @return static |
||
77 | */ |
||
78 | public function setAddresses(Router\Addresses $addresses) |
||
79 | { |
||
80 | $this->addresses = $addresses; |
||
81 | |||
82 | return $this; |
||
83 | } |
||
84 | |||
85 | // ------------------------------------------------------------------------ |
||
86 | |||
87 | /** |
||
88 | * Router::handle |
||
89 | * |
||
90 | * @param \O2System\Kernel\Http\Message\Uri|null $uri |
||
91 | * |
||
92 | * @return bool |
||
93 | * @throws \ReflectionException |
||
94 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
95 | */ |
||
96 | public function handle(Message\Uri $uri = null) |
||
97 | { |
||
98 | $this->uri = is_null($uri) ? new KernelMessageUri() : $uri; |
||
99 | |||
100 | // Handle Extension Request |
||
101 | if ($this->uri->segments->count()) { |
||
102 | $this->handleExtensionRequest(); |
||
103 | } else { |
||
104 | $uriPath = urldecode( |
||
105 | parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) |
||
106 | ); |
||
107 | |||
108 | $uriPathParts = explode('public/', $uriPath); |
||
109 | $uriPath = end($uriPathParts); |
||
110 | |||
111 | if ($uriPath !== '/') { |
||
112 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments( |
||
113 | array_filter(explode('/', $uriPath))) |
||
|
|||
114 | ); |
||
115 | } |
||
116 | |||
117 | unset($uriPathParts, $uriPath); |
||
118 | } |
||
119 | |||
120 | // Load app addresses config |
||
121 | $this->addresses = config()->loadFile('addresses', true); |
||
122 | |||
123 | // Try to translate from uri string |
||
124 | if (false !== ($action = $this->addresses->getTranslation($this->uri->segments->__toString()))) { |
||
125 | if ( ! $action->isValidHttpMethod(input()->server('REQUEST_METHOD')) && ! $action->isAnyHttpMethod()) { |
||
126 | output()->sendError(405); |
||
127 | } else { |
||
128 | // Checks if action closure is an array |
||
129 | if (is_array($closureSegments = $action->getClosure())) { |
||
130 | $this->uri->segments->exchangeArray($closureSegments); |
||
131 | |||
132 | $this->handleSegmentsRequest(); |
||
133 | } else { |
||
134 | if (false !== ($parseSegments = $action->getParseUriString($this->uri->segments->__toString()))) { |
||
135 | $uriSegments = $parseSegments; |
||
136 | } else { |
||
137 | $uriSegments = []; |
||
138 | } |
||
139 | |||
140 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
141 | |||
142 | $this->parseAction($action, $uriSegments); |
||
143 | if ( ! empty(services()->has('controller'))) { |
||
144 | return true; |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | } else { |
||
149 | $this->handleSegmentsRequest(); |
||
150 | } |
||
151 | |||
152 | // break the loop if the controller has been set |
||
153 | if (services()->has('controller')) { |
||
154 | return true; |
||
155 | } |
||
156 | |||
157 | // Let's the app do the rest when there is no controller found |
||
158 | // the app should redirect to PAGE 404 |
||
159 | } |
||
160 | |||
161 | // ------------------------------------------------------------------------ |
||
162 | |||
163 | /** |
||
164 | * Router::handleExtensionRequest |
||
165 | */ |
||
166 | protected function handleExtensionRequest() |
||
167 | { |
||
168 | $lastSegment = $this->uri->segments->last(); |
||
169 | |||
170 | if (strpos($lastSegment, '.json') !== false) { |
||
171 | output()->setContentType('application/json'); |
||
172 | $lastSegment = str_replace('.json', '', $lastSegment); |
||
173 | $this->uri->segments->pop(); |
||
174 | $this->uri->segments->push($lastSegment); |
||
175 | } elseif (strpos($lastSegment, '.xml') !== false) { |
||
176 | output()->setContentType('application/xml'); |
||
177 | $lastSegment = str_replace('.xml', '', $lastSegment); |
||
178 | $this->uri->segments->pop(); |
||
179 | $this->uri->segments->push($lastSegment); |
||
180 | } elseif (strpos($lastSegment, '.js') !== false) { |
||
181 | output()->setContentType('application/x-javascript'); |
||
182 | $lastSegment = str_replace('.js', '', $lastSegment); |
||
183 | $this->uri->segments->pop(); |
||
184 | $this->uri->segments->push($lastSegment); |
||
185 | } elseif (strpos($lastSegment, '.css') !== false) { |
||
186 | output()->setContentType('text/css'); |
||
187 | $lastSegment = str_replace('.css', '', $lastSegment); |
||
188 | $this->uri->segments->pop(); |
||
189 | $this->uri->segments->push($lastSegment); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | // ------------------------------------------------------------------------ |
||
194 | |||
195 | /** |
||
196 | * Router::handleModuleRequest |
||
197 | */ |
||
198 | public function handleSegmentsRequest() |
||
199 | { |
||
200 | // Try to get route from controller |
||
201 | if ($numOfUriSegments = $this->uri->segments->count()) { |
||
202 | $uriSegments = $this->uri->segments->getArrayCopy(); |
||
203 | |||
204 | $namespaces = [ |
||
205 | 'App\Controllers\\', |
||
206 | 'App\Http\Controllers\\', |
||
207 | 'O2System\Reactor\Http\Controllers\\', |
||
208 | ]; |
||
209 | |||
210 | for ($i = 0; $i <= $numOfUriSegments; $i++) { |
||
211 | $uriRoutedSegments = array_slice($uriSegments, 0, ($numOfUriSegments - $i)); |
||
212 | |||
213 | foreach ($namespaces as $namespace) { |
||
214 | $controllerClassName = $namespace . implode('\\', |
||
215 | array_map('studlycase', $uriRoutedSegments)); |
||
216 | |||
217 | if (class_exists($controllerClassName)) { |
||
218 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
219 | $this->setController(new Router\DataStructures\Controller($controllerClassName), |
||
220 | $uriSegments); |
||
221 | break; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | // break the loop if the controller has been set |
||
226 | if (services()->has('controller')) { |
||
227 | break; |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | |||
233 | // ------------------------------------------------------------------------ |
||
234 | |||
235 | /** |
||
236 | * Router::parseAction |
||
237 | * |
||
238 | * @param \O2System\Kernel\Http\Router\DataStructures\Action $action |
||
239 | * @param array $uriSegments |
||
240 | * |
||
241 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
242 | * @throws \ReflectionException |
||
243 | */ |
||
244 | protected function parseAction(Router\DataStructures\Action $action, array $uriSegments = []) |
||
245 | { |
||
246 | $closure = $action->getClosure(); |
||
247 | if (empty($closure)) { |
||
248 | output()->sendError(204); |
||
249 | } |
||
250 | |||
251 | if ($closure instanceof Controller) { |
||
252 | $uriSegments = empty($uriSegments) |
||
253 | ? $action->getClosureParameters() |
||
254 | : $uriSegments; |
||
255 | $this->setController( |
||
256 | (new Router\DataStructures\Controller($closure)) |
||
257 | ->setRequestMethod('index'), |
||
258 | $uriSegments |
||
259 | ); |
||
260 | } elseif ($closure instanceof Router\DataStructures\Controller) { |
||
261 | $this->setController($closure, $action->getClosureParameters()); |
||
262 | } elseif (is_array($closure)) { |
||
263 | $this->uri = (new Message\Uri()) |
||
264 | ->withSegments(new Message\Uri\Segments('')) |
||
265 | ->withQuery(''); |
||
266 | $this->handle($this->uri->addSegments($closure)); |
||
267 | } else { |
||
268 | if (class_exists($closure)) { |
||
269 | $this->setController( |
||
270 | (new Router\DataStructures\Controller($closure)) |
||
271 | ->setRequestMethod('index'), |
||
272 | $uriSegments |
||
273 | ); |
||
274 | } elseif (preg_match("/([a-zA-Z0-9\\\]+)(@)([a-zA-Z0-9\\\]+)/", $closure, $matches)) { |
||
275 | $this->setController( |
||
276 | (new Router\DataStructures\Controller($matches[ 1 ])) |
||
277 | ->setRequestMethod($matches[ 3 ]), |
||
278 | $uriSegments |
||
279 | ); |
||
280 | } elseif (is_string($closure) && $closure !== '') { |
||
281 | if (is_json($closure)) { |
||
282 | output()->setContentType('application/json'); |
||
283 | output()->send($closure); |
||
284 | } else { |
||
285 | output()->send($closure); |
||
286 | } |
||
287 | } elseif (is_array($closure) || is_object($closure)) { |
||
288 | output()->send($closure); |
||
289 | } elseif (is_numeric($closure)) { |
||
290 | output()->sendError($closure); |
||
291 | } else { |
||
292 | output()->sendError(204); |
||
293 | exit(EXIT_ERROR); |
||
294 | } |
||
295 | } |
||
296 | } |
||
297 | |||
298 | // ------------------------------------------------------------------------ |
||
299 | |||
300 | /** |
||
301 | * Router::setController |
||
302 | * |
||
303 | * @param \O2System\Kernel\Http\Router\DataStructures\Controller $controller |
||
304 | * @param array $uriSegments |
||
305 | * |
||
306 | * @throws \ReflectionException |
||
307 | */ |
||
308 | protected function setController( |
||
382 | } |
||
383 | } |