@@ -41,32 +41,42 @@ public function __construct(
4141
4242 public function metadataFor (string $ name ): ComponentMetadata
4343 {
44- $ name = $ this ->classMap [$ name ] ?? $ name ;
45-
46- if (!$ config = $ this ->config [$ name ] ?? null ) {
47- if (($ template = $ this ->componentTemplateFinder ->findAnonymousComponentTemplate ($ name )) !== null ) {
48- return new ComponentMetadata ([
49- 'key ' => $ name ,
50- 'template ' => $ template ,
51- ]);
44+ if ($ config = $ this ->config [$ name ] ?? null ) {
45+ return new ComponentMetadata ($ config );
46+ }
47+
48+ if ($ template = $ this ->componentTemplateFinder ->findAnonymousComponentTemplate ($ name )) {
49+ $ this ->config [$ name ] = [
50+ 'key ' => $ name ,
51+ 'template ' => $ template ,
52+ ];
53+
54+ return new ComponentMetadata ($ this ->config [$ name ]);
55+ }
56+
57+ if ($ mappedName = $ this ->classMap [$ name ] ?? null ) {
58+ if ($ config = $ this ->config [$ mappedName ] ?? null ) {
59+ return new ComponentMetadata ($ config );
5260 }
5361
54- $ this -> throwUnknownComponentException ( $ name );
62+ throw new \ InvalidArgumentException ( \sprintf ( ' Unknown component "%s". ' , $ name) );
5563 }
5664
57- return new ComponentMetadata ( $ config );
65+ $ this -> throwUnknownComponentException ( $ name );
5866 }
5967
6068 /**
6169 * Creates the component and "mounts" it with the passed data.
6270 */
6371 public function create (string $ name , array $ data = []): MountedComponent
6472 {
65- return $ this ->mountFromObject (
66- $ this ->getComponent ($ name ),
67- $ data ,
68- $ this ->metadataFor ($ name )
69- );
73+ $ metadata = $ this ->metadataFor ($ name );
74+
75+ if ($ metadata ->isAnonymous ()) {
76+ return $ this ->mountFromObject (new AnonymousComponent (), $ data , $ metadata );
77+ }
78+
79+ return $ this ->mountFromObject ($ this ->components ->get ($ metadata ->getName ()), $ data , $ metadata );
7080 }
7181
7282 /**
@@ -101,10 +111,7 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
101111 foreach ($ data as $ key => $ value ) {
102112 if ($ value instanceof \Stringable) {
103113 $ data [$ key ] = (string ) $ value ;
104- continue ;
105114 }
106-
107- $ data [$ key ] = $ value ;
108115 }
109116
110117 return new MountedComponent (
@@ -118,10 +125,18 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
118125
119126 /**
120127 * Returns the "unmounted" component.
128+ *
129+ * @internal
121130 */
122131 public function get (string $ name ): object
123132 {
124- return $ this ->getComponent ($ name );
133+ $ metadata = $ this ->metadataFor ($ name );
134+
135+ if ($ metadata ->isAnonymous ()) {
136+ return new AnonymousComponent ();
137+ }
138+
139+ return $ this ->components ->get ($ metadata ->getName ());
125140 }
126141
127142 private function mount (object $ component , array &$ data ): void
@@ -159,21 +174,6 @@ private function mount(object $component, array &$data): void
159174 $ component ->mount (...$ parameters );
160175 }
161176
162- private function getComponent (string $ name ): object
163- {
164- $ name = $ this ->classMap [$ name ] ?? $ name ;
165-
166- if (!$ this ->components ->has ($ name )) {
167- if ($ this ->isAnonymousComponent ($ name )) {
168- return new AnonymousComponent ();
169- }
170-
171- $ this ->throwUnknownComponentException ($ name );
172- }
173-
174- return $ this ->components ->get ($ name );
175- }
176-
177177 private function preMount (object $ component , array $ data , ComponentMetadata $ componentMetadata ): array
178178 {
179179 $ event = new PreMountEvent ($ component , $ data , $ componentMetadata );
@@ -215,11 +215,6 @@ private function postMount(object $component, array $data, ComponentMetadata $co
215215 ];
216216 }
217217
218- private function isAnonymousComponent (string $ name ): bool
219- {
220- return null !== $ this ->componentTemplateFinder ->findAnonymousComponentTemplate ($ name );
221- }
222-
223218 /**
224219 * @return never
225220 */
0 commit comments