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