[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Routing / Route.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Routing;
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
19 use Symfony\Component\Routing\CompiledRoute;
20 use Symfony\Component\Routing\Route as SymfonyRoute;
21 use TYPO3\CMS\Core\Routing\Aspect\AspectInterface;
22 use TYPO3\CMS\Core\Routing\Enhancer\EnhancerInterface;
23
24 /**
25 * TYPO3's route is built on top of Symfony's route with some special handling
26 * of "Aspects" built on top of a route
27 *
28 * @internal as this is tightly coupled to Symfony's Routing and we try to encapsulate this, please note that this might change if we change the under-the-hood implementation.
29 */
30 class Route extends SymfonyRoute
31 {
32 /**
33 * @return array
34 * @var CompiledRoute|null
35 */
36 protected $compiled;
37
38 /**
39 * @var AspectInterface[]
40 */
41 protected $aspects = [];
42
43 public function __construct(
44 string $path,
45 array $defaults = [],
46 array $requirements = [],
47 array $options = [],
48 ?string $host = '',
49 $schemes = [],
50 $methods = [],
51 ?string $condition = '',
52 array $aspects = []
53 ) {
54 parent::__construct($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
55 $this->setAspects($aspects);
56 }
57
58 /**
59 * @return array
60 * @todo '_arguments' are added implicitly, make it explicit in enhancers
61 */
62 public function getArguments(): array
63 {
64 return $this->getOption('_arguments') ?? [];
65 }
66
67 /**
68 * @return EnhancerInterface|null
69 */
70 public function getEnhancer(): ?EnhancerInterface
71 {
72 return $this->getOption('_enhancer') ?? null;
73 }
74
75 /**
76 * Returns all aspects.
77 *
78 * @return array The aspects
79 */
80 public function getAspects(): array
81 {
82 return $this->aspects;
83 }
84
85 /**
86 * Sets the aspects and removes existing ones.
87 *
88 * This method implements a fluent interface.
89 *
90 * @param array $aspects The aspects
91 * @return $this
92 */
93 public function setAspects(array $aspects): self
94 {
95 $this->aspects = [];
96 return $this->addAspects($aspects);
97 }
98
99 /**
100 * Adds aspects to the existing maps.
101 *
102 * This method implements a fluent interface.
103 *
104 * @param array $aspects The aspects
105 * @return $this
106 */
107 public function addAspects(array $aspects): self
108 {
109 foreach ($aspects as $key => $aspect) {
110 if (isset($this->aspects[$key])) {
111 throw new \OverflowException(
112 sprintf('Cannot override aspect %s', $key),
113 1538326791
114 );
115 }
116 $this->aspects[$key] = $aspect;
117 }
118 $this->compiled = null;
119 return $this;
120 }
121
122 /**
123 * Returns the aspect for the given key.
124 *
125 * @param string $key The key
126 * @return AspectInterface|null The regex or null when not given
127 */
128 public function getAspect(string $key): ?AspectInterface
129 {
130 return $this->aspects[$key] ?? null;
131 }
132
133 /**
134 * Checks if an aspect is set for the given key.
135 *
136 * @param string $key A variable name
137 * @return bool true if an aspect is specified, false otherwise
138 */
139 public function hasAspect(string $key): bool
140 {
141 return array_key_exists($key, $this->aspects);
142 }
143
144 /**
145 * Sets an aspect for the given key.
146 *
147 * @param string $key The key
148 * @param AspectInterface $aspect
149 * @return $this
150 */
151 public function setAspect(string $key, AspectInterface $aspect): self
152 {
153 $this->aspects[$key] = $aspect;
154 $this->compiled = null;
155 return $this;
156 }
157
158 /**
159 * @param string[] $classNames All (logical AND) class names that must match
160 * (including interfaces, abstract classes and traits)
161 * @param string[] $variableNames Variable names to be filtered
162 * @return AspectInterface[]
163 */
164 public function filterAspects(array $classNames, array $variableNames = []): array
165 {
166 $aspects = $this->aspects;
167 if (empty($classNames) && empty($variableNames)) {
168 return $aspects;
169 }
170 if (!empty($variableNames)) {
171 $aspects = array_filter(
172 $this->aspects,
173 function (string $variableName) use ($variableNames) {
174 return in_array($variableName, $variableNames, true);
175 },
176 ARRAY_FILTER_USE_KEY
177 );
178 }
179 return array_filter(
180 $aspects,
181 function (AspectInterface $aspect) use ($classNames) {
182 $uses = class_uses($aspect);
183 foreach ($classNames as $className) {
184 if (!is_a($aspect, $className)
185 && !in_array($className, $uses, true)
186 ) {
187 return false;
188 }
189 }
190 return true;
191 }
192 );
193 }
194 }