‪TYPO3CMS  10.4
PluginEnhancer.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 
25 
43 {
47  protected ‪$configuration;
48 
52  protected ‪$namespace;
53 
54  public function ‪__construct(array ‪$configuration)
55  {
56  $this->configuration = ‪$configuration;
57  $this->namespace = $this->configuration['namespace'] ?? '';
58  }
59 
63  public function ‪buildResult(‪Route $route, array $results, array $remainingQueryParameters = []): ‪PageArguments
64  {
66  // determine those parameters that have been processed
67  $parameters = array_intersect_key(
68  $results,
69  array_flip($route->compile()->getPathVariables())
70  );
71  // strip of those that where not processed (internals like _route, etc.)
72  $internals = array_diff_key($results, $parameters);
73  $matchedVariableNames = array_keys($parameters);
74 
75  $staticMappers = $route->‪filterAspects([StaticMappableAspectInterface::class], $matchedVariableNames);
76  $dynamicCandidates = array_diff_key($parameters, $staticMappers);
77 
78  // all route arguments
79  $routeArguments = $this->‪inflateParameters($parameters, $internals);
80  // dynamic arguments, that don't have a static mapper
81  $dynamicArguments = ‪$variableProcessor
82  ->‪inflateNamespaceParameters($dynamicCandidates, $this->namespace);
83  // route arguments, that don't appear in dynamic arguments
84  $staticArguments = ‪ArrayUtility::arrayDiffKeyRecursive($routeArguments, $dynamicArguments);
85 
86  $page = $route->getOption('_page');
87  $pageId = (int)(isset($page['t3ver_oid']) && $page['t3ver_oid'] > 0 ? $page['t3ver_oid'] : $page['uid']);
88  $pageId = (int)($page['l10n_parent'] > 0 ? $page['l10n_parent'] : $pageId);
89  // See PageSlugCandidateProvider where this is added.
90  if ($page['MPvar'] ?? '') {
91  $routeArguments['MP'] = $page['MPvar'];
92  }
93  $type = $this->‪resolveType($route, $remainingQueryParameters);
94  return new ‪PageArguments($pageId, $type, $routeArguments, $staticArguments, $remainingQueryParameters);
95  }
96 
100  public function ‪enhanceForMatching(RouteCollection $collection): void
101  {
103  $defaultPageRoute = $collection->get('default');
104  $variant = $this->‪getVariant($defaultPageRoute, $this->configuration);
105  $collection->add('enhancer_' . $this->namespace . spl_object_hash($variant), $variant);
106  }
107 
115  protected function ‪getVariant(Route $defaultPageRoute, array ‪$configuration): Route
116  {
117  $arguments = ‪$configuration['_arguments'] ?? [];
118  unset(‪$configuration['_arguments']);
119 
121  $routePath = $this->‪modifyRoutePath($configuration['routePath']);
122  $routePath = ‪$variableProcessor->‪deflateRoutePath($routePath, $this->namespace, $arguments);
123  $variant = clone $defaultPageRoute;
124  $variant->setPath(rtrim($variant->getPath(), '/') . '/' . ltrim($routePath, '/'));
125  $variant->addOptions(['_enhancer' => $this, '_arguments' => $arguments]);
126  $defaults = ‪$variableProcessor->‪deflateKeys($this->configuration['defaults'] ?? [], $this->namespace, $arguments);
127  // only keep `defaults` that are actually used in `routePath`
128  $variant->setDefaults($this->‪filterValuesByPathVariables($variant, $defaults));
129  $this->‪applyRouteAspects($variant, $this->aspects ?? [], $this->namespace);
130  $this->‪applyRequirements($variant, $this->configuration['requirements'] ?? [], $this->namespace);
131  return $variant;
132  }
133 
137  public function ‪enhanceForGeneration(RouteCollection $collection, array $parameters): void
138  {
139  // No parameter for this namespace given, so this route does not fit the requirements
140  if (!is_array($parameters[$this->namespace])) {
141  return;
142  }
144  $defaultPageRoute = $collection->get('default');
145  $variant = $this->‪getVariant($defaultPageRoute, $this->configuration);
146  $compiledRoute = $variant->compile();
147  // contains all given parameters, even if not used as variables in route
148  $deflatedParameters = $this->‪deflateParameters($variant, $parameters);
149  $variables = array_flip($compiledRoute->getPathVariables());
150  $mergedParams = array_replace($variant->getDefaults(), $deflatedParameters);
151  // all params must be given, otherwise we exclude this variant
152  if ($variables === [] || array_diff_key($variables, $mergedParams) !== []) {
153  return;
154  }
155  $variant->addOptions(['deflatedParameters' => $deflatedParameters]);
156  $collection->add('enhancer_' . $this->namespace . spl_object_hash($variant), $variant);
157  }
158 
165  protected function ‪getNamespacedRequirements(): array
166  {
167  trigger_error('PluginEnhancer::getNamespacedRequirements will be removed in TYPO3 v11.0. Use AbstractEnhancer::applyRequirements() instead.', E_USER_DEPRECATED);
168  $requirements = [];
169  foreach ($this->configuration['requirements'] ?? [] as $name => $value) {
170  $requirements[$this->namespace . '_' . $name] = $value;
171  }
172  return $requirements;
173  }
174 
180  protected function ‪deflateParameters(Route $route, array $parameters): array
181  {
183  $parameters,
184  $this->namespace,
185  $route->getArguments()
186  );
187  }
188 
194  public function ‪inflateParameters(array $parameters, array $internals = []): array
195  {
196  return $this->‪getVariableProcessor()
197  ->‪inflateNamespaceParameters($parameters, $this->namespace);
198  }
199 }
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\modifyRoutePath
‪string modifyRoutePath(string $routePath)
Definition: AbstractEnhancer.php:152
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor\deflateRoutePath
‪string deflateRoutePath(string $routePath, string $namespace=null, array $arguments=[])
Definition: VariableProcessor.php:115
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\getNamespacedRequirements
‪array getNamespacedRequirements()
Definition: PluginEnhancer.php:163
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\filterValuesByPathVariables
‪array filterValuesByPathVariables(Route $route, array $values)
Definition: AbstractEnhancer.php:98
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\applyRequirements
‪applyRequirements(Route $route, array $requirements, string $namespace=null)
Definition: AbstractEnhancer.php:57
‪TYPO3\CMS\Core\Routing\Enhancer\RoutingEnhancerInterface
Definition: RoutingEnhancerInterface.php:26
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\__construct
‪__construct(array $configuration)
Definition: PluginEnhancer.php:52
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\$variableProcessor
‪VariableProcessor $variableProcessor
Definition: AbstractEnhancer.php:35
‪TYPO3\CMS\Core\Routing\RouteCollection
Definition: RouteCollection.php:28
‪TYPO3\CMS\Core\Routing\Route\getArguments
‪array getArguments()
Definition: Route.php:61
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\enhanceForGeneration
‪enhanceForGeneration(RouteCollection $collection, array $parameters)
Definition: PluginEnhancer.php:135
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\inflateParameters
‪array inflateParameters(array $parameters, array $internals=[])
Definition: PluginEnhancer.php:192
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\getVariableProcessor
‪VariableProcessor getVariableProcessor()
Definition: AbstractEnhancer.php:197
‪TYPO3\CMS\Core\Routing\Enhancer
Definition: AbstractEnhancer.php:18
‪TYPO3\CMS\Core\Routing\Enhancer\ResultingInterface
Definition: ResultingInterface.php:28
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor\deflateNamespaceParameters
‪array deflateNamespaceParameters(array $parameters, string $namespace, array $arguments=[])
Definition: VariableProcessor.php:161
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor\deflateKeys
‪array deflateKeys(array $items, string $namespace=null, array $arguments=[], bool $hash=true)
Definition: VariableProcessor.php:233
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer
Definition: PluginEnhancer.php:43
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\getVariant
‪Route getVariant(Route $defaultPageRoute, array $configuration)
Definition: PluginEnhancer.php:113
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer
Definition: AbstractEnhancer.php:28
‪TYPO3\CMS\Core\Utility\ArrayUtility\arrayDiffKeyRecursive
‪static array arrayDiffKeyRecursive(array $array1, array $array2)
Definition: ArrayUtility.php:768
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\applyRouteAspects
‪applyRouteAspects(Route $route, array $aspects, string $namespace=null)
Definition: AbstractEnhancer.php:42
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\resolveType
‪string resolveType(Route $route, array &$remainingQueryParameters)
Definition: AbstractEnhancer.php:179
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\Routing\Route
Definition: Route.php:32
‪TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface
Definition: StaticMappableAspectInterface.php:24
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\buildResult
‪buildResult(Route $route, array $results, array $remainingQueryParameters=[])
Definition: PluginEnhancer.php:61
‪TYPO3\CMS\Core\Routing\Enhancer\InflatableEnhancerInterface
Definition: InflatableEnhancerInterface.php:24
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\$namespace
‪string $namespace
Definition: PluginEnhancer.php:50
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\deflateParameters
‪array deflateParameters(Route $route, array $parameters)
Definition: PluginEnhancer.php:178
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor\inflateNamespaceParameters
‪array inflateNamespaceParameters(array $parameters, string $namespace, array $arguments=[])
Definition: VariableProcessor.php:183
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\$configuration
‪array $configuration
Definition: PluginEnhancer.php:46
‪TYPO3\CMS\Core\Routing\Route\filterAspects
‪AspectInterface[] filterAspects(array $classNames, array $variableNames=[])
Definition: Route.php:163
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\enhanceForMatching
‪enhanceForMatching(RouteCollection $collection)
Definition: PluginEnhancer.php:98