[TASK] Streamline expressionLanguage usage in core
[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\Context\Context;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\StringUtility;
23
24 /**
25 * Class DefaultFunctionsProvider
26 * @internal
27 */
28 class DefaultFunctionsProvider implements ExpressionFunctionProviderInterface
29 {
30 /**
31 * @return ExpressionFunction[] An array of Function instances
32 */
33 public function getFunctions()
34 {
35 return [
36 $this->getLikeFunction(),
37 $this->getEnvFunction(),
38 $this->getDateFunction(),
39 ];
40 }
41
42 protected function getLikeFunction(): ExpressionFunction
43 {
44 return new ExpressionFunction('like', function ($str) {
45 // Not implemented, we only use the evaluator
46 }, function ($arguments, $haystack, $needle) {
47 $result = StringUtility::searchStringWildcard((string)$haystack, (string)$needle);
48 return $result;
49 });
50 }
51
52 protected function getEnvFunction(): ExpressionFunction
53 {
54 return ExpressionFunction::fromPhp('getenv');
55 }
56
57 protected function getDateFunction(): ExpressionFunction
58 {
59 return new ExpressionFunction('date', function ($str) {
60 // Not implemented, we only use the evaluator
61 }, function ($arguments, $format) {
62 return GeneralUtility::makeInstance(Context::class)
63 ->getAspect('date')->getDateTime()->format($format);
64 });
65 }
66 }