Total Complexity | 41 |
Total Lines | 251 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | 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 |
||
27 | class Router extends RouteController |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * Request instance |
||
32 | * @var \Quantum\Http\Request; |
||
33 | */ |
||
34 | private $request; |
||
35 | |||
36 | /** |
||
37 | * Response instance |
||
38 | * @var \Quantum\Http\Response; |
||
39 | */ |
||
40 | private $response; |
||
41 | |||
42 | /** |
||
43 | * List of routes |
||
44 | * @var array |
||
45 | */ |
||
46 | private $routes = []; |
||
47 | |||
48 | /** |
||
49 | * matched routes |
||
50 | * @var array |
||
51 | */ |
||
52 | private $matchedRoutes = []; |
||
53 | |||
54 | /** |
||
55 | * Matched URI |
||
56 | * @var string |
||
57 | */ |
||
58 | private $matchedUri = null; |
||
59 | |||
60 | /** |
||
61 | * Router constructor. |
||
62 | * @param \Quantum\Http\Request $request |
||
63 | * @param \Quantum\Http\Response $response |
||
64 | */ |
||
65 | public function __construct(Request $request, Response $response) |
||
66 | { |
||
67 | $this->request = $request; |
||
68 | $this->response = $response; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Finds the current route |
||
73 | * @throws \Quantum\Exceptions\DiException |
||
74 | * @throws \Quantum\Exceptions\RouteException |
||
75 | * @throws \Quantum\Exceptions\StopExecutionException |
||
76 | * @throws \ReflectionException |
||
77 | */ |
||
78 | public function findRoute() |
||
79 | { |
||
80 | $this->resetRoutes(); |
||
81 | |||
82 | $uri = $this->request->getUri(); |
||
83 | |||
84 | if (!$uri) { |
||
85 | throw RouteException::notFound(); |
||
86 | } |
||
87 | |||
88 | $this->findStraightMatches($uri); |
||
89 | |||
90 | if (!count($this->matchedRoutes)) { |
||
91 | $this->findPatternMatches($uri); |
||
92 | } |
||
93 | |||
94 | if (!count($this->matchedRoutes)) { |
||
95 | hook('errorPage'); |
||
96 | stop(); |
||
97 | } |
||
98 | |||
99 | if (count($this->matchedRoutes) > 1) { |
||
100 | $this->checkCollision(); |
||
101 | } |
||
102 | |||
103 | $matchedRoute = current($this->matchedRoutes); |
||
104 | |||
105 | hook('headers'); |
||
106 | |||
107 | if ($this->request->getMethod() != 'OPTIONS') { |
||
108 | $this->checkMethod($matchedRoute); |
||
109 | } |
||
110 | |||
111 | $matchedRoute['uri'] = $this->request->getUri(); |
||
112 | |||
113 | self::setCurrentRoute($matchedRoute); |
||
114 | |||
115 | if (filter_var(config()->get('debug'), FILTER_VALIDATE_BOOLEAN)) { |
||
116 | $routeInfo = []; |
||
117 | array_walk($matchedRoute, function ($value, $key) use (&$routeInfo) { |
||
118 | $routeInfo[ucfirst($key)] = is_array($value) ? implode(', ', $value) : $value; |
||
119 | }); |
||
120 | Debugger::addToStore(Debugger::ROUTES, LogLevel::INFO, $routeInfo); |
||
121 | } |
||
122 | |||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Set Routes |
||
127 | * @param array $routes |
||
128 | */ |
||
129 | public function setRoutes(array $routes) |
||
130 | { |
||
131 | $this->routes = $routes; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get Routes |
||
136 | * @return array |
||
137 | */ |
||
138 | public function getRoutes(): array |
||
139 | { |
||
140 | return $this->routes; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Resets the routes |
||
145 | */ |
||
146 | private function resetRoutes() |
||
147 | { |
||
148 | parent::$currentRoute = null; |
||
149 | $this->matchedUri = null; |
||
150 | $this->matchedRoutes = []; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Finds straight matches |
||
155 | * @param string $uri |
||
156 | */ |
||
157 | private function findStraightMatches(string $uri) |
||
158 | { |
||
159 | $requestUri = trim(urldecode(preg_replace('/[?]/', '', $uri)), '/'); |
||
160 | |||
161 | foreach ($this->routes as $route) { |
||
162 | if ($requestUri == trim($route['route'], '/')) { |
||
163 | $route['args'] = []; |
||
164 | $this->matchedUri = $route['route']; |
||
165 | $this->matchedRoutes[] = $route; |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Finds matches by pattern |
||
172 | * @param string $uri |
||
173 | */ |
||
174 | private function findPatternMatches(string $uri) |
||
175 | { |
||
176 | $requestUri = urldecode(parse_url($uri)['path']); |
||
177 | |||
178 | foreach ($this->routes as $route) { |
||
179 | $pattern = trim($route['route'], '/'); |
||
180 | $pattern = str_replace('/', '\/', $pattern); |
||
181 | $pattern = preg_replace_callback('/(\\\\\/)*\[(:num)(:([0-9]+))*\](\?)?/', [$this, 'getPattern'], $pattern); |
||
182 | $pattern = preg_replace_callback('/(\\\\\/)*\[(:alpha)(:([0-9]+))*\](\?)?/', [$this, 'getPattern'], $pattern); |
||
183 | $pattern = preg_replace_callback('/(\\\\\/)*\[(:any)(:([0-9]+))*\](\?)?/', [$this, 'getPattern'], $pattern); |
||
184 | |||
185 | $pattern = mb_substr($pattern, 0, 4) != '(\/)' ? '(\/)?' . $pattern : $pattern; |
||
186 | |||
187 | preg_match("/^" . $pattern . "$/u", $requestUri, $matches); |
||
188 | |||
189 | if (count($matches)) { |
||
190 | $this->matchedUri = reset($matches) ?: '/'; |
||
191 | array_shift($matches); |
||
192 | $route['args'] = array_diff($matches, ['', '/']); |
||
193 | $route['pattern'] = $pattern; |
||
194 | $this->matchedRoutes[] = $route; |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Checks the route collisions |
||
201 | * @throws \Quantum\Exceptions\RouteException |
||
202 | */ |
||
203 | private function checkCollision() |
||
215 | } |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Checks the request method against defined route method |
||
222 | * @param array $matchedRoute |
||
223 | * @throws \Quantum\Exceptions\RouteException |
||
224 | */ |
||
225 | private function checkMethod(array $matchedRoute) |
||
226 | { |
||
227 | if (strpos($matchedRoute['method'], '|') !== false) { |
||
228 | if (!in_array($this->request->getMethod(), explode('|', $matchedRoute['method']))) { |
||
229 | throw RouteException::incorrectMethod($this->request->getMethod()); |
||
230 | } |
||
231 | } else if ($this->request->getMethod() != $matchedRoute['method']) { |
||
232 | throw RouteException::incorrectMethod($this->request->getMethod()); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Finds URL pattern |
||
238 | * @param array $matches |
||
239 | * @return string |
||
240 | */ |
||
241 | private function getPattern(array $matches): string |
||
278 | } |
||
279 | |||
280 | } |
||
281 |