@@ -31,7 +31,7 @@ final class ComponentFactory
31
31
* @param array<class-string, string> $classMap
32
32
*/
33
33
public function __construct (
34
- private Environment $ environment ,
34
+ private ComponentTemplateFinderInterface $ componentTemplateFinder ,
35
35
private ServiceLocator $ components ,
36
36
private PropertyAccessorInterface $ propertyAccessor ,
37
37
private EventDispatcherInterface $ eventDispatcher ,
@@ -45,14 +45,14 @@ public function metadataFor(string $name): ComponentMetadata
45
45
$ name = $ this ->classMap [$ name ] ?? $ name ;
46
46
47
47
if (!$ config = $ this ->config [$ name ] ?? null ) {
48
- if (($ template = $ this ->findAnonymousComponentTemplate ($ name )) !== null ) {
48
+ if (($ template = $ this ->componentTemplateFinder -> findAnonymousComponentTemplate ($ name )) !== null ) {
49
49
return new ComponentMetadata ([
50
50
'key ' => $ name ,
51
51
'template ' => $ template ,
52
52
]);
53
53
}
54
54
55
- throw new \ InvalidArgumentException ( sprintf ( ' Unknown component "%s". The registered components are: %s. And no template anonymous component founded ' , $ name , implode ( ' , ' , array_keys ( $ this ->config ))) );
55
+ $ this ->throwUnknownComponentException ( $ name );
56
56
}
57
57
58
58
return new ComponentMetadata ($ config );
@@ -168,7 +168,7 @@ private function getComponent(string $name): object
168
168
return new AnonymousComponent ();
169
169
}
170
170
171
- throw new \ InvalidArgumentException ( sprintf ( ' Unknown component "%s". The registered components are: %s. And no anonymous component founded ' , $ name , implode ( ' , ' , array_keys ( $ this ->components -> getProvidedServices ()))) );
171
+ $ this ->throwUnknownComponentException ( $ name );
172
172
}
173
173
174
174
return $ this ->components ->get ($ name );
@@ -210,38 +210,14 @@ private function postMount(object $component, array $data): array
210
210
211
211
private function isAnonymousComponent (string $ name ): bool
212
212
{
213
- return null !== $ this ->findAnonymousComponentTemplate ($ name );
214
- }
215
-
216
- public function findAnonymousComponentTemplate (string $ name ): ?string
217
- {
218
- $ loader = $ this ->environment ->getLoader ();
219
- $ componentPath = rtrim (str_replace (': ' , '/ ' , $ name ));
220
-
221
- if ($ loader ->exists ($ componentPath )) {
222
- return $ componentPath ;
223
- }
224
-
225
- if ($ loader ->exists ($ componentPath .'.html.twig ' )) {
226
- return $ componentPath .'.html.twig ' ;
227
- }
228
-
229
- if ($ loader ->exists ('components/ ' .$ componentPath )) {
230
- return 'components/ ' .$ componentPath ;
231
- }
232
-
233
- if ($ loader ->exists ('/components/ ' .$ componentPath .'.html.twig ' )) {
234
- return '/components/ ' .$ componentPath .'.html.twig ' ;
235
- }
236
-
237
- return null ;
213
+ return null !== $ this ->componentTemplateFinder ->findAnonymousComponentTemplate ($ name );
238
214
}
239
215
240
216
/**
241
217
* @return never
242
218
*/
243
219
private function throwUnknownComponentException (string $ name ): void
244
220
{
245
- throw new \InvalidArgumentException (sprintf ('Unknown component "%s". The registered components are: %s ' , $ name , implode (', ' , array_keys ($ this ->config ))));
221
+ throw new \InvalidArgumentException (sprintf ('Unknown component "%s". The registered components are: %s. And no matching anonymous component template was found ' , $ name , implode (', ' , array_keys ($ this ->config ))));
246
222
}
247
223
}
0 commit comments