‪TYPO3CMS  10.4
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 
44  public function __construct(‪RouteCollection $routes)
45  {
46  $this->‪routes = $routes;
48  }
49 
57  public function ‪match(string $urlPath)
58  {
59  if ($ret = $this->‪matchCollection(rawurldecode($urlPath), $this->‪routes)) {
60  return $ret;
61  }
62  throw new ResourceNotFoundException(
63  sprintf('No routes found for "%s".', $urlPath),
64  1538156220
65  );
66  }
67 
75  protected function ‪matchCollection(string $urlPath, RouteCollection $routes): ?array
76  {
77  foreach ($routes as $name => $route) {
78  $urlPath = $this->‪getDecoratedRoutePath($route) ?? $urlPath;
79  $compiledRoute = $route->compile();
80 
81  // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
82  if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($urlPath, $compiledRoute->getStaticPrefix())) {
83  continue;
84  }
85 
86  if (!preg_match($compiledRoute->getRegex(), $urlPath, $matches)) {
87  continue;
88  }
89 
90  // custom handling of Mappable instances
91  if (!$this->‪mappableProcessor->resolve($route, $matches)) {
92  continue;
93  }
94 
95  return $this->‪getAttributes($route, $name, $matches);
96  }
97  return null;
98  }
99 
107  protected function ‪getDecoratedRoutePath(Route $route): ?string
108  {
109  if (!$route->hasOption('_decoratedRoutePath')) {
110  return null;
111  }
112  $urlPath = $route->getOption('_decoratedRoutePath');
113  return rawurldecode($urlPath);
114  }
115 
128  protected function ‪getAttributes(Route $route, string $name, array $attributes): array
129  {
130  $defaults = $route->getDefaults();
131  if (isset($defaults['_canonical_route'])) {
132  $name = $defaults['_canonical_route'];
133  unset($defaults['_canonical_route']);
134  }
135  $attributes['_route'] = $name;
136  // store applied default values in route options
137  $relevantDefaults = array_intersect_key($defaults, array_flip($route->compile()->getPathVariables()));
138  // option '_appliedDefaults' contains internal(!) values (default values are not mapped when resolving)
139  // (keys used are deflated and need to be inflated later using VariableProcessor)
140  $route->setOption('_appliedDefaults', array_diff_key($relevantDefaults, $attributes));
141  // side note: $defaults can contain e.g. '_controller'
142  return $this->‪mergeDefaults($attributes, $defaults);
143  }
144 
152  protected function ‪mergeDefaults(array $params, array $defaults): array
153  {
154  foreach ($params as $key => $value) {
155  if (!is_int($key) && null !== $value) {
156  $defaults[$key] = $value;
157  }
158  }
159  return $defaults;
160  }
161 }
‪TYPO3\CMS\Core\Routing\PageUriMatcher\getAttributes
‪array getAttributes(Route $route, string $name, array $attributes)
Definition: PageUriMatcher.php:126
‪TYPO3\CMS\Core\Routing\PageUriMatcher
Definition: PageUriMatcher.php:33
‪TYPO3\CMS\Core\Routing\RouteCollection
Definition: RouteCollection.php:28
‪TYPO3\CMS\Core\Routing
‪TYPO3\CMS\Core\Routing\PageUriMatcher\mergeDefaults
‪array mergeDefaults(array $params, array $defaults)
Definition: PageUriMatcher.php:150
‪TYPO3\CMS\Core\Routing\PageUriMatcher\getDecoratedRoutePath
‪string null getDecoratedRoutePath(Route $route)
Definition: PageUriMatcher.php:105
‪TYPO3\CMS\Core\Routing\PageUriMatcher\mappableProcessor
‪$this mappableProcessor
Definition: PageUriMatcher.php:45
‪TYPO3\CMS\Core\Routing\PageUriMatcher\match
‪array match(string $urlPath)
Definition: PageUriMatcher.php:55
‪TYPO3\CMS\Core\Routing\PageUriMatcher\routes
‪RouteCollection< string, $routes;protected MappableProcessor $mappableProcessor;public function __construct(RouteCollection $routes) { $this-> routes
Definition: PageUriMatcher.php:44
‪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:73