‪TYPO3CMS  ‪main
Typo3ConditionFunctionsProvider.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;
25 
32 class ‪Typo3ConditionFunctionsProvider implements ExpressionFunctionProviderInterface
33 {
37  public function ‪getFunctions(): array
38  {
39  return [
40  $this->‪getTSFEFunction(),
41  $this->‪getSessionFunction(),
42  $this->‪getSiteFunction(),
44  ];
45  }
46 
47  protected function ‪getTSFEFunction(): ExpressionFunction
48  {
49  // @todo: This should probably vanish mid-term: TSFE is shrinking and calling
50  // properties on this object is risky and becomes more and more problematic.
51  return new ExpressionFunction(
52  'getTSFE',
53  static fn() => null, // Not implemented, we only use the evaluator
54  static function ($arguments) {
55  // @todo: b/w compat layer. This will later log a deprecation log entry when
56  // for instance tsfe->id is being actively deprecated. When this happens,
57  // an alternative should be documented to for instance retrieve the current
58  // page uid from PageInformation - request attribute 'frontend.page.information'.
59  if (($arguments['tsfe'] ?? null) instanceof ‪TypoScriptFrontendController) {
60  return $arguments['tsfe'];
61  }
62  // If TSFE is not given as argument, return null.
63  return null;
64  }
65  );
66  }
67 
68  protected function ‪getSessionFunction(): ExpressionFunction
69  {
70  return new ExpressionFunction(
71  'session',
72  static fn() => null, // Not implemented, we only use the evaluator
73  static function ($arguments, $str) {
74  $retVal = null;
75  $keyParts = explode('|', $str);
76  $sessionKey = array_shift($keyParts);
77  $frontendUser = $arguments['request']->getFrontendUser();
78  if ($frontendUser) {
79  $retVal = $frontendUser->getSessionData($sessionKey);
80  foreach ($keyParts as $keyPart) {
81  if (is_object($retVal)) {
82  $retVal = $retVal->{$keyPart};
83  } elseif (is_array($retVal)) {
84  $retVal = $retVal[$keyPart];
85  } else {
86  break;
87  }
88  }
89  }
90  return $retVal;
91  }
92  );
93  }
94 
95  protected function ‪getSiteFunction(): ExpressionFunction
96  {
97  return new ExpressionFunction(
98  'site',
99  static fn() => null, // Not implemented, we only use the evaluator
100  static function ($arguments, $str) {
101  $site = $arguments['site'] ?? null;
102  if ($site instanceof ‪SiteInterface) {
103  $methodName = 'get' . ucfirst(trim($str));
104  if (method_exists($site, $methodName)) {
105  return $site->$methodName();
106  }
107  }
108  return null;
109  }
110  );
111  }
112 
113  protected function ‪getSiteLanguageFunction(): ExpressionFunction
114  {
115  return new ExpressionFunction(
116  'siteLanguage',
117  static fn() => null, // Not implemented, we only use the evaluator
118  static function ($arguments, $str) {
119  $siteLanguage = $arguments['siteLanguage'] ?? null;
120  if ($siteLanguage instanceof ‪SiteLanguage) {
121  $methodName = 'get' . ucfirst(trim($str));
122  if (method_exists($siteLanguage, $methodName)) {
123  return $siteLanguage->$methodName();
124  }
125  }
126  return null;
127  }
128  );
129  }
130 }
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getSiteFunction
‪getSiteFunction()
Definition: Typo3ConditionFunctionsProvider.php:95
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider
Definition: DefaultFunctionsProvider.php:18
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getFunctions
‪ExpressionFunction[] getFunctions()
Definition: Typo3ConditionFunctionsProvider.php:37
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getTSFEFunction
‪getTSFEFunction()
Definition: Typo3ConditionFunctionsProvider.php:47
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:27
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:58
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getSessionFunction
‪getSessionFunction()
Definition: Typo3ConditionFunctionsProvider.php:68
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider
Definition: Typo3ConditionFunctionsProvider.php:33
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getSiteLanguageFunction
‪getSiteLanguageFunction()
Definition: Typo3ConditionFunctionsProvider.php:113