‪TYPO3CMS  ‪main
PageUriMatcher.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
22 
33 {
37  protected $routes;
38 
42  protected $mappableProcessor;
43 
47  public function __construct(‪RouteCollection $routes)
48  {
49  $this->‪routes = $routes;
51  }
52 
59  public function ‪match(string $urlPath)
60  {
61  if ($ret = $this->‪matchCollection(rawurldecode($urlPath), $this->‪routes)) {
62  return $ret;
63  }
64  throw new ResourceNotFoundException(
65  sprintf('No routes found for "%s".', $urlPath),
66  1538156220
67  );
68  }
69 
77  protected function ‪matchCollection(string $urlPath, RouteCollection $routes): ?array
78  {
79  foreach ($routes as $name => $route) {
80  $urlPath = $this->‪getDecoratedRoutePath($route) ?? $urlPath;
81  $compiledRoute = $route->compile();
82 
83  // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
84  if ($compiledRoute->getStaticPrefix() !== '' && !str_starts_with($urlPath, $compiledRoute->getStaticPrefix())) {
85  continue;
86  }
87 
88  if (!preg_match($compiledRoute->getRegex(), $urlPath, $matches)) {
89  continue;
90  }
91 
92  // custom handling of Mappable instances
93  if (!$this->‪mappableProcessor->resolve($route, $matches)) {
94  continue;
95  }
96 
97  return $this->‪getAttributes($route, $name, $matches);
98  }
99  return null;
100  }
101 
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) && $value !== null) {
155  $defaults[$key] = $value;
156  }
157  }
158  return $defaults;
159  }
160 }
‪TYPO3\CMS\Core\Routing\PageUriMatcher\getDecoratedRoutePath
‪getDecoratedRoutePath(Route $route)
Definition: PageUriMatcher.php:104
‪TYPO3\CMS\Core\Routing\PageUriMatcher\getAttributes
‪array getAttributes(Route $route, string $name, array $attributes)
Definition: PageUriMatcher.php:125
‪TYPO3\CMS\Core\Routing\PageUriMatcher
Definition: PageUriMatcher.php:33
‪TYPO3\CMS\Core\Routing\RouteCollection
Definition: RouteCollection.php:30
‪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\mappableProcessor
‪$this mappableProcessor
Definition: PageUriMatcher.php:48
‪TYPO3\CMS\Core\Routing\PageUriMatcher\match
‪array match(string $urlPath)
Definition: PageUriMatcher.php:57
‪TYPO3\CMS\Core\Routing\PageUriMatcher\routes
‪RouteCollection< string, $routes;protected MappableProcessor $mappableProcessor;public function __construct(RouteCollection $routes) { $this-> routes
Definition: PageUriMatcher.php:47
‪TYPO3\CMS\Core\Routing\Aspect\MappableProcessor
Definition: MappableProcessor.php:26
‪TYPO3\CMS\Core\Routing\Route
Definition: Route.php:32
‪TYPO3\CMS\Core\Routing\PageUriMatcher\matchCollection
‪array matchCollection(string $urlPath, RouteCollection $routes)
Definition: PageUriMatcher.php:75