‪TYPO3CMS  ‪main
Resolver.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 
20 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
21 use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
23 
32 {
33  private ExpressionLanguage ‪$expressionLanguage;
35 
36  public function ‪__construct(string $context, array $variables)
37  {
38  $functionProviderInstances = [];
39  // @todo: The entire ProviderConfigurationLoader approach should fall and
40  // substituted with a symfony service provider strategy in v13.
41  // Also, the magic "DefaultProvider" approach should fall at this time,
42  // default functions and variable providers should be provided explicitly
43  // by config.
44  // The entire construct should be reviewed at this point and most likely
45  // declared final as well.
46  $providers = GeneralUtility::makeInstance(ProviderConfigurationLoader::class)->getExpressionLanguageProviders()[$context] ?? [];
47  // Always add default provider
48  array_unshift($providers, DefaultProvider::class);
49  $providers = array_unique($providers);
50  $functionProviders = [];
51  $generalVariables = [];
52  foreach ($providers as $provider) {
54  $providerInstance = GeneralUtility::makeInstance($provider);
55  $functionProviders[] = $providerInstance->getExpressionLanguageProviders();
56  $generalVariables[] = $providerInstance->getExpressionLanguageVariables();
57  }
58  $functionProviders = array_merge(...$functionProviders);
59  $generalVariables = array_replace_recursive(...$generalVariables);
60  $this->expressionLanguageVariables = array_replace_recursive($generalVariables, $variables);
61  foreach ($functionProviders as $functionProvider) {
63  $functionProviderInstances[] = GeneralUtility::makeInstance($functionProvider);
64  }
65  $this->expressionLanguage = new ExpressionLanguage(null, $functionProviderInstances);
66  }
67 
71  public function ‪evaluate(string $expression, array $contextVariables = []): mixed
72  {
73  return $this->expressionLanguage->evaluate($expression, array_replace($this->expressionLanguageVariables, $contextVariables));
74  }
75 
80  public function ‪compile(string $condition): string
81  {
82  return $this->expressionLanguage->compile($condition, array_keys($this->expressionLanguageVariables));
83  }
84 }
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\$expressionLanguageVariables
‪array $expressionLanguageVariables
Definition: Resolver.php:34
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\$expressionLanguage
‪ExpressionLanguage $expressionLanguage
Definition: Resolver.php:33
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\compile
‪compile(string $condition)
Definition: Resolver.php:80
‪TYPO3\CMS\Core\ExpressionLanguage
Definition: AbstractProvider.php:18
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver
Definition: Resolver.php:32
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\__construct
‪__construct(string $context, array $variables)
Definition: Resolver.php:36
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\evaluate
‪evaluate(string $expression, array $contextVariables=[])
Definition: Resolver.php:71
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52