15
15
use Symfony \Component \DependencyInjection \ServiceLocator ;
16
16
use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
17
17
use Symfony \Contracts \Service \ResetInterface ;
18
- use Symfony \UX \TwigComponent \Attribute \AsTwigComponent ;
19
18
use Symfony \UX \TwigComponent \Event \PostMountEvent ;
20
19
use Symfony \UX \TwigComponent \Event \PreMountEvent ;
21
20
27
26
final class ComponentFactory implements ResetInterface
28
27
{
29
28
private static $ mountMethods = [];
30
- private static $ preMountMethods = [];
31
- private static $ postMountMethods = [];
32
29
33
30
/**
34
31
* @param array<string, array> $config
@@ -151,23 +148,15 @@ private function mount(object $component, array &$data): void
151
148
152
149
return ;
153
150
}
154
-
155
- if (null === (self ::$ mountMethods [$ component ::class] ?? null )) {
156
- try {
157
- $ mountMethod = self ::$ mountMethods [$ component ::class] = (new \ReflectionClass ($ component ))->getMethod ('mount ' );
158
- } catch (\ReflectionException ) {
159
- self ::$ mountMethods [$ component ::class] = false ;
160
-
161
- return ;
162
- }
163
- }
164
-
165
- if (false === $ mountMethod ??= self ::$ mountMethods [$ component ::class]) {
151
+
152
+ if (!($ this ->config [$ component ::class]['mount ' ] ?? false )) {
166
153
return ;
167
154
}
168
-
155
+
156
+ $ mount = self ::$ mountMethods [$ component ::class] ??= (new \ReflectionClass ($ component ))->getMethod ('mount ' );
157
+
169
158
$ parameters = [];
170
- foreach ($ mountMethod ->getParameters () as $ refParameter ) {
159
+ foreach ($ mount ->getParameters () as $ refParameter ) {
171
160
if (\array_key_exists ($ name = $ refParameter ->getName (), $ data )) {
172
161
$ parameters [] = $ data [$ name ];
173
162
// remove the data element so it isn't used to set the property directly.
@@ -179,7 +168,7 @@ private function mount(object $component, array &$data): void
179
168
}
180
169
}
181
170
182
- $ mountMethod ->invoke ($ component , ...$ parameters );
171
+ $ mount ->invoke ($ component , ...$ parameters );
183
172
}
184
173
185
174
private function preMount (object $ component , array $ data , ComponentMetadata $ componentMetadata ): array
@@ -188,10 +177,11 @@ private function preMount(object $component, array $data, ComponentMetadata $com
188
177
$ this ->eventDispatcher ->dispatch ($ event );
189
178
$ data = $ event ->getData ();
190
179
191
- $ methods = self ::$ preMountMethods [$ component ::class] ??= AsTwigComponent::preMountMethods ($ component ::class);
192
- foreach ($ methods as $ method ) {
193
- if (null !== $ newData = $ method ->invoke ($ component , $ data )) {
194
- $ data = $ newData ;
180
+ if ($ preMounts = $ this ->config [$ component ::class]['preMount ' ] ?? []) {
181
+ foreach ($ preMounts as $ preMount ) {
182
+ if (null !== $ newData = $ component ->$ preMount ($ data )) {
183
+ $ data = $ newData ;
184
+ }
195
185
}
196
186
}
197
187
@@ -207,10 +197,11 @@ private function postMount(object $component, array $data, ComponentMetadata $co
207
197
$ this ->eventDispatcher ->dispatch ($ event );
208
198
$ data = $ event ->getData ();
209
199
210
- $ methods = self ::$ postMountMethods [$ component ::class] ??= AsTwigComponent::postMountMethods ($ component ::class);
211
- foreach ($ methods as $ method ) {
212
- if (null !== $ newData = $ method ->invoke ($ component , $ data )) {
213
- $ data = $ newData ;
200
+ if ($ postMounts = $ this ->config [$ component ::class]['postMount ' ] ?? []) {
201
+ foreach ($ postMounts as $ postMount ) {
202
+ if (null !== $ newData = $ component ->$ postMount ($ data )) {
203
+ $ data = $ newData ;
204
+ }
214
205
}
215
206
}
216
207
@@ -257,7 +248,5 @@ private function throwUnknownComponentException(string $name): void
257
248
public function reset (): void
258
249
{
259
250
self ::$ mountMethods = [];
260
- self ::$ preMountMethods = [];
261
- self ::$ postMountMethods = [];
262
251
}
263
252
}
0 commit comments