‪TYPO3CMS  9.5
SimpleEnhancer.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 
37 {
41  protected ‪$configuration;
42 
43  public function ‪__construct(array ‪$configuration)
44  {
45  $this->configuration = ‪$configuration;
46  }
47 
51  public function ‪buildResult(‪Route $route, array $results, array $remainingQueryParameters = []): ‪PageArguments
52  {
53  // determine those parameters that have been processed
54  $parameters = array_intersect_key(
55  $results,
56  array_flip($route->compile()->getPathVariables())
57  );
58  // strip of those that where not processed (internals like _route, etc.)
59  $internals = array_diff_key($results, $parameters);
60  $matchedVariableNames = array_keys($parameters);
61 
62  $staticMappers = $route->‪filterAspects([StaticMappableAspectInterface::class], $matchedVariableNames);
63  $dynamicCandidates = array_diff_key($parameters, $staticMappers);
64 
65  // all route arguments
66  $routeArguments = $this->‪inflateParameters($parameters, $internals);
67  // dynamic arguments, that don't have a static mapper
68  $dynamicArguments = $this->‪inflateParameters($dynamicCandidates);
69  // route arguments, that don't appear in dynamic arguments
70  $staticArguments = ‪ArrayUtility::arrayDiffAssocRecursive($routeArguments, $dynamicArguments);
71 
72  $page = $route->getOption('_page');
73  $pageId = (int)($page['l10n_parent'] > 0 ? $page['l10n_parent'] : $page['uid']);
74  // See PageSlugCandidateProvider where this is added.
75  if ($page['MPvar'] ?? '') {
76  $routeArguments['MP'] = $page['MPvar'];
77  }
78  $type = $this->‪resolveType($route, $remainingQueryParameters);
79  return new ‪PageArguments($pageId, $type, $routeArguments, $staticArguments, $remainingQueryParameters);
80  }
81 
85  public function ‪enhanceForMatching(RouteCollection $collection): void
86  {
88  $defaultPageRoute = $collection->get('default');
89  $variant = $this->‪getVariant($defaultPageRoute, $this->configuration);
90  $collection->add('enhancer_' . spl_object_hash($variant), $variant);
91  }
92 
100  protected function ‪getVariant(Route $defaultPageRoute, array ‪$configuration): Route
101  {
102  $arguments = ‪$configuration['_arguments'] ?? [];
103  unset(‪$configuration['_arguments']);
104 
106  $routePath = $this->‪modifyRoutePath($configuration['routePath']);
107  $routePath = ‪$variableProcessor->‪deflateRoutePath($routePath, null, $arguments);
108  $variant = clone $defaultPageRoute;
109  $variant->setPath(rtrim($variant->getPath(), '/') . '/' . ltrim($routePath, '/'));
110  $variant->addOptions(['_enhancer' => $this, '_arguments' => $arguments]);
111  $defaults = ‪$variableProcessor->‪deflateKeys($this->configuration['defaults'] ?? [], null, $arguments);
112  // only keep `defaults` that are actually used in `routePath`
113  $variant->setDefaults($this->‪filterValuesByPathVariables($variant, $defaults));
114  $this->‪applyRouteAspects($variant, $this->aspects ?? []);
115  $this->‪applyRequirements($variant, $this->configuration['requirements'] ?? []);
116  return $variant;
117  }
118 
122  public function ‪enhanceForGeneration(RouteCollection $collection, array $parameters): void
123  {
125  $defaultPageRoute = $collection->get('default');
126  $variant = $this->‪getVariant($defaultPageRoute, $this->configuration);
127  $compiledRoute = $variant->compile();
128  // contains all given parameters, even if not used as variables in route
129  $deflatedParameters = $this->‪getVariableProcessor()->‪deflateParameters($parameters, $variant->getArguments());
130  $variables = array_flip($compiledRoute->getPathVariables());
131  $mergedParams = array_replace($variant->getDefaults(), $deflatedParameters);
132  // all params must be given, otherwise we exclude this variant
133  if ($variables === [] || array_diff_key($variables, $mergedParams) !== []) {
134  return;
135  }
136  $variant->addOptions(['deflatedParameters' => $deflatedParameters]);
137  $collection->add('enhancer_' . spl_object_hash($variant), $variant);
138  }
139 
140  public function ‪inflateParameters(array $parameters, array $internals = []): array
141  {
142  return $this->‪getVariableProcessor()->‪inflateParameters($parameters, $internals);
143  }
144 }
‪TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer\modifyRoutePath
‪string modifyRoutePath(string $routePath)
Definition: AbstractEnhancer.php:151
‪TYPO3\CMS\Core\Routing\Enhancer\SimpleEnhancer\buildResult
‪buildResult(Route $route, array $results, array $remainingQueryParameters=[])
Definition: SimpleEnhancer.php:50
‪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\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\SimpleEnhancer\__construct
‪__construct(array $configuration)
Definition: SimpleEnhancer.php:42
‪TYPO3\CMS\Core\Routing\Enhancer\SimpleEnhancer\enhanceForGeneration
‪enhanceForGeneration(RouteCollection $collection, array $parameters)
Definition: SimpleEnhancer.php:121
‪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\Enhancer\SimpleEnhancer\getVariant
‪Route getVariant(Route $defaultPageRoute, array $configuration)
Definition: SimpleEnhancer.php:99
‪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\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\SimpleEnhancer\$configuration
‪array $configuration
Definition: SimpleEnhancer.php:40
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor\inflateParameters
‪array inflateParameters(array $parameters, array $arguments=[])
Definition: VariableProcessor.php:216
‪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\Enhancer\SimpleEnhancer\enhanceForMatching
‪enhanceForMatching(RouteCollection $collection)
Definition: SimpleEnhancer.php:84
‪TYPO3\CMS\Core\Routing\Enhancer\SimpleEnhancer\inflateParameters
‪inflateParameters(array $parameters, array $internals=[])
Definition: SimpleEnhancer.php:139
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Core\Routing\Route
Definition: Route.php:31
‪TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor\deflateParameters
‪array deflateParameters(array $parameters, array $arguments=[])
Definition: VariableProcessor.php:203
‪TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface
Definition: StaticMappableAspectInterface.php:23
‪TYPO3\CMS\Core\Routing\Enhancer\InflatableEnhancerInterface
Definition: InflatableEnhancerInterface.php:23
‪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\SimpleEnhancer
Definition: SimpleEnhancer.php:37