‪TYPO3CMS  9.5
PluginEnhancer.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 
24 
42 {
46  protected ‪$configuration;
47 
51  protected ‪$namespace;
52 
53  public function ‪__construct(array ‪$configuration)
54  {
55  $this->configuration = ‪$configuration;
56  $this->namespace = $this->configuration['namespace'] ?? '';
57  }
58 
62  public function ‪buildResult(‪Route $route, array $results, array $remainingQueryParameters = []): ‪PageArguments
63  {
65  // determine those parameters that have been processed
66  $parameters = array_intersect_key(
67  $results,
68  array_flip($route->compile()->getPathVariables())
69  );
70  // strip of those that where not processed (internals like _route, etc.)
71  $internals = array_diff_key($results, $parameters);
72  $matchedVariableNames = array_keys($parameters);
73 
74  $staticMappers = $route->‪filterAspects([StaticMappableAspectInterface::class], $matchedVariableNames);
75  $dynamicCandidates = array_diff_key($parameters, $staticMappers);
76 
77  // all route arguments
78  $routeArguments = $this->‪inflateParameters($parameters, $internals);
79  // dynamic arguments, that don't have a static mapper
80  $dynamicArguments = ‪$variableProcessor
81  ->‪inflateNamespaceParameters($dynamicCandidates, $this->namespace);
82  // route arguments, that don't appear in dynamic arguments
83  $staticArguments = ‪ArrayUtility::arrayDiffAssocRecursive($routeArguments, $dynamicArguments);
84 
85  $page = $route->getOption('_page');
86  $pageId = (int)($page['l10n_parent'] > 0 ? $page['l10n_parent'] : $page['uid']);
87  // See PageSlugCandidateProvider where this is added.
88  if ($page['MPvar'] ?? '') {
89  $routeArguments['MP'] = $page['MPvar'];
90  }
91  $type = $this->‪resolveType($route, $remainingQueryParameters);
92  return new ‪PageArguments($pageId, $type, $routeArguments, $staticArguments, $remainingQueryParameters);
93  }
94 
98  public function ‪enhanceForMatching(RouteCollection $collection): void
99  {
101  $defaultPageRoute = $collection->get('default');
102  $variant = $this->‪getVariant($defaultPageRoute, $this->configuration);
103  $collection->add('enhancer_' . $this->namespace . spl_object_hash($variant), $variant);
104  }
105 
113  protected function ‪getVariant(Route $defaultPageRoute, array ‪$configuration): Route
114  {
115  $arguments = ‪$configuration['_arguments'] ?? [];
116  unset(‪$configuration['_arguments']);
117 
119  $routePath = $this->‪modifyRoutePath($configuration['routePath']);
120  $routePath = ‪$variableProcessor->‪deflateRoutePath($routePath, $this->namespace, $arguments);
121  $variant = clone $defaultPageRoute;
122  $variant->setPath(rtrim($variant->getPath(), '/') . '/' . ltrim($routePath, '/'));
123  $variant->addOptions(['_enhancer' => $this, '_arguments' => $arguments]);
124  $defaults = ‪$variableProcessor->‪deflateKeys($this->configuration['defaults'] ?? [], $this->namespace, $arguments);
125  // only keep `defaults` that are actually used in `routePath`
126  $variant->setDefaults($this->‪filterValuesByPathVariables($variant, $defaults));
127  $this->‪applyRouteAspects($variant, $this->aspects ?? [], $this->namespace);
128  $this->‪applyRequirements($variant, $this->configuration['requirements'] ?? [], $this->namespace);
129  return $variant;
130  }
131 
135  public function ‪enhanceForGeneration(RouteCollection $collection, array $parameters): void
136  {
137  // No parameter for this namespace given, so this route does not fit the requirements
138  if (!is_array($parameters[$this->namespace])) {
139  return;
140  }
142  $defaultPageRoute = $collection->get('default');
143  $variant = $this->‪getVariant($defaultPageRoute, $this->configuration);
144  $compiledRoute = $variant->compile();
145  // contains all given parameters, even if not used as variables in route
146  $deflatedParameters = $this->‪deflateParameters($variant, $parameters);
147  $variables = array_flip($compiledRoute->getPathVariables());
148  $mergedParams = array_replace($variant->getDefaults(), $deflatedParameters);
149  // all params must be given, otherwise we exclude this variant
150  if ($variables === [] || array_diff_key($variables, $mergedParams) !== []) {
151  return;
152  }
153  $variant->addOptions(['deflatedParameters' => $deflatedParameters]);
154  $collection->add('enhancer_' . $this->namespace . spl_object_hash($variant), $variant);
155  }
156 
163  protected function ‪getNamespacedRequirements(): array
164  {
165  $requirements = [];
166  foreach ($this->configuration['requirements'] ?? [] as $name => $value) {
167  $requirements[$this->namespace . '_' . $name] = $value;
168  }
169  return $requirements;
170  }
171 
177  protected function ‪deflateParameters(Route $route, array $parameters): array
178  {
180  $parameters,
181  $this->namespace,
182  $route->getArguments()
183  );
184  }
185 
191  public function ‪inflateParameters(array $parameters, array $internals = []): array
192  {
193  return $this->‪getVariableProcessor()
194  ->‪inflateNamespaceParameters($parameters, $this->namespace);
195  }
196 }
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\modifyRoutePath
‪string modifyRoutePath(string $routePath)
Definition: AbstractEnhancer.php:151
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor\deflateRoutePath
‪string deflateRoutePath(string $routePath, string $namespace=null, array $arguments=[])
Definition: VariableProcessor.php:114
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:25
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\getNamespacedRequirements
‪array getNamespacedRequirements()
Definition: PluginEnhancer.php:161
‪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\RoutingEnhancerInterface
Definition: RoutingEnhancerInterface.php:25
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\__construct
‪__construct(array $configuration)
Definition: PluginEnhancer.php:51
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\$variableProcessor
‪VariableProcessor $variableProcessor
Definition: AbstractEnhancer.php:34
‪TYPO3\CMS\Core\Routing\RouteCollection
Definition: RouteCollection.php:27
‪TYPO3\CMS\Core\Routing\Route\getArguments
‪array getArguments()
Definition: Route.php:60
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\enhanceForGeneration
‪enhanceForGeneration(RouteCollection $collection, array $parameters)
Definition: PluginEnhancer.php:133
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\inflateParameters
‪array inflateParameters(array $parameters, array $internals=[])
Definition: PluginEnhancer.php:189
‪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\ResultingInterface
Definition: ResultingInterface.php:27
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor\deflateNamespaceParameters
‪array deflateNamespaceParameters(array $parameters, string $namespace, array $arguments=[])
Definition: VariableProcessor.php:160
‪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\PluginEnhancer
Definition: PluginEnhancer.php:42
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\getVariant
‪Route getVariant(Route $defaultPageRoute, array $configuration)
Definition: PluginEnhancer.php:111
‪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\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Core\Routing\Route
Definition: Route.php:31
‪TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface
Definition: StaticMappableAspectInterface.php:23
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\buildResult
‪buildResult(Route $route, array $results, array $remainingQueryParameters=[])
Definition: PluginEnhancer.php:60
‪TYPO3\CMS\Core\Routing\Enhancer\InflatableEnhancerInterface
Definition: InflatableEnhancerInterface.php:23
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\$namespace
‪string $namespace
Definition: PluginEnhancer.php:49
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\deflateParameters
‪array deflateParameters(Route $route, array $parameters)
Definition: PluginEnhancer.php:175
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor\inflateNamespaceParameters
‪array inflateNamespaceParameters(array $parameters, string $namespace, array $arguments=[])
Definition: VariableProcessor.php:182
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\$configuration
‪array $configuration
Definition: PluginEnhancer.php:45
‪TYPO3\CMS\Core\Utility\ArrayUtility\arrayDiffAssocRecursive
‪static array arrayDiffAssocRecursive(array $array1, array $array2)
Definition: ArrayUtility.php:728
‪TYPO3\CMS\Core\Routing\Route\filterAspects
‪AspectInterface[] filterAspects(array $classNames, array $variableNames=[])
Definition: Route.php:181
‪TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer\enhanceForMatching
‪enhanceForMatching(RouteCollection $collection)
Definition: PluginEnhancer.php:96