‪TYPO3CMS  ‪main
Router.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Http\Message\ServerRequestInterface;
19 use Symfony\Component\Routing\Matcher\UrlMatcher;
20 use Symfony\Component\Routing\Route as SymfonyRoute;
26 
39 {
44 
45  public function ‪__construct(
46  protected readonly ‪RequestContextFactory $requestContextFactory
47  ) {
48  $this->routeCollection = new ‪RouteCollection();
49  }
50 
54  public function ‪addRoute(string $routeIdentifier, ‪Route $route, array $aliases = []): void
55  {
56  $symfonyRoute = new SymfonyRoute($route->‪getPath(), [], [], $route->‪getOptions());
57  $symfonyRoute->setMethods($route->‪getMethods());
58  $this->routeCollection->add($routeIdentifier, $symfonyRoute);
59  foreach ($aliases as $aliasName) {
60  $this->routeCollection->addAlias($aliasName, $routeIdentifier);
61  }
62  }
63 
65  {
66  $this->routeCollection->addCollection(‪$routeCollection);
67  }
68 
75  public function ‪getRoutes(): iterable
76  {
77  return $this->routeCollection->getIterator();
78  }
79 
80  public function ‪hasRoute(string $routeName): bool
81  {
82  return $this->routeCollection->get($routeName) !== null;
83  }
84 
90  public function ‪getRoute(string $routeName)
91  {
92  return $this->routeCollection->get($routeName);
93  }
94 
99  {
101  }
102 
110  public function ‪match($pathInfo): ‪Route
111  {
112  foreach ($this->routeCollection->getIterator() as $routeIdentifier => $route) {
113  // This check is done in a simple way as there are no parameters yet (get parameters only)
114  if ($route->getPath() === $pathInfo) {
115  $routeResult = new ‪Route($route->getPath(), $route->getOptions());
116  // Store the name of the Route in the _identifier option so the token can be checked against that
117  $routeResult->setOption('_identifier', $routeIdentifier);
118  return $routeResult;
119  }
120  }
121 
122  throw new ‪ResourceNotFoundException('The requested resource "' . $pathInfo . '" was not found.', 1425389240);
123  }
124 
128  public function ‪matchResult(ServerRequestInterface $request): ‪RouteResult
129  {
130  $path = $request->getUri()->getPath();
131  if (($normalizedParams = $request->getAttribute('normalizedParams')) !== null) {
132  // Remove the directory name of the script from the path. This will usually be `/typo3` in this context.
133  $path = substr($path, strlen(dirname($normalizedParams->getScriptName())));
134  }
135  if ($path === '' || $path === '/' || $path === '/index.php') {
136  // Allow the login page to be displayed if routing is not used and on index.php
137  // (consolidate RouteDispatcher::evaluateReferrer() when changing 'login' to something different)
138  $path = '/login';
139  }
140  $requestContext = $this->requestContextFactory->fromBackendRequest($request);
141  try {
142  $result = (new UrlMatcher($this->routeCollection, $requestContext))->‪match($path);
143  $matchedSymfonyRoute = $this->routeCollection->get($result['_route']);
144  if ($matchedSymfonyRoute === null) {
145  throw new ‪ResourceNotFoundException('The requested resource "' . $path . '" was not found.', 1607596900);
146  }
147  } catch (\Symfony\Component\Routing\‪Exception\‪MethodNotAllowedException $e) {
148  throw new ‪MethodNotAllowedException($e->getMessage(), 1612649842);
149  } catch (\Symfony\Component\Routing\‪Exception\‪ResourceNotFoundException $e) {
150  throw new ‪ResourceNotFoundException('The requested resource "' . $path . '" was not found.', 1612649840);
151  }
152  // Apply matched method to route
153  $matchedOptions = $matchedSymfonyRoute->getOptions();
154  $methods = $matchedOptions['methods'] ?? [];
155  unset($matchedOptions['methods']);
156  $route = new ‪Route($matchedSymfonyRoute->getPath(), $matchedOptions);
157  if (count($methods) > 0) {
158  $route->setMethods($methods);
159  }
160  $route->setOption('_identifier', $result['_route']);
161  unset($result['_route']);
162  return new ‪RouteResult($route, $result);
163  }
164 
171  public function ‪matchRequest(ServerRequestInterface $request): ‪Route
172  {
173  return $this->‪matchResult($request)->getRoute();
174  }
175 }
‪TYPO3\CMS\Backend\Routing\Route\getPath
‪string getPath()
Definition: Route.php:51
‪TYPO3\CMS\Backend\Routing\Router\addRouteCollection
‪addRouteCollection(RouteCollection $routeCollection)
Definition: Router.php:64
‪TYPO3\CMS\Backend\Routing\Router\hasRoute
‪hasRoute(string $routeName)
Definition: Router.php:80
‪TYPO3\CMS\Backend\Routing\Exception\MethodNotAllowedException
Definition: MethodNotAllowedException.php:23
‪TYPO3\CMS\Backend\Routing\Router\getRoute
‪SymfonyRoute Route null getRoute(string $routeName)
Definition: Router.php:90
‪TYPO3\CMS\Backend\Routing\Router\addRoute
‪addRoute(string $routeIdentifier, Route $route, array $aliases=[])
Definition: Router.php:54
‪TYPO3\CMS\Backend\Routing\Router\getRouteCollection
‪getRouteCollection()
Definition: Router.php:98
‪TYPO3\CMS\Core\Routing\RouteCollection
Definition: RouteCollection.php:30
‪TYPO3\CMS\Backend\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Backend\Routing\Router\$routeCollection
‪RouteCollection $routeCollection
Definition: Router.php:43
‪TYPO3\CMS\Backend\Routing\Route\getOptions
‪array getOptions()
Definition: Route.php:102
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Routing\RouteResult
Definition: RouteResult.php:27
‪TYPO3\CMS\Backend\Routing\Router\matchRequest
‪Route matchRequest(ServerRequestInterface $request)
Definition: Router.php:171
‪TYPO3\CMS\Backend\Routing\Route\getMethods
‪string[] getMethods()
Definition: Route.php:78
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Backend\Routing\Router\matchResult
‪matchResult(ServerRequestInterface $request)
Definition: Router.php:128
‪TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException
Definition: ResourceNotFoundException.php:23
‪TYPO3\CMS\Backend\Routing\Router\__construct
‪__construct(protected readonly RequestContextFactory $requestContextFactory)
Definition: Router.php:45
‪TYPO3\CMS\Backend\Routing\Router\match
‪Route match($pathInfo)
Definition: Router.php:110
‪TYPO3\CMS\Backend\Routing\Router\getRoutes
‪Route[] getRoutes()
Definition: Router.php:75
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:39
‪TYPO3\CMS\Backend\Routing
‪TYPO3\CMS\Core\Routing\RequestContextFactory
Definition: RequestContextFactory.php:30