1515use  Symfony \Component \DependencyInjection \ServiceLocator ;
1616use  Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
1717use  Symfony \Contracts \Service \ResetInterface ;
18- use  Symfony \UX \TwigComponent \Attribute \AsTwigComponent ;
1918use  Symfony \UX \TwigComponent \Event \PostMountEvent ;
2019use  Symfony \UX \TwigComponent \Event \PreMountEvent ;
2120
2726final  class  ComponentFactory implements  ResetInterface
2827{
2928    private  static  $ mountMethods  = [];
30-     private  static  $ preMountMethods  = [];
31-     private  static  $ postMountMethods  = [];
3229
3330    /** 
3431     * @param array<string, array>        $config 
@@ -151,23 +148,15 @@ private function mount(object $component, array &$data): void
151148
152149            return ;
153150        }
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 )) {
166153            return ;
167154        }
168- 
155+         
156+         $ mount  = self ::$ mountMethods [$ component ::class] ??= (new  \ReflectionClass ($ component ))->getMethod ('mount ' );
157+         
169158        $ parameters  = [];
170-         foreach  ($ mountMethod  ->getParameters () as  $ refParameter ) {
159+         foreach  ($ mount  ->getParameters () as  $ refParameter ) {
171160            if  (\array_key_exists ($ name  = $ refParameter ->getName (), $ data )) {
172161                $ parameters [] = $ data [$ name ];
173162                // 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
179168            }
180169        }
181170
182-         $ mountMethod  ->invoke ($ component , ...$ parameters );
171+         $ mount  ->invoke ($ component , ...$ parameters );
183172    }
184173
185174    private  function  preMount (object   $ component , array  $ data , ComponentMetadata   $ componentMetadata ): array 
@@ -188,10 +177,11 @@ private function preMount(object $component, array $data, ComponentMetadata $com
188177        $ this  ->eventDispatcher ->dispatch ($ event );
189178        $ data  = $ event ->getData ();
190179
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+                 }
195185            }
196186        }
197187
@@ -207,10 +197,11 @@ private function postMount(object $component, array $data, ComponentMetadata $co
207197        $ this  ->eventDispatcher ->dispatch ($ event );
208198        $ data  = $ event ->getData ();
209199
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+                 }
214205            }
215206        }
216207
@@ -257,7 +248,5 @@ private function throwUnknownComponentException(string $name): void
257248    public  function  reset (): void 
258249    {
259250        self ::$ mountMethods  = [];
260-         self ::$ preMountMethods  = [];
261-         self ::$ postMountMethods  = [];
262251    }
263252}
0 commit comments