‪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;
27 
40 {
45 
46  public function ‪__construct(
47  protected readonly ‪RequestContextFactory $requestContextFactory,
48  protected readonly ‪BackendEntryPointResolver $backendEntryPointResolver,
49  ) {
50  $this->routeCollection = new ‪RouteCollection();
51  }
52 
56  public function ‪addRoute(string $routeIdentifier, ‪Route $route, array $aliases = []): void
57  {
58  $symfonyRoute = new SymfonyRoute($route->‪getPath(), [], [], $route->‪getOptions());
59  $symfonyRoute->setMethods($route->‪getMethods());
60  $this->routeCollection->add($routeIdentifier, $symfonyRoute);
61  foreach ($aliases as $aliasName) {
62  $this->routeCollection->addAlias($aliasName, $routeIdentifier);
63  }
64  }
65 
67  {
68  $this->routeCollection->addCollection(‪$routeCollection);
69  }
70 
77  public function ‪getRoutes(): iterable
78  {
79  return $this->routeCollection->getIterator();
80  }
81 
82  public function ‪hasRoute(string $routeName): bool
83  {
84  return $this->routeCollection->get($routeName) !== null;
85  }
86 
92  public function ‪getRoute(string $routeName)
93  {
94  return $this->routeCollection->get($routeName);
95  }
96 
101  {
103  }
104 
112  public function ‪match($pathInfo): ‪Route
113  {
114  foreach ($this->routeCollection->getIterator() as $routeIdentifier => $route) {
115  // This check is done in a simple way as there are no parameters yet (get parameters only)
116  if ($route->getPath() === $pathInfo) {
117  $routeResult = new ‪Route($route->getPath(), $route->getOptions());
118  // Store the name of the Route in the _identifier option so the token can be checked against that
119  $routeResult->setOption('_identifier', $routeIdentifier);
120  return $routeResult;
121  }
122  }
123 
124  throw new ‪ResourceNotFoundException('The requested resource "' . $pathInfo . '" was not found.', 1425389240);
125  }
126 
130  public function ‪matchResult(ServerRequestInterface $request): ‪RouteResult
131  {
132  $path = $this->backendEntryPointResolver->getBackendRoutePath($request);
133  if ($path === null) {
134  throw new ‪ResourceNotFoundException('The requested resource "' . $request->getUri()->getPath() . '" does not contain a known backend route prefix.', 1704787661);
135  }
136 
137  if ($path === '' || $path === '/' || $path === '/index.php') {
138  // Allow the login page to be displayed if routing is not used and on index.php
139  // (consolidate RouteDispatcher::evaluateReferrer() when changing 'login' to something different)
140  $path = '/login';
141  }
142  $requestContext = $this->requestContextFactory->fromBackendRequest($request);
143  try {
144  $result = (new UrlMatcher($this->routeCollection, $requestContext))->‪match($path);
145  $matchedSymfonyRoute = $this->routeCollection->get($result['_route']);
146  if ($matchedSymfonyRoute === null) {
147  throw new ‪ResourceNotFoundException('The requested resource "' . $path . '" was not found.', 1607596900);
148  }
149  } catch (\Symfony\Component\Routing\‪Exception\‪MethodNotAllowedException $e) {
150  throw new ‪MethodNotAllowedException($e->getMessage(), 1612649842);
151  } catch (\Symfony\Component\Routing\‪Exception\‪ResourceNotFoundException $e) {
152  throw new ‪ResourceNotFoundException('The requested resource "' . $path . '" was not found.', 1612649840);
153  }
154  // Apply matched method to route
155  $matchedOptions = $matchedSymfonyRoute->getOptions();
156  $methods = $matchedOptions['methods'] ?? [];
157  unset($matchedOptions['methods']);
158  $route = new ‪Route($matchedSymfonyRoute->getPath(), $matchedOptions);
159  if (count($methods) > 0) {
160  $route->setMethods($methods);
161  }
162  $route->setOption('_identifier', $result['_route']);
163  unset($result['_route']);
164  return new ‪RouteResult($route, $result);
165  }
166 
173  public function ‪matchRequest(ServerRequestInterface $request): ‪Route
174  {
175  return $this->‪matchResult($request)->getRoute();
176  }
177 }
‪TYPO3\CMS\Backend\Routing\Route\getPath
‪string getPath()
Definition: Route.php:51
‪TYPO3\CMS\Backend\Routing\Router\addRouteCollection
‪addRouteCollection(RouteCollection $routeCollection)
Definition: Router.php:66
‪TYPO3\CMS\Backend\Routing\Router\hasRoute
‪hasRoute(string $routeName)
Definition: Router.php:82
‪TYPO3\CMS\Backend\Routing\Router\__construct
‪__construct(protected readonly RequestContextFactory $requestContextFactory, protected readonly BackendEntryPointResolver $backendEntryPointResolver,)
Definition: Router.php:46
‪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:92
‪TYPO3\CMS\Backend\Routing\Router\addRoute
‪addRoute(string $routeIdentifier, Route $route, array $aliases=[])
Definition: Router.php:56
‪TYPO3\CMS\Backend\Routing\Router\getRouteCollection
‪getRouteCollection()
Definition: Router.php:100
‪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:44
‪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:173
‪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:130
‪TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException
Definition: ResourceNotFoundException.php:23
‪TYPO3\CMS\Backend\Routing\Router\match
‪Route match($pathInfo)
Definition: Router.php:112
‪TYPO3\CMS\Backend\Routing\Router\getRoutes
‪Route[] getRoutes()
Definition: Router.php:77
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:40
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver
Definition: BackendEntryPointResolver.php:29
‪TYPO3\CMS\Backend\Routing
‪TYPO3\CMS\Core\Routing\RequestContextFactory
Definition: RequestContextFactory.php:30