‪TYPO3CMS  ‪main
IncludeTreeSetupConditionConstantSubstitutionVisitor.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 
24 
36 {
40  private array ‪$flattenedConstants;
41 
49  public function ‪setFlattenedConstants(array ‪$flattenedConstants): void
50  {
51  $this->flattenedConstants = ‪$flattenedConstants;
52  }
53 
60  public function ‪visitBeforeChildren(‪IncludeInterface $include, int $currentDepth): void
61  {
62  if (!$include instanceof ‪IncludeConditionInterface) {
63  return;
64  }
65  $conditionToken = $include->getConditionToken();
66  $conditionValue = $conditionToken->getValue();
68  $hadSubstitution = false;
69  $newConditionValue = preg_replace_callback(
70  '/{\$(.[^}]*)}/',
71  static function ($match) use (‪$flattenedConstants, &$hadSubstitution) {
72  // Replace {$someConstant} if found, else leave unchanged
73  if (array_key_exists($match[1], ‪$flattenedConstants)) {
74  $hadSubstitution = true;
75  return ‪$flattenedConstants[$match[1]];
76  }
77  return $match[0];
78  },
79  $conditionValue
80  );
81  if ($hadSubstitution) {
82  $include->setOriginalConditionToken($conditionToken);
83  $include->setConditionToken(new ‪Token(TokenType::T_VALUE, $newConditionValue, $conditionToken->getLine(), $conditionToken->getColumn()));
84  }
85  }
86 
87  public function ‪visit(‪IncludeInterface $include, int $currentDepth): void
88  {
89  // Noop, just implement interface
90  }
91 }
‪TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeSetupConditionConstantSubstitutionVisitor
Definition: IncludeTreeSetupConditionConstantSubstitutionVisitor.php:36
‪TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeSetupConditionConstantSubstitutionVisitor\$flattenedConstants
‪array $flattenedConstants
Definition: IncludeTreeSetupConditionConstantSubstitutionVisitor.php:40
‪TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeSetupConditionConstantSubstitutionVisitor\visitBeforeChildren
‪visitBeforeChildren(IncludeInterface $include, int $currentDepth)
Definition: IncludeTreeSetupConditionConstantSubstitutionVisitor.php:60
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Token\Token
Definition: Token.php:29
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Token\TokenType
‪TokenType
Definition: TokenType.php:26
‪TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeSetupConditionConstantSubstitutionVisitor\setFlattenedConstants
‪setFlattenedConstants(array $flattenedConstants)
Definition: IncludeTreeSetupConditionConstantSubstitutionVisitor.php:49
‪TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeVisitorInterface
Definition: IncludeTreeVisitorInterface.php:28
‪TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\IncludeConditionInterface
Definition: IncludeConditionInterface.php:32
‪TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\IncludeInterface
Definition: IncludeInterface.php:39
‪TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeSetupConditionConstantSubstitutionVisitor\visit
‪visit(IncludeInterface $include, int $currentDepth)
Definition: IncludeTreeSetupConditionConstantSubstitutionVisitor.php:87
‪TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor
Definition: IncludeTreeAstBuilderVisitor.php:18