‪TYPO3CMS  9.5
AbstractEnhancer.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 
22 
26 abstract class ‪AbstractEnhancer implements ‪EnhancerInterface
27 {
31  protected ‪$aspects = [];
32 
36  protected ‪$variableProcessor;
37 
43  protected function ‪applyRouteAspects(‪Route $route, array ‪$aspects, string $namespace = null)
44  {
45  if (empty(‪$aspects)) {
46  return;
47  }
49  ->‪deflateKeys($aspects, $namespace, $route->‪getArguments());
50  $route->‪setAspects(‪$aspects);
51  }
52 
58  protected function ‪applyRequirements(‪Route $route, array $requirements, string $namespace = null)
59  {
60  $requirements = $this->‪getVariableProcessor()
61  ->‪deflateKeys($requirements, $namespace, $route->‪getArguments());
62  // only keep requirements that are actually part of the current route path
63  $requirements = $this->‪filterValuesByPathVariables($route, $requirements);
64  // Symfony's behavior on applying pattern for parameters just concerns values
65  // to be passed either to URL or to internal parameters - they are always the
66  // same, without any transformation.
67  //
68  // TYPO3 extends ("enhances") this behavior by making a difference between values
69  // for generation (resulting in a URL) and matching (resulting in query parameters)
70  // having the following implications and meaning:
71  //
72  // + since requirements in classic Symfony focus on parameters in URLs
73  // and aspects define a mapping between URL part (e.g. 'some-example-news')
74  // and the corresponding internal argument (e.g. 'tx_news_pi1[news]=123')
75  // + thus, the requirement definition cannot be used for resolving and generating
76  // a route at the same time (it would have to be e.g. `[\w_._]+` AND `\d+`)
77  //
78  // Symfony's default regular expression pattern `[^/]+` (see
79  // `RouteCompiler::compilePattern()`) has to be overridden with `.+` to
80  // allow URI parameters like `some-example-news/january` as well.
81  //
82  // Existing `requirements` for TYPO3 route enhancers are not modified, only those
83  // that are not defined and would use Symfony's default pattern.
84  $requirements = $this->‪defineValuesByAspect($route, $requirements, '.+');
85  $route->setRequirements($requirements);
86  }
87 
99  protected function ‪filterValuesByPathVariables(‪Route $route, array $values): array
100  {
101  return array_intersect_key(
102  $values,
103  array_flip($route->compile()->getPathVariables())
104  );
105  }
106 
116  protected function ‪overrideValuesByAspect(‪Route $route, array $values, string $targetValue): array
117  {
118  foreach (array_keys($route->‪getAspects()) as $variableName) {
119  $values[$variableName] = $targetValue;
120  }
121  return $values;
122  }
123 
133  protected function ‪defineValuesByAspect(‪Route $route, array $values, string $targetValue): array
134  {
135  foreach (array_keys($route->‪getAspects()) as $variableName) {
136  if (isset($values[$variableName])) {
137  continue;
138  }
139  $values[$variableName] = $targetValue;
140  }
141  return $values;
142  }
143 
153  protected function ‪modifyRoutePath(string $routePath): string
154  {
155  $substitutes = [];
156  foreach ($this->aspects as $variableName => $aspect) {
157  if (!$aspect instanceof ‪ModifiableAspectInterface) {
158  continue;
159  }
160  $value = $aspect->modify();
161  if ($value !== null) {
162  $substitutes['{' . $variableName . '}'] = $value;
163  $substitutes['{!' . $variableName . '}'] = $value;
164  }
165  }
166  return str_replace(
167  array_keys($substitutes),
168  array_values($substitutes),
169  $routePath
170  );
171  }
172 
180  protected function ‪resolveType(‪Route $route, array &$remainingQueryParameters): string
181  {
182  $type = $remainingQueryParameters['type'] ?? 0;
183  $decoratedParameters = $route->getOption('_decoratedParameters');
184  if (isset($decoratedParameters['type'])) {
185  $type = $decoratedParameters['type'];
186  unset($decoratedParameters['type']);
187  $remainingQueryParameters = array_replace_recursive(
188  $remainingQueryParameters,
189  $decoratedParameters
190  );
191  }
192  return (string)$type;
193  }
194 
198  protected function ‪getVariableProcessor(): ‪VariableProcessor
199  {
200  if (isset($this->variableProcessor)) {
202  }
203  return $this->variableProcessor = new ‪VariableProcessor();
204  }
205 
209  public function ‪setAspects(array ‪$aspects): void
210  {
211  $this->aspects = ‪$aspects;
212  }
213 
217  public function ‪getAspects(): array
218  {
219  return ‪$this->aspects;
220  }
221 }
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\modifyRoutePath
‪string modifyRoutePath(string $routePath)
Definition: AbstractEnhancer.php:151
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\getAspects
‪getAspects()
Definition: AbstractEnhancer.php:215
‪TYPO3\CMS\Core\Routing\Aspect\AspectInterface
Definition: AspectInterface.php:23
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\filterValuesByPathVariables
‪array filterValuesByPathVariables(Route $route, array $values)
Definition: AbstractEnhancer.php:97
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\applyRequirements
‪applyRequirements(Route $route, array $requirements, string $namespace=null)
Definition: AbstractEnhancer.php:56
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor
Definition: VariableProcessor.php:23
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\$variableProcessor
‪VariableProcessor $variableProcessor
Definition: AbstractEnhancer.php:34
‪TYPO3\CMS\Core\Routing\Route\getArguments
‪array getArguments()
Definition: Route.php:60
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\defineValuesByAspect
‪array defineValuesByAspect(Route $route, array $values, string $targetValue)
Definition: AbstractEnhancer.php:131
‪TYPO3\CMS\Core\Routing\Route\setAspects
‪$this setAspects(array $aspects)
Definition: Route.php:110
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\getVariableProcessor
‪VariableProcessor getVariableProcessor()
Definition: AbstractEnhancer.php:196
‪TYPO3\CMS\Core\Routing\Enhancer
Definition: AbstractEnhancer.php:4
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor\deflateKeys
‪array deflateKeys(array $items, string $namespace=null, array $arguments=[], bool $hash=true)
Definition: VariableProcessor.php:232
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer
Definition: AbstractEnhancer.php:27
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\applyRouteAspects
‪applyRouteAspects(Route $route, array $aspects, string $namespace=null)
Definition: AbstractEnhancer.php:41
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\resolveType
‪string resolveType(Route $route, array &$remainingQueryParameters)
Definition: AbstractEnhancer.php:178
‪TYPO3\CMS\Core\Routing\Route
Definition: Route.php:31
‪TYPO3\CMS\Core\Routing\Route\getAspects
‪array getAspects()
Definition: Route.php:97
‪TYPO3\CMS\Core\Routing\Aspect\ModifiableAspectInterface
Definition: ModifiableAspectInterface.php:24
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\setAspects
‪setAspects(array $aspects)
Definition: AbstractEnhancer.php:207
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\$aspects
‪AspectInterface[] $aspects
Definition: AbstractEnhancer.php:30
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\overrideValuesByAspect
‪array overrideValuesByAspect(Route $route, array $values, string $targetValue)
Definition: AbstractEnhancer.php:114
‪TYPO3\CMS\Core\Routing\Enhancer\EnhancerInterface
Definition: EnhancerInterface.php:26