1
1
<?php
2
2
3
- declare (strict_types=1 );
4
-
5
3
/*
6
4
* This file is part of the Symfony package.
7
5
*
22
20
use Twig \Environment ;
23
21
use Twig \Loader \ArrayLoader ;
24
22
23
+ use function array_merge ;
24
+ use function explode ;
25
+ use function implode ;
26
+ use function is_array ;
27
+ use function is_string ;
28
+ use function str_contains ;
29
+
25
30
class FormTypeExtension extends AbstractTypeExtension
26
31
{
27
32
private StimulusAttributes $ stimulusAttributes ;
@@ -34,42 +39,70 @@ public static function getExtendedTypes(): iterable
34
39
public function buildView (FormView $ view , FormInterface $ form , array $ options ): void
35
40
{
36
41
if (
37
- null === $ options ['stimulus_controller ' ]
38
- && null === $ options ['stimulus_target ' ]
39
- && null === $ options ['stimulus_action ' ]
42
+ isset ( $ options ['stimulus_controller ' ])
43
+ || ! isset ( $ options ['stimulus_target ' ])
44
+ || ! isset ( $ options ['stimulus_action ' ])
40
45
) {
41
- return ;
42
- }
46
+ $ this ->stimulusAttributes = new StimulusAttributes (new Environment (new ArrayLoader ()));
43
47
44
- $ this ->stimulusAttributes = new StimulusAttributes (new Environment (new ArrayLoader ()));
48
+ if (isset ($ options ['stimulus_controller ' ])) {
49
+ $ this ->handleController ($ options ['stimulus_controller ' ]);
50
+ }
45
51
46
- if ( true === \array_key_exists ( ' stimulus_controller ' , $ options )) {
47
- $ this ->handleController ($ options ['stimulus_controller ' ]);
48
- }
52
+ if ( isset ( $ options[ ' stimulus_target ' ] )) {
53
+ $ this ->handleTarget ($ options ['stimulus_target ' ]);
54
+ }
49
55
50
- if ( true === \array_key_exists ( ' stimulus_target ' , $ options )) {
51
- $ this ->handleTarget ($ options ['stimulus_target ' ]);
52
- }
56
+ if ( isset ( $ options[ ' stimulus_action ' ] )) {
57
+ $ this ->handleAction ($ options ['stimulus_action ' ]);
58
+ }
53
59
54
- if ( true === \array_key_exists ( ' stimulus_action ' , $ options )) {
55
- $ this -> handleAction ( $ options [ ' stimulus_action ' ]) ;
60
+ $ attributes = array_merge ( $ view -> vars [ ' attr ' ] , $ this -> stimulusAttributes -> toArray ());
61
+ $ view -> vars [ ' attr ' ] = $ attributes ;
56
62
}
57
63
58
- $ attributes = array_merge ($ view ->vars ['attr ' ], $ this ->stimulusAttributes ->toArray ());
64
+ foreach (['row_attr ' , 'choice_attr ' ] as $ index ) {
65
+ if (
66
+ isset ($ options [$ index ])
67
+ && (
68
+ isset ($ options [$ index ]['stimulus_controller ' ])
69
+ || isset ($ options [$ index ]['stimulus_target ' ])
70
+ || isset ($ options [$ index ]['stimulus_action ' ])
71
+ )
72
+ ) {
73
+ $ this ->stimulusAttributes = new StimulusAttributes (new Environment (new ArrayLoader ()));
74
+
75
+ if (isset ($ options [$ index ]['stimulus_controller ' ])) {
76
+ $ this ->handleController ($ options [$ index ]['stimulus_controller ' ]);
77
+ unset($ options [$ index ]['stimulus_controller ' ]);
78
+ }
79
+
80
+ if (isset ($ options [$ index ]['stimulus_target ' ])) {
81
+ $ this ->handleTarget ($ options [$ index ]['stimulus_target ' ]);
82
+ unset($ options [$ index ]['stimulus_target ' ]);
83
+ }
84
+
85
+ if (isset ($ options [$ index ]['stimulus_action ' ])) {
86
+ $ this ->handleAction ($ options [$ index ]['stimulus_action ' ]);
87
+ unset($ options [$ index ]['stimulus_action ' ]);
88
+ }
59
89
60
- $ view ->vars ['attr ' ] = $ attributes ;
90
+ $ attributes = array_merge ($ options [$ index ], $ this ->stimulusAttributes ->toArray ());
91
+ $ view ->vars [$ index ] = $ attributes ;
92
+ }
93
+ }
61
94
}
62
95
63
96
private function handleController (string |array $ controllers ): void
64
97
{
65
98
if (\is_string ($ controllers )) {
66
- $ controllers = [$ controllcers ];
99
+ $ controllers = [$ controllers ];
67
100
}
68
101
69
102
foreach ($ controllers as $ controllerName => $ controller ) {
70
- if (\ is_string ($ controller )) { // 'stimulus_controller' => ['controllerName1', 'controllerName2']
103
+ if (is_string ($ controller )) { // 'stimulus_controller' => ['controllerName1', 'controllerName2']
71
104
$ this ->stimulusAttributes ->addController ($ controller );
72
- } elseif (\ is_array ($ controller )) { // 'stimulus_controller' => ['controllerName' => ['values' => ['key' => 'value'], 'classes' => ['key' => 'value'], 'targets' => ['otherControllerName' => '.targetName']]]
105
+ } elseif (is_array ($ controller )) { // 'stimulus_controller' => ['controllerName' => ['values' => ['key' => 'value'], 'classes' => ['key' => 'value'], 'targets' => ['otherControllerName' => '.targetName']]]
73
106
$ this ->stimulusAttributes ->addController ((string ) $ controllerName , $ controller ['values ' ] ?? [], $ controller ['classes ' ] ?? [], $ controller ['outlets ' ] ?? []);
74
107
}
75
108
}
@@ -78,15 +111,15 @@ private function handleController(string|array $controllers): void
78
111
private function handleTarget (array $ targets ): void
79
112
{
80
113
foreach ($ targets as $ controllerName => $ target ) {
81
- $ this ->stimulusAttributes ->addTarget ($ controllerName , \ is_array ($ target ) ? implode (' ' , $ target ) : $ target );
114
+ $ this ->stimulusAttributes ->addTarget ($ controllerName , is_array ($ target ) ? implode (' ' , $ target ) : $ target );
82
115
}
83
116
}
84
117
85
118
private function handleAction (string |array $ actions ): void
86
119
{
87
120
// 'stimulus_action' => 'controllerName#actionName'
88
121
// 'stimulus_action' => 'eventName->controllerName#actionName'
89
- if (\ is_string ($ actions ) && str_contains ($ actions , '# ' )) {
122
+ if (is_string ($ actions ) && str_contains ($ actions , '# ' )) {
90
123
$ eventName = null ;
91
124
92
125
if (str_contains ($ actions , '-> ' )) {
@@ -103,13 +136,13 @@ private function handleAction(string|array $actions): void
103
136
}
104
137
105
138
foreach ($ actions as $ controllerName => $ action ) {
106
- if (\ is_string ($ action )) { // 'stimulus_action' => ['controllerName' => 'actionName']
139
+ if (is_string ($ action )) { // 'stimulus_action' => ['controllerName' => 'actionName']
107
140
$ this ->stimulusAttributes ->addAction ($ controllerName , $ action );
108
- } elseif (\ is_array ($ action )) {
141
+ } elseif (is_array ($ action )) {
109
142
foreach ($ action as $ eventName => $ actionName ) {
110
- if (\ is_string ($ actionName )) { // 'stimulus_action' => ['controllerName' => ['eventName' => 'actionName']]
143
+ if (is_string ($ actionName )) { // 'stimulus_action' => ['controllerName' => ['eventName' => 'actionName']]
111
144
$ this ->stimulusAttributes ->addAction ($ controllerName , $ actionName , $ eventName );
112
- } elseif (\ is_array ($ actionName )) { // 'stimulus_action' => ['controllerName' => ['eventName' => ['actionName' => ['key' => 'value']]]]
145
+ } elseif (is_array ($ actionName )) { // 'stimulus_action' => ['controllerName' => ['eventName' => ['actionName' => ['key' => 'value']]]]
113
146
foreach ($ actionName as $ index => $ params ) {
114
147
$ this ->stimulusAttributes ->addAction ($ controllerName , $ index , $ eventName , $ params );
115
148
}
@@ -124,13 +157,13 @@ public function configureOptions(OptionsResolver $resolver): void
124
157
parent ::configureOptions ($ resolver );
125
158
126
159
$ resolver ->setDefaults ([
127
- 'stimulus_action ' => null ,
160
+ 'stimulus_action ' => null ,
128
161
'stimulus_controller ' => null ,
129
- 'stimulus_target ' => null ,
162
+ 'stimulus_target ' => null ,
130
163
]);
131
164
132
- $ resolver ->setAllowedTypes ('stimulus_action ' , ['string ' , 'array ' , 'null ' ]);
133
- $ resolver ->setAllowedTypes ('stimulus_controller ' , ['string ' , 'array ' , 'null ' ]);
134
- $ resolver ->setAllowedTypes ('stimulus_target ' , ['string ' , 'array ' , 'null ' ]);
165
+ $ resolver ->setAllowedTypes ('stimulus_action ' , ['string ' , 'array ' , 'callable ' , ' null ' ]);
166
+ $ resolver ->setAllowedTypes ('stimulus_controller ' , ['string ' , 'array ' , 'callable ' , ' null ' ]);
167
+ $ resolver ->setAllowedTypes ('stimulus_target ' , ['string ' , 'array ' , 'callable ' , ' null ' ]);
135
168
}
136
169
}
0 commit comments