‪TYPO3CMS  11.5
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\RequestContext;
21 use Symfony\Component\Routing\Route as SymfonyRoute;
22 use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;
26 
39 {
44  protected ‪$routeCollection;
45 
46  public function ‪__construct()
47  {
48  $this->routeCollection = new SymfonyRouteCollection();
49  }
56  public function ‪addRoute($routeIdentifier, $route)
57  {
58  $symfonyRoute = new SymfonyRoute($route->getPath(), [], [], $route->getOptions());
59  $symfonyRoute->setMethods($route->getMethods());
60  $this->routeCollection->add($routeIdentifier, $symfonyRoute);
61  }
62 
68  public function ‪getRoutes(): iterable
69  {
70  return $this->routeCollection->getIterator();
71  }
72 
77  public function ‪getRouteCollection(): SymfonyRouteCollection
78  {
80  }
81 
89  public function ‪match($pathInfo)
90  {
91  foreach ($this->routeCollection->getIterator() as $routeIdentifier => $route) {
92  // This check is done in a simple way as there are no parameters yet (get parameters only)
93  if ($route->getPath() === $pathInfo) {
94  $routeResult = new Route($route->getPath(), $route->getOptions());
95  // Store the name of the Route in the _identifier option so the token can be checked against that
96  $routeResult->setOption('_identifier', $routeIdentifier);
97  return $routeResult;
98  }
99  }
100 
101  throw new ResourceNotFoundException('The requested resource "' . $pathInfo . '" was not found.', 1425389240);
102  }
103 
110  public function ‪matchRequest(ServerRequestInterface $request)
111  {
112  $path = $request->getUri()->‪getPath();
113  if (($normalizedParams = $request->getAttribute('normalizedParams')) !== null) {
114  // Remove the directory name of the script from the path. This will usually be `/typo3` in this context.
115  $path = substr($path, strlen(dirname($normalizedParams->getScriptName())));
116  }
117  if ($path === '' || $path === '/' || $path === '/index.php') {
118  // Allow the login page to be displayed if routing is not used and on index.php
119  // (consolidate RouteDispatcher::evaluateReferrer() when changing 'login' to something different)
120  $path = $request->getQueryParams()['route'] ?? $request->getParsedBody()['route'] ?? '/login';
121  }
122  $context = new RequestContext(
123  $path,
124  $request->getMethod(),
125  (string)idn_to_ascii($request->getUri()->getHost()),
126  $request->getUri()->getScheme()
127  );
128  try {
129  $result = (new UrlMatcher($this->routeCollection, $context))->‪match($path);
130  $matchedSymfonyRoute = $this->routeCollection->get($result['_route']);
131  if ($matchedSymfonyRoute === null) {
132  throw new ResourceNotFoundException('The requested resource "' . $path . '" was not found.', 1607596900);
133  }
134  } catch (\Symfony\Component\Routing\Exception\MethodNotAllowedException $e) {
135  throw new MethodNotAllowedException($e->getMessage(), 1612649842);
136  } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
137  throw new ResourceNotFoundException('The requested resource "' . $path . '" was not found.', 1612649840);
138  }
139  // Apply matched method to route
140  $matchedOptions = $matchedSymfonyRoute->getOptions();
141  $methods = $matchedOptions['methods'] ?? [];
142  unset($matchedOptions['methods']);
143  $route = new Route($matchedSymfonyRoute->getPath(), $matchedOptions);
144  if (count($methods) > 0) {
145  $route->setMethods($methods);
146  }
147  $route->setOption('_identifier', $result['_route']);
148  return $route;
149  }
150 }
‪TYPO3\CMS\Backend\Routing\Route\getPath
‪string getPath()
Definition: Route.php:51
‪TYPO3\CMS\Backend\Routing\Router\addRoute
‪addRoute($routeIdentifier, $route)
Definition: Router.php:55
‪TYPO3\CMS\Backend\Routing\Exception\MethodNotAllowedException
Definition: MethodNotAllowedException.php:23
‪TYPO3\CMS\Backend\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Backend\Routing\Router\getRouteCollection
‪SymfonyRouteCollection getRouteCollection()
Definition: Router.php:76
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Routing\Router\__construct
‪__construct()
Definition: Router.php:45
‪TYPO3\CMS\Backend\Routing\Router\matchRequest
‪Route matchRequest(ServerRequestInterface $request)
Definition: Router.php:109
‪TYPO3\CMS\Backend\Routing\Router\$routeCollection
‪SymfonyRouteCollection $routeCollection
Definition: Router.php:43
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException
Definition: ResourceNotFoundException.php:23
‪TYPO3\CMS\Backend\Routing\Router\match
‪Route match($pathInfo)
Definition: Router.php:88
‪TYPO3\CMS\Backend\Routing\Router\getRoutes
‪Route[] getRoutes()
Definition: Router.php:67
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:39
‪TYPO3\CMS\Backend\Routing