‪TYPO3CMS  ‪main
DefaultFunctionsProvider.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\ExpressionFunction;
21 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
28 
36 class ‪DefaultFunctionsProvider implements ExpressionFunctionProviderInterface
37 {
41  public function ‪getFunctions(): array
42  {
43  return [
44  $this->‪getIpFunction(),
46  $this->‪getLikeFunction(),
47  $this->‪getEnvFunction(),
48  $this->‪getDateFunction(),
51  ];
52  }
53 
54  protected function ‪getIpFunction(): ExpressionFunction
55  {
56  return new ExpressionFunction(
57  'ip',
58  static fn() => null, // Not implemented, we only use the evaluator
59  static function ($arguments, $str) {
60  if ($str === 'devIP') {
61  $str = $arguments['typo3']->devIpMask;
62  }
63  $request = $arguments['request'] ?? null;
64  if (!$request instanceof ‪RequestWrapper) {
65  throw new \RuntimeException(
66  'Using expression language function "ip(' . $str . ')" in a context without request.',
67  1686745105
68  );
69  }
70  $normalizedParams = $request->getNormalizedParams();
71  if ($normalizedParams === null) {
72  return false;
73  }
74  return ‪GeneralUtility::cmpIP($normalizedParams->getRemoteAddress(), $str);
75  }
76  );
77  }
78 
79  protected function ‪getCompatVersionFunction(): ExpressionFunction
80  {
81  return new ExpressionFunction(
82  'compatVersion',
83  static fn() => null, // Not implemented, we only use the evaluator
84  static function ($arguments, mixed $str) {
85  return ‪VersionNumberUtility::convertVersionNumberToInteger($arguments['typo3']->branch) >=
87  }
88  );
89  }
90 
91  protected function ‪getLikeFunction(): ExpressionFunction
92  {
93  return new ExpressionFunction(
94  'like',
95  static fn() => null, // Not implemented, we only use the evaluator
96  static function ($arguments, $haystack, $needle) {
97  return ‪StringUtility::searchStringWildcard((string)$haystack, (string)$needle);
98  }
99  );
100  }
101 
102  protected function ‪getEnvFunction(): ExpressionFunction
103  {
104  return ExpressionFunction::fromPhp('getenv');
105  }
106 
107  protected function ‪getDateFunction(): ExpressionFunction
108  {
109  return new ExpressionFunction(
110  'date',
111  static fn() => null, // Not implemented, we only use the evaluator
112  static function ($arguments, $format) {
113  return $arguments['date']->getDateTime()->format($format);
114  }
115  );
116  }
117 
118  protected function ‪getFeatureToggleFunction(): ExpressionFunction
119  {
120  return new ExpressionFunction(
121  'feature',
122  static fn() => null, // Not implemented, we only use the evaluator
123  static function ($arguments, $featureName) {
124  return $arguments['features']->isFeatureEnabled($featureName);
125  }
126  );
127  }
128 
129  protected function ‪getTraverseArrayFunction(): ExpressionFunction
130  {
131  return new ExpressionFunction(
132  'traverse',
133  static fn() => null, // Not implemented, we only use the evaluator
134  static function ($arguments, $array, $path) {
135  if (!is_array($array) || !is_string($path) || $path === '') {
136  return '';
137  }
138  try {
139  return ‪ArrayUtility::getValueByPath($array, $path);
140  } catch (‪MissingArrayPathException) {
141  return '';
142  }
143  }
144  );
145  }
146 }
‪TYPO3\CMS\Core\ExpressionLanguage\RequestWrapper
Definition: RequestWrapper.php:36
‪TYPO3\CMS\Core\Utility\VersionNumberUtility
Definition: VersionNumberUtility.php:26
‪TYPO3\CMS\Core\Utility\VersionNumberUtility\convertVersionNumberToInteger
‪static int convertVersionNumberToInteger(string $versionNumber)
Definition: VersionNumberUtility.php:33
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider
Definition: DefaultFunctionsProvider.php:18
‪TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException
Definition: MissingArrayPathException.php:27
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getEnvFunction
‪getEnvFunction()
Definition: DefaultFunctionsProvider.php:102
‪TYPO3\CMS\Core\Utility\StringUtility\searchStringWildcard
‪static bool searchStringWildcard(string $haystack, string $needle)
Definition: StringUtility.php:95
‪TYPO3\CMS\Core\Utility\GeneralUtility\cmpIP
‪static bool cmpIP(string $baseIP, string $list)
Definition: GeneralUtility.php:113
‪TYPO3\CMS\Core\Utility\ArrayUtility\getValueByPath
‪static getValueByPath(array $array, array|string $path, string $delimiter='/')
Definition: ArrayUtility.php:176
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getFunctions
‪ExpressionFunction[] getFunctions()
Definition: DefaultFunctionsProvider.php:41
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getDateFunction
‪getDateFunction()
Definition: DefaultFunctionsProvider.php:107
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getTraverseArrayFunction
‪getTraverseArrayFunction()
Definition: DefaultFunctionsProvider.php:129
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider
Definition: DefaultFunctionsProvider.php:37
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getCompatVersionFunction
‪getCompatVersionFunction()
Definition: DefaultFunctionsProvider.php:79
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getLikeFunction
‪getLikeFunction()
Definition: DefaultFunctionsProvider.php:91
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getIpFunction
‪getIpFunction()
Definition: DefaultFunctionsProvider.php:54
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getFeatureToggleFunction
‪getFeatureToggleFunction()
Definition: DefaultFunctionsProvider.php:118
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24