[FEATURE] Add Log Module to AdminPanel
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Condition / ConditionContext.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Form\Domain\Condition;
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 TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Form\Domain\Exception;
20 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
21 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
23
24 /**
25 * Scope: frontend
26 * **This class is NOT meant to be sub classed by developers.**
27 *
28 * @internal
29 */
30 class ConditionContext
31 {
32
33 /**
34 * @var array
35 */
36 public $expressionLanguageProviders = [];
37
38 /**
39 * @var array
40 */
41 public $expressionLanguageVariables = [];
42
43 /**
44 * @param FormRuntime $formRuntime
45 */
46 public function __construct(FormRuntime $formRuntime)
47 {
48 $this->expressionLanguageVariables = $this->getInitialExpressionLanguageVariables($formRuntime);
49
50 $conditionContextDefinition = $formRuntime->getFormDefinition()->getConditionContextDefinition();
51
52 foreach ($conditionContextDefinition['expressionLanguageProvider'] ?? [] as $expressionLanguageProviderName => $expressionLanguageProviderDefinition) {
53 if (!isset($expressionLanguageProviderDefinition['implementationClassName'])) {
54 throw new Exception(sprintf('The "implementationClassName" was not set for expression language provider "%s".', $expressionLanguageProviderName), 1526695869);
55 }
56 $implementationClassName = $expressionLanguageProviderDefinition['implementationClassName'];
57
58 /** @see https://symfony.com/doc/4.0/components/expression_language/extending.html#using-expression-providers */
59 $this->expressionLanguageProviders[] = new $implementationClassName();
60 }
61
62 foreach ($conditionContextDefinition['expressionLanguageVariableProvider'] ?? [] as $expressionLanguageVariableProviderName => $expressionLanguageVariableProviderDefinition) {
63 if (!isset($expressionLanguageVariableProviderDefinition['implementationClassName'])) {
64 throw new Exception(sprintf('The "implementationClassName" was not set for expression language variable provider "%s".', $expressionLanguageVariableProviderName), 1526695870);
65 }
66
67 $implementationClassName = $expressionLanguageVariableProviderDefinition['implementationClassName'];
68 $expressionLanguageVariableProvider = new $implementationClassName($formRuntime);
69 if (!($expressionLanguageVariableProvider instanceof ExpressionLanguageVariableProviderInterface)) {
70 throw new Exception(sprintf('The expression language provider "%s" must implement "%s".', $implementationClassName, ExpressionLanguageVariableProviderInterface::class), 1526695874);
71 }
72 /** @see https://symfony.com/doc/4.0/components/expression_language.html#passing-in-variables */
73 $this->expressionLanguageVariables[$expressionLanguageVariableProvider->getVariableName()] = $expressionLanguageVariableProvider->getVariableValue();
74 }
75 }
76
77 /**
78 * @return array
79 */
80 public function getExpressionLanguageProviders(): array
81 {
82 return $this->expressionLanguageProviders;
83 }
84
85 /**
86 * @return array
87 */
88 public function getExpressionLanguageVariables(): array
89 {
90 return $this->expressionLanguageVariables;
91 }
92
93 /**
94 * @param FormRuntime $formRuntime
95 * @return array
96 */
97 protected function getInitialExpressionLanguageVariables(FormRuntime $formRuntime): array
98 {
99 $formValues = array_replace_recursive($formRuntime->getFormState()->getFormValues(), $formRuntime->getRequest()->getArguments());
100 $page = $formRuntime->getCurrentPage() ?? $formRuntime->getFormDefinition()->getPageByIndex(0);
101
102 $finisherIdentifier = '';
103 if ($formRuntime->getCurrentFinisher() !== null) {
104 $finisherIdentifier = (new \ReflectionClass($formRuntime->getCurrentFinisher()))->getShortName();
105 $finisherIdentifier = preg_replace('/Finisher$/', '', $finisherIdentifier);
106 }
107
108 $contentObjectData = [];
109 if (
110 TYPO3_MODE === 'FE'
111 && $this->getTypoScriptFrontendController()->cObj instanceof ContentObjectRenderer
112 ) {
113 $contentObjectData = $this->getTypoScriptFrontendController()->cObj->data;
114 }
115
116 return [
117 'formRuntime' => $formRuntime,
118 // some shortcuts
119 'formValues' => $formValues,
120 'stepIdentifier' => $page->getIdentifier(),
121 'stepType' => $page->getType(),
122 'finisherIdentifier' => $finisherIdentifier,
123 'siteLanguage' => $formRuntime->getCurrentSiteLanguage(),
124 'applicationContext' => GeneralUtility::getApplicationContext()->__toString(),
125 'contentObject' => $contentObjectData,
126 ];
127 }
128
129 /**
130 * @return TypoScriptFrontendController
131 */
132 protected function getTypoScriptFrontendController(): TypoScriptFrontendController
133 {
134 return $GLOBALS['TSFE'];
135 }
136 }