Total Complexity | 61 |
Total Lines | 372 |
Duplicated Lines | 0 % |
Changes | 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 |
||
47 | class Router implements IRouter { |
||
|
|||
48 | /** @var RouteCollection[] */ |
||
49 | protected $collections = []; |
||
50 | /** @var null|RouteCollection */ |
||
51 | protected $collection = null; |
||
52 | /** @var null|string */ |
||
53 | protected $collectionName = null; |
||
54 | /** @var null|RouteCollection */ |
||
55 | protected $root = null; |
||
56 | /** @var null|UrlGenerator */ |
||
57 | protected $generator = null; |
||
58 | /** @var string[] */ |
||
59 | protected $routingFiles; |
||
60 | /** @var bool */ |
||
61 | protected $loaded = false; |
||
62 | /** @var array */ |
||
63 | protected $loadedApps = []; |
||
64 | /** @var ILogger */ |
||
65 | protected $logger; |
||
66 | /** @var RequestContext */ |
||
67 | protected $context; |
||
68 | |||
69 | /** |
||
70 | * @param ILogger $logger |
||
71 | */ |
||
72 | public function __construct(ILogger $logger) { |
||
73 | $this->logger = $logger; |
||
74 | $baseUrl = \OC::$WEBROOT; |
||
75 | if (!(\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) { |
||
76 | $baseUrl = \OC::$server->getURLGenerator()->linkTo('', 'index.php'); |
||
77 | } |
||
78 | if (!\OC::$CLI && isset($_SERVER['REQUEST_METHOD'])) { |
||
79 | $method = $_SERVER['REQUEST_METHOD']; |
||
80 | } else { |
||
81 | $method = 'GET'; |
||
82 | } |
||
83 | $request = \OC::$server->getRequest(); |
||
84 | $host = $request->getServerHost(); |
||
85 | $schema = $request->getServerProtocol(); |
||
86 | $this->context = new RequestContext($baseUrl, $method, $host, $schema); |
||
87 | // TODO cache |
||
88 | $this->root = $this->getCollection('root'); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get the files to load the routes from |
||
93 | * |
||
94 | * @return string[] |
||
95 | */ |
||
96 | public function getRoutingFiles() { |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Loads the routes |
||
114 | * |
||
115 | * @param null|string $app |
||
116 | */ |
||
117 | public function loadRoutes($app = null) { |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | * @deprecated |
||
182 | */ |
||
183 | public function getCacheKey() { |
||
184 | return ''; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @param string $name |
||
189 | * @return \Symfony\Component\Routing\RouteCollection |
||
190 | */ |
||
191 | protected function getCollection($name) { |
||
192 | if (!isset($this->collections[$name])) { |
||
193 | $this->collections[$name] = new RouteCollection(); |
||
194 | } |
||
195 | return $this->collections[$name]; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Sets the collection to use for adding routes |
||
200 | * |
||
201 | * @param string $name Name of the collection to use. |
||
202 | * @return void |
||
203 | */ |
||
204 | public function useCollection($name) { |
||
205 | $this->collection = $this->getCollection($name); |
||
206 | $this->collectionName = $name; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * returns the current collection name in use for adding routes |
||
211 | * |
||
212 | * @return string the collection name |
||
213 | */ |
||
214 | public function getCurrentCollection() { |
||
215 | return $this->collectionName; |
||
216 | } |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Create a \OC\Route\Route. |
||
221 | * |
||
222 | * @param string $name Name of the route to create. |
||
223 | * @param string $pattern The pattern to match |
||
224 | * @param array $defaults An array of default parameter values |
||
225 | * @param array $requirements An array of requirements for parameters (regexes) |
||
226 | * @return \OC\Route\Route |
||
227 | */ |
||
228 | public function create($name, |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Find the route matching $url |
||
239 | * |
||
240 | * @param string $url The url to find |
||
241 | * @throws \Exception |
||
242 | * @return void |
||
243 | */ |
||
244 | public function match($url) { |
||
245 | if (substr($url, 0, 6) === '/apps/') { |
||
246 | // empty string / 'apps' / $app / rest of the route |
||
247 | list(, , $app,) = explode('/', $url, 4); |
||
248 | |||
249 | $app = \OC_App::cleanAppId($app); |
||
250 | \OC::$REQUESTEDAPP = $app; |
||
251 | $this->loadRoutes($app); |
||
252 | } elseif (substr($url, 0, 13) === '/ocsapp/apps/') { |
||
253 | // empty string / 'ocsapp' / 'apps' / $app / rest of the route |
||
254 | list(, , , $app,) = explode('/', $url, 5); |
||
255 | |||
256 | $app = \OC_App::cleanAppId($app); |
||
257 | \OC::$REQUESTEDAPP = $app; |
||
258 | $this->loadRoutes($app); |
||
259 | } elseif (substr($url, 0, 10) === '/settings/') { |
||
260 | $this->loadRoutes('settings'); |
||
261 | } elseif (substr($url, 0, 6) === '/core/') { |
||
262 | \OC::$REQUESTEDAPP = $url; |
||
263 | if (!\OC::$server->getConfig()->getSystemValueBool('maintenance') && !Util::needUpgrade()) { |
||
264 | \OC_App::loadApps(); |
||
265 | } |
||
266 | $this->loadRoutes('core'); |
||
267 | } else { |
||
268 | $this->loadRoutes(); |
||
269 | } |
||
270 | |||
271 | $matcher = new UrlMatcher($this->root, $this->context); |
||
272 | try { |
||
273 | $parameters = $matcher->match($url); |
||
274 | } catch (ResourceNotFoundException $e) { |
||
275 | if (substr($url, -1) !== '/') { |
||
276 | // We allow links to apps/files? for backwards compatibility reasons |
||
277 | // However, since Symfony does not allow empty route names, the route |
||
278 | // we need to match is '/', so we need to append the '/' here. |
||
279 | try { |
||
280 | $parameters = $matcher->match($url . '/'); |
||
281 | } catch (ResourceNotFoundException $newException) { |
||
282 | // If we still didn't match a route, we throw the original exception |
||
283 | throw $e; |
||
284 | } |
||
285 | } else { |
||
286 | throw $e; |
||
287 | } |
||
288 | } |
||
289 | |||
290 | \OC::$server->getEventLogger()->start('run_route', 'Run route'); |
||
291 | if (isset($parameters['caller'])) { |
||
292 | $caller = $parameters['caller']; |
||
293 | unset($parameters['caller']); |
||
294 | $application = $this->getApplicationClass($caller[0]); |
||
295 | \OC\AppFramework\App::main($caller[1], $caller[2], $application->getContainer(), $parameters); |
||
296 | } elseif (isset($parameters['action'])) { |
||
297 | $action = $parameters['action']; |
||
298 | if (!is_callable($action)) { |
||
299 | throw new \Exception('not a callable action'); |
||
300 | } |
||
301 | unset($parameters['action']); |
||
302 | call_user_func($action, $parameters); |
||
303 | } elseif (isset($parameters['file'])) { |
||
304 | include $parameters['file']; |
||
305 | } else { |
||
306 | throw new \Exception('no action available'); |
||
307 | } |
||
308 | \OC::$server->getEventLogger()->end('run_route'); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Get the url generator |
||
313 | * |
||
314 | * @return \Symfony\Component\Routing\Generator\UrlGenerator |
||
315 | * |
||
316 | */ |
||
317 | public function getGenerator() { |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Generate url based on $name and $parameters |
||
327 | * |
||
328 | * @param string $name Name of the route to use. |
||
329 | * @param array $parameters Parameters for the route |
||
330 | * @param bool $absolute |
||
331 | * @return string |
||
332 | */ |
||
333 | public function generate($name, |
||
334 | $parameters = [], |
||
335 | $absolute = false) { |
||
336 | $this->loadRoutes(); |
||
337 | try { |
||
338 | $referenceType = UrlGenerator::ABSOLUTE_URL; |
||
339 | if ($absolute === false) { |
||
340 | $referenceType = UrlGenerator::ABSOLUTE_PATH; |
||
341 | } |
||
342 | $name = $this->fixLegacyRootName($name); |
||
343 | return $this->getGenerator()->generate($name, $parameters, $referenceType); |
||
344 | } catch (RouteNotFoundException $e) { |
||
345 | $this->logger->logException($e); |
||
346 | return ''; |
||
347 | } |
||
348 | } |
||
349 | |||
350 | protected function fixLegacyRootName(string $routeName): string { |
||
351 | if ($routeName === 'files.viewcontroller.showFile') { |
||
352 | return 'files.View.showFile'; |
||
353 | } |
||
354 | if ($routeName === 'files_sharing.sharecontroller.showShare') { |
||
355 | return 'files_sharing.Share.showShare'; |
||
356 | } |
||
357 | if ($routeName === 'files_sharing.sharecontroller.showAuthenticate') { |
||
358 | return 'files_sharing.Share.showAuthenticate'; |
||
359 | } |
||
360 | if ($routeName === 'files_sharing.sharecontroller.authenticate') { |
||
361 | return 'files_sharing.Share.authenticate'; |
||
362 | } |
||
363 | if ($routeName === 'files_sharing.sharecontroller.downloadShare') { |
||
364 | return 'files_sharing.Share.downloadShare'; |
||
365 | } |
||
366 | if ($routeName === 'files_sharing.publicpreview.directLink') { |
||
367 | return 'files_sharing.PublicPreview.directLink'; |
||
368 | } |
||
369 | if ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') { |
||
370 | return 'cloud_federation_api.RequestHandler.addShare'; |
||
371 | } |
||
372 | if ($routeName === 'cloud_federation_api.requesthandlercontroller.receiveNotification') { |
||
373 | return 'cloud_federation_api.RequestHandler.receiveNotification'; |
||
374 | } |
||
375 | return $routeName; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * To isolate the variable scope used inside the $file it is required in it's own method |
||
380 | * |
||
381 | * @param string $file the route file location to include |
||
382 | * @param string $appName |
||
383 | */ |
||
384 | private function requireRouteFile($file, $appName) { |
||
385 | $this->setupRoutes(include_once $file, $appName); |
||
386 | } |
||
387 | |||
388 | |||
389 | /** |
||
390 | * If a routes.php file returns an array, try to set up the application and |
||
391 | * register the routes for the app. The application class will be chosen by |
||
392 | * camelcasing the appname, e.g.: my_app will be turned into |
||
393 | * \OCA\MyApp\AppInfo\Application. If that class does not exist, a default |
||
394 | * App will be intialized. This makes it optional to ship an |
||
395 | * appinfo/application.php by using the built in query resolver |
||
396 | * |
||
397 | * @param array $routes the application routes |
||
398 | * @param string $appName the name of the app. |
||
399 | */ |
||
400 | private function setupRoutes($routes, $appName) { |
||
401 | if (is_array($routes)) { |
||
402 | $application = $this->getApplicationClass($appName); |
||
403 | $application->registerRoutes($this, $routes); |
||
404 | } |
||
405 | } |
||
406 | |||
407 | private function getApplicationClass(string $appName) { |
||
419 | } |
||
420 | } |
||
421 |
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.