‪TYPO3CMS  10.4
YamlPlaceholderGuard.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
27 {
31  protected $existingConfiguration;
32 
36  protected $fragmentSplitter;
37 
38  public function __construct(array $existingConfiguration)
39  {
40  $this->‪existingConfiguration = $existingConfiguration;
41  ‪$fragmentPattern = GeneralUtility::makeInstance(
42  StringFragmentPattern::class,
45  );
46  $this->‪fragmentSplitter = GeneralUtility::makeInstance(
47  StringFragmentSplitter::class,
49  );
50  }
51 
55  public function ‪process(array ‪$modified): array
56  {
57  return $this->‪protectPlaceholders($this->‪existingConfiguration, $modified);
58  }
59 
70  protected function ‪protectPlaceholders(array $current, array ‪$modified, array $steps = []): array
71  {
72  foreach (‪$modified as $key => $value) {
73  $currentSteps = array_merge($steps, [$key]);
74  if (is_array($value)) {
75  ‪$modified[$key] = $this->‪protectPlaceholders(
76  $current[$key] ?? [],
77  $value,
78  $currentSteps
79  );
80  } elseif (is_string($value)) {
82  $newFragments = $this->‪fragmentSplitter->split($value, $splitFlags);
83  if (is_string($current[$key] ?? null)) {
84  $currentFragments = $this->‪fragmentSplitter->split($current[$key] ?? '', $splitFlags);
85  } else {
86  $currentFragments = null;
87  }
88  // in case there are new fragments (at least one matching the pattern)
89  if ($newFragments !== null) {
90  // compares differences in `expression` fragments only
91  $differences = $currentFragments === null
92  ? $newFragments->withOnlyType(‪StringFragmentSplitter::TYPE_EXPRESSION)
93  : $newFragments->withOnlyType(‪StringFragmentSplitter::TYPE_EXPRESSION)
94  ->diff($currentFragments->withOnlyType(‪StringFragmentSplitter::TYPE_EXPRESSION));
95  if (count($differences) > 0) {
96  throw new YamlPlaceholderException(
97  sprintf(
98  'Introducing placeholder%s %s for %s is not allowed',
99  count($differences) !== 1 ? 's' : '',
100  implode(', ', $differences->getFragments()),
101  implode('.', $currentSteps)
102  ),
103  1651690534
104  );
105  }
106  }
107  }
108  }
109  return ‪$modified;
110  }
111 }
‪TYPO3\CMS\Core\Configuration\Loader\YamlPlaceholderGuard\process
‪process(array $modified)
Definition: YamlPlaceholderGuard.php:53
‪TYPO3\CMS\Core\Configuration\Loader\YamlPlaceholderGuard\$fragmentPattern
‪$fragmentPattern
Definition: YamlPlaceholderGuard.php:39
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader\PATTERN_PARTS
‪const PATTERN_PARTS
Definition: YamlFileLoader.php:51
‪TYPO3\CMS\Core\Configuration\Loader\YamlPlaceholderGuard\fragmentSplitter
‪$this fragmentSplitter
Definition: YamlPlaceholderGuard.php:44
‪TYPO3\CMS\Core\Configuration\Loader\Exception\YamlPlaceholderException
Definition: YamlPlaceholderException.php:21
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter
Definition: StringFragmentSplitter.php:27
‪TYPO3\CMS\Core\Utility\String\StringFragmentPattern
Definition: StringFragmentPattern.php:24
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\TYPE_EXPRESSION
‪const TYPE_EXPRESSION
Definition: StringFragmentSplitter.php:36
‪TYPO3\CMS\Core\Configuration\Loader\YamlPlaceholderGuard\protectPlaceholders
‪array< string, protectPlaceholders(array $current, array $modified, array $steps=[]):array { foreach( $modified as $key=> $value) { $currentSteps=array_merge( $steps,[ $key]);if(is_array( $value)) { $modified[ $key]=$this-> protectPlaceholders( $current[$key] ??[], $value, $currentSteps)
‪TYPO3\CMS\Core\Configuration\Loader
‪TYPO3\CMS\Core\Configuration\Loader\YamlPlaceholderGuard\$modified
‪elseif(is_string($value)) return $modified
Definition: YamlPlaceholderGuard.php:78
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Configuration\Loader\YamlPlaceholderGuard\existingConfiguration
‪array< string, $existingConfiguration;protected StringFragmentSplitter $fragmentSplitter;public function __construct(array $existingConfiguration) { $this-> existingConfiguration
Definition: YamlPlaceholderGuard.php:38
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\FLAG_UNMATCHED_AS_NULL
‪const FLAG_UNMATCHED_AS_NULL
Definition: StringFragmentSplitter.php:42
‪TYPO3\CMS\Core\Configuration\Loader\YamlPlaceholderGuard
Definition: YamlPlaceholderGuard.php:27