Skip to content

Commit a0c2003

Browse files
committed
[TwigComponent] Fix twig:lint bug with anonymous component tag
1 parent 340edb7 commit a0c2003

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/TwigComponent/src/ComponentTemplateFinder.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@
1212
namespace Symfony\UX\TwigComponent;
1313

1414
use Twig\Environment;
15+
use Twig\Loader\LoaderInterface;
1516

1617
/**
1718
* @author Matheo Daninos <[email protected]>
1819
*/
1920
final class ComponentTemplateFinder implements ComponentTemplateFinderInterface
2021
{
22+
/**
23+
* Avoid bug when the lint:command changes the Environment Loader.
24+
*
25+
* @internal
26+
*/
27+
private LoaderInterface $loader;
28+
2129
public function __construct(
2230
private Environment $environment,
2331
private readonly ?string $directory = null,
@@ -29,7 +37,7 @@ public function __construct(
2937

3038
public function findAnonymousComponentTemplate(string $name): ?string
3139
{
32-
$loader = $this->environment->getLoader();
40+
$loader = $this->getLoader();
3341
$componentPath = rtrim(str_replace(':', '/', $name));
3442

3543
// Legacy auto-naming rules < 2.13
@@ -60,4 +68,20 @@ public function findAnonymousComponentTemplate(string $name): ?string
6068

6169
return null;
6270
}
71+
72+
/**
73+
* @internal
74+
*/
75+
public function setLoader(LoaderInterface $loader): void
76+
{
77+
$this->loader = $loader;
78+
}
79+
80+
/**
81+
* @internal
82+
*/
83+
private function getLoader(): LoaderInterface
84+
{
85+
return $this->loader ??= $this->environment->getLoader();
86+
}
6387
}

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function load(array $configs, ContainerBuilder $container): void
6868
new Reference('twig'),
6969
$config['anonymous_template_directory'],
7070
])
71-
;
71+
->addMethodCall('setLoader', [new Reference('twig.loader')]);
7272

7373
$container->setAlias(ComponentRendererInterface::class, 'ux.twig_component.component_renderer');
7474

@@ -101,8 +101,6 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %
101101
])
102102
;
103103

104-
$container->register(ComponentTemplateFinder::class, 'ux.twig_component.component_template_finder');
105-
106104
$container->register('ux.twig_component.twig.component_extension', ComponentExtension::class)
107105
->addTag('twig.extension')
108106
->addTag('container.service_subscriber', ['key' => ComponentRenderer::class, 'id' => 'ux.twig_component.component_renderer'])

0 commit comments

Comments
 (0)