‪TYPO3CMS  10.4
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\ExpressionLanguage;
22 
27 {
31  protected ‪$provider;
32 
36  protected ‪$expressionLanguage;
37 
42 
47  public function ‪__construct(string $context, array $variables)
48  {
49  $functionProviderInstances = [];
50  $providers = GeneralUtility::makeInstance(ProviderConfigurationLoader::class)->getExpressionLanguageProviders()[$context] ?? [];
51  // Always add default provider
52  array_unshift($providers, DefaultProvider::class);
53  $providers = array_unique($providers);
54  $functionProviders = [];
55  $generalVariables = [];
56  foreach ($providers as ‪$provider) {
58  $providerInstance = GeneralUtility::makeInstance(‪$provider);
59  $functionProviders[] = $providerInstance->getExpressionLanguageProviders();
60  $generalVariables[] = $providerInstance->getExpressionLanguageVariables();
61  }
62  $functionProviders = array_merge(...$functionProviders);
63  $generalVariables = array_replace_recursive(...$generalVariables);
64  $this->expressionLanguageVariables = array_replace_recursive($generalVariables, $variables);
65  foreach ($functionProviders as $functionProvider) {
66  $functionProviderInstances[] = GeneralUtility::makeInstance($functionProvider);
67  }
68  $this->expressionLanguage = new ExpressionLanguage(null, $functionProviderInstances);
69  }
70 
77  public function ‪evaluate(string $condition): bool
78  {
79  return (bool)$this->expressionLanguage->evaluate($condition, $this->expressionLanguageVariables);
80  }
81 
88  public function ‪compile(string $condition): string
89  {
90  return (string)$this->expressionLanguage->compile($condition, array_keys($this->expressionLanguageVariables));
91  }
92 }
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\$expressionLanguageVariables
‪array $expressionLanguageVariables
Definition: Resolver.php:38
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\evaluate
‪bool evaluate(string $condition)
Definition: Resolver.php:74
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\$expressionLanguage
‪Symfony Component ExpressionLanguage ExpressionLanguage $expressionLanguage
Definition: Resolver.php:34
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\compile
‪string compile(string $condition)
Definition: Resolver.php:85
‪TYPO3\CMS\Core\ExpressionLanguage\ProviderInterface
Definition: ProviderInterface.php:26
‪TYPO3\CMS\Core\ExpressionLanguage
Definition: AbstractProvider.php:18
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver
Definition: Resolver.php:27
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\$provider
‪ProviderInterface $provider
Definition: Resolver.php:30
‪TYPO3\CMS\Core\ExpressionLanguage\Resolver\__construct
‪__construct(string $context, array $variables)
Definition: Resolver.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46