1313
1414use Brotkrueml \Schema \Core \Model \TypeInterface ;
1515use Brotkrueml \Schema \Type \TypeFactory ;
16- use TYPO3Fluid \Fluid \Core \Compiler \TemplateCompiler ;
17- use TYPO3Fluid \Fluid \Core \Parser \SyntaxTree \ViewHelperNode ;
18- use TYPO3Fluid \Fluid \Core \Rendering \RenderingContextInterface ;
16+ use TYPO3Fluid \Fluid \Core \ViewHelper \AbstractViewHelper ;
1917use TYPO3Fluid \Fluid \Core \ViewHelper \ArgumentDefinition ;
20- use TYPO3Fluid \Fluid \Core \ViewHelper \Exception ;
21- use TYPO3Fluid \Fluid \Core \ViewHelper \ViewHelperInterface ;
2218
2319/**
2420 * @internal
2521 */
26- final class TypeViewHelper implements ViewHelperInterface
22+ final class TypeViewHelper extends AbstractViewHelper
2723{
28- private ViewHelperNode $ viewHelperNode ;
2924 private TypeInterface $ schemaType ;
30- private RenderingContextInterface $ renderingContext ;
31-
32- /**
33- * @var list<RenderingContextInterface>
34- */
35- private array $ renderingContextStack = [];
3625
3726 /**
3827 * @var array<string, list<ArgumentDefinition>>
3928 */
40- private static array $ argumentDefinitions = [];
41-
42- /**
43- * @var array<string, mixed>
44- */
45- private array $ arguments = [];
29+ private static array $ typeArgumentDefinitions = [];
4630
4731 public function __construct (
48- private TypeFactory $ typeFactory ,
49- private SchemaTypeHandler $ schemaTypeHandler ,
32+ private readonly TypeFactory $ typeFactory ,
33+ private readonly SchemaTypeHandler $ schemaTypeHandler ,
5034 ) {}
5135
36+ /**
37+ * @param array<string, mixed> $arguments
38+ */
39+ public function setArguments (array $ arguments ): void
40+ {
41+ $ this ->arguments = $ arguments ;
42+ if ($ this ->arguments [NotableArgument::SpecificType->value ] !== '' ) {
43+ $ this ->schemaType = $ this ->typeFactory ->create ($ this ->arguments [NotableArgument::SpecificType->value ]);
44+ }
45+ }
46+
5247 /**
5348 * @return list<ArgumentDefinition>
5449 */
@@ -65,13 +60,13 @@ public function prepareArguments(): array
6560 ));
6661 }
6762
68- $ typeName = $ this -> schemaType -> getType ( );
69- assert ( is_string ( $ typeName) );
63+ $ typeName = \ucfirst ( \str_replace ( ' type. ' , '' , $ name ) );
64+ $ this -> schemaType = $ this -> typeFactory -> create ( $ typeName );
7065
71- if (! isset (self ::$ argumentDefinitions [$ typeName ])) {
72- self ::$ argumentDefinitions [$ typeName ] = $ this ->getNotableArguments ();
66+ if (! isset (self ::$ typeArgumentDefinitions [$ typeName ])) {
67+ self ::$ typeArgumentDefinitions [$ typeName ] = $ this ->getNotableArguments ();
7368 foreach ($ this ->schemaType ->getPropertyNames () as $ propertyName ) {
74- self ::$ argumentDefinitions [$ this ->schemaType ->getType ()][$ propertyName ] = new ArgumentDefinition (
69+ self ::$ typeArgumentDefinitions [$ this ->schemaType ->getType ()][$ propertyName ] = new ArgumentDefinition (
7570 $ propertyName ,
7671 'mixed ' ,
7772 \sprintf ('Property "%s" ' , $ propertyName ),
@@ -80,7 +75,7 @@ public function prepareArguments(): array
8075 }
8176 }
8277
83- return self ::$ argumentDefinitions [$ this ->schemaType ->getType ()];
78+ return self ::$ typeArgumentDefinitions [$ this ->schemaType ->getType ()];
8479 }
8580
8681 /**
@@ -105,124 +100,10 @@ private function getNotableArguments(): array
105100 return $ notableArguments ;
106101 }
107102
108- /**
109- * @param array<string, mixed> $arguments
110- */
111- public function setArguments (array $ arguments ): void
112- {
113- $ this ->arguments = $ arguments ;
114- if ($ this ->arguments [NotableArgument::SpecificType->value ] !== '' ) {
115- $ this ->schemaType = $ this ->typeFactory ->create ($ this ->arguments [NotableArgument::SpecificType->value ]);
116- }
117- }
118-
119- public function getContentArgumentName (): null
120- {
121- return null ;
122- }
123-
124- public function setChildNodes (array $ nodes ): void
125- {
126- // Do nothing, this method is deprecated in Fluid v4 and will be removed with Fluid v5
127- }
128-
129- public function setViewHelperNode (ViewHelperNode $ node ): void
130- {
131- $ name = $ node ->getName ();
132- if (! \str_starts_with ($ name , 'type. ' )) {
133- // There is something wrong, this should not happen
134- throw new \Exception (\sprintf (
135- 'ViewHelper %s:%s cannot be processed by %s ' ,
136- $ this ->viewHelperNode ->getNamespace (),
137- $ name ,
138- self ::class,
139- ));
140- }
141-
142- $ this ->viewHelperNode = $ node ;
143- $ typeName = \ucfirst (\str_replace ('type. ' , '' , $ name ));
144- $ this ->schemaType = $ this ->typeFactory ->create ($ typeName );
145- }
146-
147- public function setRenderingContext (RenderingContextInterface $ renderingContext ): void
148- {
149- $ this ->renderingContext = $ renderingContext ;
150- }
151-
152- public function initializeArgumentsAndRender (): string
103+ public function render (): string
153104 {
154105 $ this ->schemaTypeHandler ->addToSchema ($ this ->schemaType , $ this ->arguments , $ this ->buildRenderChildrenClosure ());
155106
156107 return '' ;
157108 }
158-
159- public function handleAdditionalArguments (array $ arguments ): void
160- {
161- // We do not have to handle additional arguments
162- }
163-
164- public function validateAdditionalArguments (array $ arguments ): void
165- {
166- $ invalidArguments = \array_filter (
167- $ arguments ,
168- fn (string $ argument ) => ! \in_array ($ argument , $ this ->schemaType ->getPropertyNames (), true ),
169- \ARRAY_FILTER_USE_KEY ,
170- );
171-
172- if ($ invalidArguments !== []) {
173- throw new Exception (
174- \sprintf (
175- 'Undeclared arguments passed to ViewHelper %s:%s: %s. Valid arguments are: %s ' ,
176- $ this ->viewHelperNode ->getNamespace (),
177- $ this ->viewHelperNode ->getName (),
178- \implode (', ' , \array_keys ($ arguments )),
179- \implode (', ' , $ this ->schemaType ->getPropertyNames ()),
180- ),
181- 1790604520 ,
182- );
183- }
184- }
185-
186- public function setRenderChildrenClosure (\Closure $ renderChildrenClosure ): void
187- {
188- // Actually, the ViewHelperNode does not call ViewHelperInvoker with a closure,
189- // this method should therefore not be called.
190- throw new \Exception ('Not implemented! ' );
191- }
192-
193- public function convert (TemplateCompiler $ templateCompiler ): array
194- {
195- return [
196- 'initialization ' => '' ,
197- 'execution ' => '' ,
198- ];
199- }
200-
201- public function isChildrenEscapingEnabled (): bool
202- {
203- // This does not really matter as we have no output
204- return true ;
205- }
206-
207- public function isOutputEscapingEnabled (): bool
208- {
209- // This does not really matter as we have no output
210- return true ;
211- }
212-
213- public function renderChildren (): mixed
214- {
215- return $ this ->buildRenderChildrenClosure ()();
216- }
217-
218- private function buildRenderChildrenClosure (): \Closure
219- {
220- return function () {
221- $ this ->renderingContextStack [] = $ this ->renderingContext ;
222- $ result = $ this ->viewHelperNode ->evaluateChildNodes ($ this ->renderingContext );
223- $ this ->setRenderingContext (\array_pop ($ this ->renderingContextStack ));
224-
225- return $ result ;
226- };
227- }
228109}
0 commit comments