[FEATURE] Support feature toggle in expression language
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / ExpressionLanguage / FunctionsProvider / DefaultFunctionsProvider.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider;
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
18 use Symfony\Component\ExpressionLanguage\ExpressionFunction;
19 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
20 use TYPO3\CMS\Core\Configuration\Features;
21 use TYPO3\CMS\Core\Context\Context;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\StringUtility;
24 use TYPO3\CMS\Core\Utility\VersionNumberUtility;
25
26 /**
27 * Class DefaultFunctionsProvider
28 * @internal
29 */
30 class DefaultFunctionsProvider implements ExpressionFunctionProviderInterface
31 {
32 /**
33 * @return ExpressionFunction[] An array of Function instances
34 */
35 public function getFunctions()
36 {
37 return [
38 $this->getIpFunction(),
39 $this->getCompatVersionFunction(),
40 $this->getLikeFunction(),
41 $this->getEnvFunction(),
42 $this->getDateFunction(),
43 $this->getFeatureToggleFunction(),
44 ];
45 }
46
47 protected function getIpFunction(): ExpressionFunction
48 {
49 return new ExpressionFunction(
50 'ip',
51 function () {
52 // Not implemented, we only use the evaluator
53 },
54 function ($arguments, $str) {
55 if ($str === 'devIP') {
56 $str = trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] ?? '');
57 }
58 return (bool)GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $str);
59 }
60 );
61 }
62
63 protected function getCompatVersionFunction(): ExpressionFunction
64 {
65 return new ExpressionFunction(
66 'compatVersion',
67 function () {
68 // Not implemented, we only use the evaluator
69 },
70 function ($arguments, $str) {
71 return VersionNumberUtility::convertVersionNumberToInteger(TYPO3_branch) >=
72 VersionNumberUtility::convertVersionNumberToInteger($str);
73 }
74 );
75 }
76
77 protected function getLikeFunction(): ExpressionFunction
78 {
79 return new ExpressionFunction('like', function () {
80 // Not implemented, we only use the evaluator
81 }, function ($arguments, $haystack, $needle) {
82 $result = StringUtility::searchStringWildcard((string)$haystack, (string)$needle);
83 return $result;
84 });
85 }
86
87 protected function getEnvFunction(): ExpressionFunction
88 {
89 return ExpressionFunction::fromPhp('getenv');
90 }
91
92 protected function getDateFunction(): ExpressionFunction
93 {
94 return new ExpressionFunction('date', function () {
95 // Not implemented, we only use the evaluator
96 }, function ($arguments, $format) {
97 return GeneralUtility::makeInstance(Context::class)
98 ->getAspect('date')->getDateTime()->format($format);
99 });
100 }
101
102 protected function getFeatureToggleFunction(): ExpressionFunction
103 {
104 return new ExpressionFunction('feature', function () {
105 // Not implemented, we only use the evaluator
106 }, function ($arguments, $featureName) {
107 return GeneralUtility::makeInstance(Features::class)
108 ->isFeatureEnabled($featureName);
109 });
110 }
111 }