[!!!][FEATURE] Introduce Backend Routing
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Routing / Router.php
1 <?php
2 namespace TYPO3\CMS\Backend\Routing;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException;
19 use TYPO3\CMS\Core\Cache\CacheManager;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Package\PackageManager;
22
23 /**
24 * Implementation of a class for adding routes, collecting throughout the Bootstrap
25 * to register all sorts of Backend Routes, and to fetch the main Collection in order
26 * to resolve a route (see ->match() and ->matchRequest()).
27 *
28 * Ideally, the Router is solely instantiated and accessed via the Bootstrap, the RequestHandler and the UriBuilder.
29 *
30 * See \TYPO3\CMS\Backend\RequestHandler for more details on route matching() and Bootstrap->initializeBackendRouting().
31 *
32 * The architecture is inspired by the Symfony Routing Component.
33 */
34 class Router implements \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * All routes used in the Backend
38 *
39 * @var array|null
40 */
41 protected $routes = array();
42
43 /**
44 * Adds a new route with the identifiers
45 *
46 * @param string $routeIdentifier
47 * @param Route $route
48 */
49 public function addRoute($routeIdentifier, $route) {
50 $this->routes[$routeIdentifier] = $route;
51 }
52
53 /**
54 * Fetch all registered routes, only use in UriBuilder
55 *
56 * @return array|null
57 */
58 public function getRoutes() {
59 return $this->routes;
60 }
61
62 /**
63 * Tries to match a URL path with a set of routes.
64 *
65 * @param string $pathInfo The path info to be parsed
66 * @return Route the first Route object found
67 * @throws ResourceNotFoundException If the resource could not be found
68 */
69 public function match($pathInfo) {
70 $pathInfo = rawurldecode($pathInfo);
71 foreach ($this->routes as $routeIdentifier => $route) {
72 // This check is done in a simple way as there are no parameters yet (get parameters only)
73 if ($route->getPath() === $pathInfo) {
74 // Store the name of the Route in the _identifier option so the token can be checked against that
75 $route->setOption('_identifier', $routeIdentifier);
76 return $route;
77 }
78 }
79 throw new ResourceNotFoundException('The requested resource "' . $pathInfo . '" was not found.', 1425389240);
80 }
81
82 /**
83 * Tries to match a URI against the registered routes
84 *
85 * @param ServerRequestInterface $request
86 * @return Route the first Route object found
87 */
88 public function matchRequest(ServerRequestInterface $request) {
89 return $this->match($request->getAttribute('routePath'));
90 }
91 }