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