@@ -41,32 +41,42 @@ public function __construct(
41
41
42
42
public function metadataFor (string $ name ): ComponentMetadata
43
43
{
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 );
52
60
}
53
61
54
- $ this -> throwUnknownComponentException ( $ name );
62
+ throw new \ InvalidArgumentException ( \sprintf ( ' Unknown component "%s". ' , $ name) );
55
63
}
56
64
57
- return new ComponentMetadata ( $ config );
65
+ $ this -> throwUnknownComponentException ( $ name );
58
66
}
59
67
60
68
/**
61
69
* Creates the component and "mounts" it with the passed data.
62
70
*/
63
71
public function create (string $ name , array $ data = []): MountedComponent
64
72
{
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 );
70
80
}
71
81
72
82
/**
@@ -101,10 +111,7 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
101
111
foreach ($ data as $ key => $ value ) {
102
112
if ($ value instanceof \Stringable) {
103
113
$ data [$ key ] = (string ) $ value ;
104
- continue ;
105
114
}
106
-
107
- $ data [$ key ] = $ value ;
108
115
}
109
116
110
117
return new MountedComponent (
@@ -118,10 +125,18 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
118
125
119
126
/**
120
127
* Returns the "unmounted" component.
128
+ *
129
+ * @internal
121
130
*/
122
131
public function get (string $ name ): object
123
132
{
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 ());
125
140
}
126
141
127
142
private function mount (object $ component , array &$ data ): void
@@ -159,21 +174,6 @@ private function mount(object $component, array &$data): void
159
174
$ component ->mount (...$ parameters );
160
175
}
161
176
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
-
177
177
private function preMount (object $ component , array $ data , ComponentMetadata $ componentMetadata ): array
178
178
{
179
179
$ event = new PreMountEvent ($ component , $ data , $ componentMetadata );
@@ -215,11 +215,6 @@ private function postMount(object $component, array $data, ComponentMetadata $co
215
215
];
216
216
}
217
217
218
- private function isAnonymousComponent (string $ name ): bool
219
- {
220
- return null !== $ this ->componentTemplateFinder ->findAnonymousComponentTemplate ($ name );
221
- }
222
-
223
218
/**
224
219
* @return never
225
220
*/
0 commit comments