‪TYPO3CMS  ‪main
CacheHashConfiguration.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 
35 {
36  public const ‪ASPECT_CACHED_PARAMETERS_WHITELIST = 'cachedParametersWhiteList';
37  public const ‪ASPECT_EXCLUDED_PARAMETERS = 'excludedParameters';
38  public const ‪ASPECT_EXCLUDED_PARAMETERS_IF_EMPTY = 'excludedParametersIfEmpty';
39  public const ‪ASPECT_REQUIRED_CACHE_HASH_PRESENCE_PARAMETERS = 'requireCacheHashPresenceParameters';
40 
41  protected const ‪PROPERTY_EXCLUDE_ALL_EMPTY_PARAMETERS = 'excludeAllEmptyParameters';
42  protected const ‪INDICATOR_STARTS_WITH = '^';
43  protected const ‪INDICATOR_CONTAINS = '~';
44  protected const ‪INDICATOR_EQUALS = '=';
45 
46  protected const ‪ALLOWED_INDICATORS = [
50  ];
51 
52  protected const ‪ALLOWED_PROPERTY_NAMES = [
58  ];
59 
63  protected ‪$configuration;
64 
68  protected ‪$data = [];
69 
70  public function ‪__construct(array ‪$configuration = null)
71  {
72  ‪$configuration = ‪$configuration ?? ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash'] ?? [];
73  $this->configuration = array_filter(‪$configuration, [$this, 'isAllowedProperty'], ARRAY_FILTER_USE_KEY);
74  $this->‪processConfiguration();
75  }
76 
88  public function ‪with(‪CacheHashConfiguration $other): self
89  {
90  $target = clone $this;
91  $target->configuration = array_merge($this->configuration, $other->configuration);
92  $target->processConfiguration();
93  return $target;
94  }
95 
96  public function ‪shallExcludeAllEmptyParameters(): bool
97  {
98  return !empty($this->configuration[self::PROPERTY_EXCLUDE_ALL_EMPTY_PARAMETERS]);
99  }
100 
101  public function ‪applies(string $aspect, string $value): bool
102  {
103  return $this->‪equals($aspect, $value)
104  || $this->‪contains($aspect, $value)
105  || $this->‪startsWith($aspect, $value);
106  }
107 
108  public function ‪equals(string $aspect, string $value): bool
109  {
110  ‪$data = $this->‪getData($aspect, self::INDICATOR_EQUALS);
111  return !empty(‪$data) && in_array($value, ‪$data, true);
112  }
113 
114  public function ‪startsWith(string $aspect, string $value): bool
115  {
116  ‪$data = $this->‪getData($aspect, self::INDICATOR_STARTS_WITH);
117  if (empty(‪$data)) {
118  return false;
119  }
120  foreach (‪$data as $item) {
121  if (str_starts_with($value, $item)) {
122  return true;
123  }
124  }
125  return false;
126  }
127 
128  public function ‪contains(string $aspect, string $value): bool
129  {
130  ‪$data = $this->‪getData($aspect, self::INDICATOR_CONTAINS);
131  if (empty(‪$data)) {
132  return false;
133  }
134  foreach (‪$data as $item) {
135  if (str_contains($value, $item)) {
136  return true;
137  }
138  }
139  return false;
140  }
141 
142  public function ‪hasData(string $aspect): bool
143  {
144  return !empty($this->data[$aspect]);
145  }
146 
147  protected function ‪getData(string $aspect, string $indicator): ?array
148  {
149  return $this->data[$aspect][$indicator] ?? null;
150  }
151 
152  protected function ‪defineData(string $aspect): void
153  {
154  if (empty($this->configuration[$aspect])) {
155  return;
156  }
157  if (!is_array($this->configuration[$aspect])) {
158  throw new \LogicException(
159  sprintf('Expected array value, got %s', gettype($this->configuration[$aspect])),
160  1580225311
161  );
162  }
163  ‪$data = [];
164  foreach ($this->configuration[$aspect] as $value) {
165  if (!is_scalar($value)) {
166  throw new \LogicException(
167  sprintf('Expected scalar value, got %s', gettype($value)),
168  1580225312
169  );
170  }
171  if ($value === '') {
172  continue;
173  }
174  $indicator = $value[0] ?? null;
175  // normalize value to be indicated
176  if (!in_array($indicator, self::ALLOWED_INDICATORS, true)) {
177  $indicator = ‪self::INDICATOR_EQUALS;
178  $value = self::INDICATOR_EQUALS . $value;
179  }
180  if (strlen((string)$value) === 1) {
181  throw new \LogicException(
182  sprintf('Empty value after %s indicator', $indicator),
183  1580225313
184  );
185  }
186  ‪$data[$indicator][] = substr((string)$value, 1);
187  }
188  if (!empty(‪$data)) {
189  $this->data[$aspect] = ‪$data;
190  }
191  }
192 
193  protected function ‪processConfiguration(): void
194  {
195  $this->data = [];
196  $this->‪defineData(self::ASPECT_CACHED_PARAMETERS_WHITELIST);
197  $this->‪defineData(self::ASPECT_EXCLUDED_PARAMETERS);
198  $this->‪defineData(self::ASPECT_EXCLUDED_PARAMETERS_IF_EMPTY);
199  $this->‪defineData(self::ASPECT_REQUIRED_CACHE_HASH_PRESENCE_PARAMETERS);
200  }
201 
202  protected function ‪isAllowedProperty(string $propertyName): bool
203  {
204  return in_array($propertyName, self::ALLOWED_PROPERTY_NAMES, true);
205  }
206 }
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\PROPERTY_EXCLUDE_ALL_EMPTY_PARAMETERS
‪const PROPERTY_EXCLUDE_ALL_EMPTY_PARAMETERS
Definition: CacheHashConfiguration.php:41
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\ALLOWED_INDICATORS
‪const ALLOWED_INDICATORS
Definition: CacheHashConfiguration.php:46
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\$data
‪array $data
Definition: CacheHashConfiguration.php:66
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\ASPECT_CACHED_PARAMETERS_WHITELIST
‪const ASPECT_CACHED_PARAMETERS_WHITELIST
Definition: CacheHashConfiguration.php:36
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\INDICATOR_STARTS_WITH
‪const INDICATOR_STARTS_WITH
Definition: CacheHashConfiguration.php:42
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\ALLOWED_PROPERTY_NAMES
‪const ALLOWED_PROPERTY_NAMES
Definition: CacheHashConfiguration.php:52
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\ASPECT_REQUIRED_CACHE_HASH_PRESENCE_PARAMETERS
‪const ASPECT_REQUIRED_CACHE_HASH_PRESENCE_PARAMETERS
Definition: CacheHashConfiguration.php:39
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\hasData
‪hasData(string $aspect)
Definition: CacheHashConfiguration.php:140
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\INDICATOR_EQUALS
‪const INDICATOR_EQUALS
Definition: CacheHashConfiguration.php:44
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\defineData
‪defineData(string $aspect)
Definition: CacheHashConfiguration.php:150
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\__construct
‪__construct(array $configuration=null)
Definition: CacheHashConfiguration.php:68
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\shallExcludeAllEmptyParameters
‪shallExcludeAllEmptyParameters()
Definition: CacheHashConfiguration.php:94
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\ASPECT_EXCLUDED_PARAMETERS
‪const ASPECT_EXCLUDED_PARAMETERS
Definition: CacheHashConfiguration.php:37
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\startsWith
‪startsWith(string $aspect, string $value)
Definition: CacheHashConfiguration.php:112
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration
Definition: CacheHashConfiguration.php:35
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\applies
‪applies(string $aspect, string $value)
Definition: CacheHashConfiguration.php:99
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\contains
‪contains(string $aspect, string $value)
Definition: CacheHashConfiguration.php:126
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\getData
‪getData(string $aspect, string $indicator)
Definition: CacheHashConfiguration.php:145
‪TYPO3\CMS\Frontend\Page
Definition: CacheHashCalculator.php:16
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\processConfiguration
‪processConfiguration()
Definition: CacheHashConfiguration.php:191
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\INDICATOR_CONTAINS
‪const INDICATOR_CONTAINS
Definition: CacheHashConfiguration.php:43
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\ASPECT_EXCLUDED_PARAMETERS_IF_EMPTY
‪const ASPECT_EXCLUDED_PARAMETERS_IF_EMPTY
Definition: CacheHashConfiguration.php:38
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\$configuration
‪array $configuration
Definition: CacheHashConfiguration.php:62
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\with
‪static with(CacheHashConfiguration $other)
Definition: CacheHashConfiguration.php:86
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\equals
‪equals(string $aspect, string $value)
Definition: CacheHashConfiguration.php:106
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\isAllowedProperty
‪isAllowedProperty(string $propertyName)
Definition: CacheHashConfiguration.php:200