[TASK] Use single quotes instead of double quotes
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / NodeFactory.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Create an element object depending on type.
21 */
22 class NodeFactory
23 {
24 /**
25 * Default registry of node name to handling class
26 *
27 * @var array
28 */
29 protected $nodeTypes = array(
30 'flex' => Container\FlexFormEntryContainer::class,
31 'flexFormContainerContainer' => Container\FlexFormContainerContainer::class,
32 'flexFormElementContainer' => Container\FlexFormElementContainer::class,
33 'flexFormNoTabsContainer' => Container\FlexFormNoTabsContainer::class,
34 'flexFormSectionContainer' => Container\FlexFormSectionContainer::class,
35 'flexFormTabsContainer' => Container\FlexFormTabsContainer::class,
36 'fullRecordContainer' => Container\FullRecordContainer::class,
37 'inline' => Container\InlineControlContainer::class,
38 'inlineRecordContainer' => Container\InlineRecordContainer::class,
39 'listOfFieldsContainer' => Container\ListOfFieldsContainer::class,
40 'noTabsContainer' => Container\NoTabsContainer::class,
41 'outerWrapContainer' => Container\OuterWrapContainer::class,
42 'paletteAndSingleContainer' => Container\PaletteAndSingleContainer::class,
43 'singleFieldContainer' => Container\SingleFieldContainer::class,
44 'soloFieldContainer' => Container\SoloFieldContainer::class,
45 'tabsContainer' => Container\TabsContainer::class,
46
47 'check' => Element\CheckboxElement::class,
48 'group' => Element\GroupElement::class,
49 'input' => Element\InputTextElement::class,
50 'hidden' => Element\InputHiddenElement::class,
51 // rsaInput is defined with a fallback so extensions can use it even if ext:rsaauth is not loaded
52 'rsaInput' => Element\InputTextElement::class,
53 'imageManipulation' => Element\ImageManipulationElement::class,
54 'none' => Element\NoneElement::class,
55 'radio' => Element\RadioElement::class,
56 'selectCheckBox' => Element\SelectCheckBoxElement::class,
57 'selectMultipleSideBySide' => Element\SelectMultipleSideBySideElement::class,
58 'selectTree' => Element\SelectTreeElement::class,
59 'selectSingle' => Element\SelectSingleElement::class,
60 'selectSingleBox' => Element\SelectSingleBoxElement::class,
61 // t3editor is defined with a fallback so extensions can use it even if ext:t3editor is not loaded
62 't3editor' => Element\TextElement::class,
63 'text' => Element\TextElement::class,
64 'unknown' => Element\UnknownElement::class,
65 'user' => Element\UserElement::class,
66 );
67
68 /**
69 * Node resolver classes
70 * Nested array with nodeName as key, (sorted) priority as sub key and class as value
71 *
72 * @var array
73 */
74 protected $nodeResolver = array();
75
76 /**
77 * Set up factory. Initialize additionally registered nodes.
78 */
79 public function __construct()
80 {
81 $this->registerAdditionalNodeTypesFromConfiguration();
82 $this->initializeNodeResolver();
83 }
84
85 /**
86 * Create a node depending on type
87 *
88 * @param array $data All information to decide which class should be instantiated and given down to sub nodes
89 * @return AbstractNode
90 * @throws Exception
91 */
92 public function create(array $data)
93 {
94 if (empty($data['renderType'])) {
95 throw new Exception('No renderType definition found', 1431452406);
96 }
97 $type = $data['renderType'];
98
99 $className = isset($this->nodeTypes[$type]) ? $this->nodeTypes[$type] : $this->nodeTypes['unknown'];
100
101 if (!empty($this->nodeResolver[$type])) {
102 // Resolver with highest priority is called first. If it returns with a new class name,
103 // it will be taken and loop is aborted, otherwise resolver with next lower priority is called.
104 foreach ($this->nodeResolver[$type] as $priority => $resolverClassName) {
105 /** @var NodeResolverInterface $resolver */
106 $resolver = $this->instantiate($resolverClassName, $data);
107 if (!$resolver instanceof NodeResolverInterface) {
108 throw new Exception(
109 'Node resolver for type ' . $type . ' at priority ' . $priority . ' must implement NodeResolverInterface',
110 1433157422
111 );
112 }
113 // Resolver classes do NOT receive the name of the already resolved class. Single
114 // resolvers should not have dependencies to each other or the default implementation,
115 // so they also shouldn't know the output of a different resolving class.
116 // Additionally, the globalOptions array is NOT given by reference here, changing config is a
117 // task of container classes alone and must not be abused here.
118 $newClassName = $resolver->resolve();
119 if ($newClassName !== null) {
120 $className = $newClassName;
121 break;
122 }
123 }
124 }
125
126 /** @var AbstractNode $nodeInstance */
127 $nodeInstance = $this->instantiate($className, $data);
128 if (!$nodeInstance instanceof NodeInterface) {
129 throw new Exception('Node of type ' . get_class($nodeInstance) . ' must implement NodeInterface', 1431872546);
130 }
131 return $nodeInstance;
132 }
133
134 /**
135 * Add node types from nodeRegistry to $this->nodeTypes.
136 * This can be used to add new render types or to overwrite existing node types. The registered class must
137 * implement the NodeInterface and will be called if a node with this renderType is rendered.
138 *
139 * @throws Exception if configuration is incomplete or two nodes with identical priorities are registered
140 */
141 protected function registerAdditionalNodeTypesFromConfiguration()
142 {
143 // List of additional or override nodes
144 $registeredTypeOverrides = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'];
145 // Sanitize input array
146 $registeredPrioritiesForNodeNames = array();
147 foreach ($registeredTypeOverrides as $override) {
148 if (!isset($override['nodeName']) || !isset($override['class']) || !isset($override['priority'])) {
149 throw new Exception(
150 'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeRegistry\']',
151 1432207533
152 );
153 }
154 if ($override['priority'] < 0 || $override['priority'] > 100) {
155 throw new Exception(
156 'Priority of element ' . $override['nodeName'] . ' with class ' . $override['class'] . ' is ' . $override['priority'] . ', but must between 0 and 100',
157 1432223531
158 );
159 }
160 if (isset($registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']])) {
161 throw new Exception(
162 'Element ' . $override['nodeName'] . ' already has an override registered with priority ' . $override['priority'],
163 1432223893
164 );
165 }
166 $registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']] = '';
167 }
168 // Add element with highest priority to registry
169 $highestPriority = array();
170 foreach ($registeredTypeOverrides as $override) {
171 if (!isset($highestPriority[$override['nodeName']]) || $override['priority'] > $highestPriority[$override['nodeName']]) {
172 $highestPriority[$override['nodeName']] = $override['priority'];
173 $this->nodeTypes[$override['nodeName']] = $override['class'];
174 }
175 }
176 }
177
178 /**
179 * Add resolver and add them sorted to a local property.
180 * This can be used to manipulate the nodeName to class resolution with own code.
181 *
182 * @throws Exception if configuration is incomplete or two resolver with identical priorities are registered
183 */
184 protected function initializeNodeResolver()
185 {
186 // List of node resolver
187 $registeredNodeResolvers = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'];
188 $resolversByType = array();
189 foreach ($registeredNodeResolvers as $nodeResolver) {
190 if (!isset($nodeResolver['nodeName']) || !isset($nodeResolver['class']) || !isset($nodeResolver['priority'])) {
191 throw new Exception(
192 'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeResolver\']',
193 1433155522
194 );
195 }
196 if ($nodeResolver['priority'] < 0 || $nodeResolver['priority'] > 100) {
197 throw new Exception(
198 'Priority of element ' . $nodeResolver['nodeName'] . ' with class ' . $nodeResolver['class'] . ' is ' . $nodeResolver['priority'] . ', but must between 0 and 100',
199 1433155563
200 );
201 }
202 if (isset($resolversByType[$nodeResolver['nodeName']][$nodeResolver['priority']])) {
203 throw new Exception(
204 'Element ' . $nodeResolver['nodeName'] . ' already has a resolver registered with priority ' . $nodeResolver['priority'],
205 1433155705
206 );
207 }
208 $resolversByType[$nodeResolver['nodeName']][$nodeResolver['priority']] = $nodeResolver['class'];
209 }
210 $sortedResolversByType = array();
211 foreach ($resolversByType as $nodeName => $prioritiesAndClasses) {
212 krsort($prioritiesAndClasses);
213 $sortedResolversByType[$nodeName] = $prioritiesAndClasses;
214 }
215 $this->nodeResolver = $sortedResolversByType;
216 }
217
218 /**
219 * Instantiate given class name
220 *
221 * @param string $className Given class name
222 * @param array $data Main data array
223 * @return object
224 */
225 protected function instantiate($className, array $data)
226 {
227 return GeneralUtility::makeInstance($className, $this, $data);
228 }
229 }