‪TYPO3CMS  ‪main
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 {
29 
30  public function ‪__construct(protected array $existingConfiguration)
31  {
32  $fragmentPattern = GeneralUtility::makeInstance(
33  StringFragmentPattern::class,
36  );
37  $this->fragmentSplitter = GeneralUtility::makeInstance(
38  StringFragmentSplitter::class,
39  $fragmentPattern
40  );
41  }
42 
46  public function ‪process(array ‪$modified): array
47  {
48  return $this->‪protectPlaceholders($this->existingConfiguration, ‪$modified);
49  }
50 
61  protected function ‪protectPlaceholders(array $current, array ‪$modified, array $steps = []): array
62  {
63  foreach (‪$modified as $key => $value) {
64  $currentSteps = array_merge($steps, [$key]);
65  if (is_array($value)) {
67  $current[$key] ?? [],
68  $value,
69  $currentSteps
70  );
71  } elseif (is_string($value)) {
73  $newFragments = $this->fragmentSplitter->split($value, $splitFlags);
74  if (is_string($current[$key] ?? null)) {
75  $currentFragments = $this->fragmentSplitter->split($current[$key] ?? '', $splitFlags);
76  } else {
77  $currentFragments = null;
78  }
79  // in case there are new fragments (at least one matching the pattern)
80  if ($newFragments !== null) {
81  // compares differences in `expression` fragments only
82  $differences = $currentFragments === null
83  ? $newFragments->withOnlyType(‪StringFragmentSplitter::TYPE_EXPRESSION)
84  : $newFragments->withOnlyType(‪StringFragmentSplitter::TYPE_EXPRESSION)
85  ->diff($currentFragments->withOnlyType(‪StringFragmentSplitter::TYPE_EXPRESSION));
86  if (count($differences) > 0) {
87  throw new YamlPlaceholderException(
88  sprintf(
89  'Introducing placeholder%s %s for %s is not allowed',
90  count($differences) !== 1 ? 's' : '',
91  implode(', ', $differences->getFragments()),
92  implode('.', $currentSteps)
93  ),
94  1651690534
95  );
96  }
97  }
98  }
99  }
100  return ‪$modified;
101  }
102 }
‪TYPO3\CMS\Core\Configuration\Loader\YamlPlaceholderGuard\process
‪process(array $modified)
Definition: YamlPlaceholderGuard.php:46
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader\PATTERN_PARTS
‪const PATTERN_PARTS
Definition: YamlFileLoader.php:50
‪TYPO3\CMS\Core\Configuration\Loader\YamlPlaceholderGuard\__construct
‪__construct(protected array $existingConfiguration)
Definition: YamlPlaceholderGuard.php:30
‪TYPO3\CMS\Core\Configuration\Loader\Exception\YamlPlaceholderException
Definition: YamlPlaceholderException.php:20
‪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:71
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪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
‪TYPO3\CMS\Core\Configuration\Loader\YamlPlaceholderGuard\$fragmentSplitter
‪StringFragmentSplitter $fragmentSplitter
Definition: YamlPlaceholderGuard.php:28