‪TYPO3CMS  9.5
PageUriMatcher.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
21 
32 {
36  protected ‪$routes;
37 
42 
44  {
45  $this->routes = ‪$routes;
46  $this->mappableProcessor = new ‪MappableProcessor();
47  }
48 
56  public function ‪match(string $urlPath)
57  {
58  if ($ret = $this->‪matchCollection(rawurldecode($urlPath), $this->routes)) {
59  return $ret;
60  }
61  throw new ResourceNotFoundException(
62  sprintf('No routes found for "%s".', $urlPath),
63  1538156220
64  );
65  }
66 
74  protected function ‪matchCollection(string $urlPath, RouteCollection ‪$routes): ?array
75  {
76  foreach (‪$routes as $name => $route) {
77  $urlPath = $this->‪getDecoratedRoutePath($route) ?? $urlPath;
78  $compiledRoute = $route->compile();
79 
80  // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
81  if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($urlPath, $compiledRoute->getStaticPrefix())) {
82  continue;
83  }
84 
85  if (!preg_match($compiledRoute->getRegex(), $urlPath, $matches)) {
86  continue;
87  }
88 
89  // custom handling of Mappable instances
90  if (!$this->mappableProcessor->resolve($route, $matches)) {
91  continue;
92  }
93 
94  return $this->‪getAttributes($route, $name, $matches);
95  }
96  return null;
97  }
98 
106  protected function ‪getDecoratedRoutePath(Route $route): ?string
107  {
108  if (!$route->hasOption('_decoratedRoutePath')) {
109  return null;
110  }
111  $urlPath = $route->getOption('_decoratedRoutePath');
112  return rawurldecode($urlPath);
113  }
114 
127  protected function ‪getAttributes(Route $route, string $name, array $attributes): array
128  {
129  $defaults = $route->getDefaults();
130  if (isset($defaults['_canonical_route'])) {
131  $name = $defaults['_canonical_route'];
132  unset($defaults['_canonical_route']);
133  }
134  $attributes['_route'] = $name;
135  // store applied default values in route options
136  $relevantDefaults = array_intersect_key($defaults, array_flip($route->compile()->getPathVariables()));
137  // option '_appliedDefaults' contains internal(!) values (default values are not mapped when resolving)
138  // (keys used are deflated and need to be inflated later using VariableProcessor)
139  $route->setOption('_appliedDefaults', array_diff_key($relevantDefaults, $attributes));
140  // side note: $defaults can contain e.g. '_controller'
141  return $this->‪mergeDefaults($attributes, $defaults);
142  }
143 
151  protected function ‪mergeDefaults(array $params, array $defaults): array
152  {
153  foreach ($params as $key => $value) {
154  if (!is_int($key) && null !== $value) {
155  $defaults[$key] = $value;
156  }
157  }
158  return $defaults;
159  }
160 }
‪TYPO3\CMS\Core\Routing\PageUriMatcher\getAttributes
‪array getAttributes(Route $route, string $name, array $attributes)
Definition: PageUriMatcher.php:125
‪TYPO3\CMS\Core\Routing\PageUriMatcher\$mappableProcessor
‪MappableProcessor $mappableProcessor
Definition: PageUriMatcher.php:39
‪TYPO3\CMS\Core\Routing\PageUriMatcher
Definition: PageUriMatcher.php:32
‪TYPO3\CMS\Core\Routing\RouteCollection
Definition: RouteCollection.php:27
‪TYPO3\CMS\Core\Routing
‪TYPO3\CMS\Core\Routing\PageUriMatcher\mergeDefaults
‪array mergeDefaults(array $params, array $defaults)
Definition: PageUriMatcher.php:149
‪TYPO3\CMS\Core\Routing\PageUriMatcher\getDecoratedRoutePath
‪string null getDecoratedRoutePath(Route $route)
Definition: PageUriMatcher.php:104
‪TYPO3\CMS\Core\Routing\PageUriMatcher\match
‪array match(string $urlPath)
Definition: PageUriMatcher.php:54
‪TYPO3\CMS\Core\Routing\PageUriMatcher\__construct
‪__construct(RouteCollection $routes)
Definition: PageUriMatcher.php:41
‪TYPO3\CMS\Core\Routing\Aspect\MappableProcessor
Definition: MappableProcessor.php:25
‪TYPO3\CMS\Core\Routing\Route
Definition: Route.php:31
‪TYPO3\CMS\Core\Routing\PageUriMatcher\matchCollection
‪array matchCollection(string $urlPath, RouteCollection $routes)
Definition: PageUriMatcher.php:72
‪TYPO3\CMS\Core\Routing\PageUriMatcher\$routes
‪RouteCollection $routes
Definition: PageUriMatcher.php:35