‪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  return ‪GeneralUtility::cmpIP($request->getNormalizedParams()->getRemoteAddress(), $str);
71  }
72  );
73  }
74 
75  protected function ‪getCompatVersionFunction(): ExpressionFunction
76  {
77  return new ExpressionFunction(
78  'compatVersion',
79  static fn() => null, // Not implemented, we only use the evaluator
80  static function ($arguments, mixed $str) {
81  return ‪VersionNumberUtility::convertVersionNumberToInteger($arguments['typo3']->branch) >=
83  }
84  );
85  }
86 
87  protected function ‪getLikeFunction(): ExpressionFunction
88  {
89  return new ExpressionFunction(
90  'like',
91  static fn() => null, // Not implemented, we only use the evaluator
92  static function ($arguments, $haystack, $needle) {
93  return ‪StringUtility::searchStringWildcard((string)$haystack, (string)$needle);
94  }
95  );
96  }
97 
98  protected function ‪getEnvFunction(): ExpressionFunction
99  {
100  return ExpressionFunction::fromPhp('getenv');
101  }
102 
103  protected function ‪getDateFunction(): ExpressionFunction
104  {
105  return new ExpressionFunction(
106  'date',
107  static fn() => null, // Not implemented, we only use the evaluator
108  static function ($arguments, $format) {
109  return $arguments['date']->getDateTime()->format($format);
110  }
111  );
112  }
113 
114  protected function ‪getFeatureToggleFunction(): ExpressionFunction
115  {
116  return new ExpressionFunction(
117  'feature',
118  static fn() => null, // Not implemented, we only use the evaluator
119  static function ($arguments, $featureName) {
120  return $arguments['features']->isFeatureEnabled($featureName);
121  }
122  );
123  }
124 
125  protected function ‪getTraverseArrayFunction(): ExpressionFunction
126  {
127  return new ExpressionFunction(
128  'traverse',
129  static fn() => null, // Not implemented, we only use the evaluator
130  static function ($arguments, $array, $path) {
131  if (!is_array($array) || !is_string($path) || $path === '') {
132  return '';
133  }
134  try {
135  return ‪ArrayUtility::getValueByPath($array, $path);
136  } catch (‪MissingArrayPathException) {
137  return '';
138  }
139  }
140  );
141  }
142 }
‪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:98
‪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:103
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getTraverseArrayFunction
‪getTraverseArrayFunction()
Definition: DefaultFunctionsProvider.php:125
‪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:75
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getLikeFunction
‪getLikeFunction()
Definition: DefaultFunctionsProvider.php:87
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getIpFunction
‪getIpFunction()
Definition: DefaultFunctionsProvider.php:54
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getFeatureToggleFunction
‪getFeatureToggleFunction()
Definition: DefaultFunctionsProvider.php:114
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24