Skip to content

Commit 1d55891

Browse files
author
matheo
committed
ComponentTemplateFinderInterface to isolate logic
1 parent 6a162a1 commit 1d55891

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

src/TwigComponent/src/ComponentFactory.php

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class ComponentFactory
3131
* @param array<class-string, string> $classMap
3232
*/
3333
public function __construct(
34-
private Environment $environment,
34+
private ComponentTemplateFinderInterface $componentTemplateFinder,
3535
private ServiceLocator $components,
3636
private PropertyAccessorInterface $propertyAccessor,
3737
private EventDispatcherInterface $eventDispatcher,
@@ -45,14 +45,14 @@ public function metadataFor(string $name): ComponentMetadata
4545
$name = $this->classMap[$name] ?? $name;
4646

4747
if (!$config = $this->config[$name] ?? null) {
48-
if (($template = $this->findAnonymousComponentTemplate($name)) !== null) {
48+
if (($template = $this->componentTemplateFinder->findAnonymousComponentTemplate($name)) !== null) {
4949
return new ComponentMetadata([
5050
'key' => $name,
5151
'template' => $template,
5252
]);
5353
}
5454

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);
5656
}
5757

5858
return new ComponentMetadata($config);
@@ -168,7 +168,7 @@ private function getComponent(string $name): object
168168
return new AnonymousComponent();
169169
}
170170

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);
172172
}
173173

174174
return $this->components->get($name);
@@ -210,38 +210,14 @@ private function postMount(object $component, array $data): array
210210

211211
private function isAnonymousComponent(string $name): bool
212212
{
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);
238214
}
239215

240216
/**
241217
* @return never
242218
*/
243219
private function throwUnknownComponentException(string $name): void
244220
{
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))));
246222
}
247223
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\UX\TwigComponent;
4+
5+
interface ComponentTemplateFinder
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\UX\TwigComponent;
4+
5+
interface ComponentTemplateFinderInterface
6+
{
7+
8+
}

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\UX\TwigComponent\ComponentRenderer;
2323
use Symfony\UX\TwigComponent\ComponentRendererInterface;
2424
use Symfony\UX\TwigComponent\ComponentStack;
25+
use Symfony\UX\TwigComponent\ComponentTemplateFinder;
26+
use Symfony\UX\TwigComponent\ComponentTemplateFinderInterface;
2527
use Symfony\UX\TwigComponent\DependencyInjection\Compiler\TwigComponentPass;
2628
use Symfony\UX\TwigComponent\Twig\ComponentExtension;
2729
use Symfony\UX\TwigComponent\Twig\ComponentLexer;
@@ -40,6 +42,14 @@ public function load(array $configs, ContainerBuilder $container): void
4042
throw new LogicException('The TwigBundle is not registered in your application. Try running "composer require symfony/twig-bundle".');
4143
}
4244

45+
$container->register('ux.twig_component.component_template_finder', ComponentTemplateFinder::class)
46+
->setArguments([
47+
new Reference('twig')
48+
])
49+
;
50+
51+
$container->setAlias(ComponentRendererInterface::class, 'ux.twig_component.component_renderer');
52+
4353
$container->registerAttributeForAutoconfiguration(
4454
AsTwigComponent::class,
4555
static function (ChildDefinition $definition, AsTwigComponent $attribute) {
@@ -49,7 +59,7 @@ static function (ChildDefinition $definition, AsTwigComponent $attribute) {
4959

5060
$container->register('ux.twig_component.component_factory', ComponentFactory::class)
5161
->setArguments([
52-
new Reference('twig'),
62+
new Reference('ux.twig_component.component_template_finder'),
5363
class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %s.', TwigComponentPass::class)) : null,
5464
new Reference('property_accessor'),
5565
new Reference('event_dispatcher'),
@@ -69,7 +79,7 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %
6979
])
7080
;
7181

72-
$container->setAlias(ComponentRendererInterface::class, 'ux.twig_component.component_renderer');
82+
$container->setAlias(ComponentTemplateFinderInterface::class, 'ux.twig_component.component_template_finder');
7383

7484
$container->register('ux.twig_component.twig.component_extension', ComponentExtension::class)
7585
->addTag('twig.extension')

0 commit comments

Comments
 (0)