Skip to content

Commit e44893f

Browse files
committed
Fix twig:lint bug with anonymous component tag
1 parent a0c2003 commit e44893f

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

src/TwigComponent/src/ComponentTemplateFinder.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@
1919
*/
2020
final class ComponentTemplateFinder implements ComponentTemplateFinderInterface
2121
{
22-
/**
23-
* Avoid bug when the lint:command changes the Environment Loader.
24-
*
25-
* @internal
26-
*/
27-
private LoaderInterface $loader;
22+
private readonly LoaderInterface $loader;
2823

2924
public function __construct(
30-
private Environment $environment,
25+
Environment|LoaderInterface $loader,
3126
private readonly ?string $directory = null,
3227
) {
28+
if ($loader instanceof Environment) {
29+
trigger_deprecation('symfony/ux-twig-component', '2.13', 'The "%s()" method will require "%s $loader" as first argument in 3.0. Passing an "Environment" instance is deprecated.', __METHOD__, LoaderInterface::class);
30+
$loader = $loader->getLoader();
31+
}
32+
$this->loader = $loader;
3333
if (null === $this->directory) {
3434
trigger_deprecation('symfony/ux-twig-component', '2.13', 'The "%s()" method will require "string $directory" argument in 3.0. Not defining it or passing null is deprecated.', __METHOD__);
3535
}
3636
}
3737

3838
public function findAnonymousComponentTemplate(string $name): ?string
3939
{
40-
$loader = $this->getLoader();
40+
$loader = $this->loader;
4141
$componentPath = rtrim(str_replace(':', '/', $name));
4242

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

6969
return null;
7070
}
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-
}
8771
}

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,9 @@ public function load(array $configs, ContainerBuilder $container): void
6565

6666
$container->register('ux.twig_component.component_template_finder', ComponentTemplateFinder::class)
6767
->setArguments([
68-
new Reference('twig'),
68+
new Reference('twig.loader'),
6969
$config['anonymous_template_directory'],
70-
])
71-
->addMethodCall('setLoader', [new Reference('twig.loader')]);
72-
70+
]);
7371
$container->setAlias(ComponentRendererInterface::class, 'ux.twig_component.component_renderer');
7472

7573
$container->registerAttributeForAutoconfiguration(

src/TwigComponent/tests/Unit/ComponentTemplateFinderTest.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\UX\TwigComponent\ComponentTemplateFinder;
1717
use Twig\Environment;
1818
use Twig\Loader\ArrayLoader;
19+
use Twig\Loader\LoaderInterface;
1920

2021
/**
2122
* @author Simon André <[email protected]>
@@ -36,8 +37,8 @@ public function testFindTemplate(): void
3637
'components/b.html.twig',
3738
'components/c',
3839
];
39-
$environment = $this->createEnvironment($templates);
40-
$finder = new ComponentTemplateFinder($environment, 'components');
40+
$loader = $this->createLoader($templates);
41+
$finder = new ComponentTemplateFinder($loader, 'components');
4142

4243
$this->assertEquals('components/aa.html.twig', $finder->findAnonymousComponentTemplate('aa'));
4344
$this->assertEquals('components/aa/bb.html.twig', $finder->findAnonymousComponentTemplate('aa:bb'));
@@ -84,6 +85,17 @@ public function testFindTemplateWithLegacyAutonaming(): void
8485
$this->assertNull($finder->findAnonymousComponentTemplate('nope:bar'));
8586
}
8687

88+
/**
89+
* @group legacy
90+
*/
91+
public function testTriggerDeprecationWhenEnvironmentAsFirstArgument(): void
92+
{
93+
$environment = $this->createEnvironment([]);
94+
95+
$this->expectDeprecation('Since symfony/ux-twig-component 2.13: The "Symfony\UX\TwigComponent\ComponentTemplateFinder::__construct()" method will require "Twig\Loader\LoaderInterface $loader" as first argument in 3.0. Passing an "Environment" instance is deprecated.');
96+
$finder = new ComponentTemplateFinder($environment, 'foo');
97+
}
98+
8799
/**
88100
* @group legacy
89101
*/
@@ -106,18 +118,21 @@ public function testFindTemplateWithinDirectory(): void
106118
'bar/foo/bar.html.twig',
107119
'foo/foo/bar.html.twig',
108120
];
109-
$environment = $this->createEnvironment($templates);
110-
$finder = new ComponentTemplateFinder($environment, 'foo');
121+
$loader = $this->createLoader($templates);
122+
$finder = new ComponentTemplateFinder($loader, 'foo');
111123

112124
$this->assertEquals('foo/bar.html.twig', $finder->findAnonymousComponentTemplate('bar'));
113125
$this->assertEquals('foo/foo/bar.html.twig', $finder->findAnonymousComponentTemplate('foo:bar'));
114126
$this->assertEquals('foo/foo/bar.html.twig', $finder->findAnonymousComponentTemplate('foo:bar'));
115127
}
116128

117-
private function createEnvironment(array $templates): Environment
129+
private function createLoader(array $templates): LoaderInterface
118130
{
119-
$loader = new ArrayLoader(array_combine($templates, $templates));
131+
return new ArrayLoader(array_combine($templates, $templates));
132+
}
120133

121-
return new Environment($loader);
134+
private function createEnvironment(array $templates): Environment
135+
{
136+
return new Environment($this->createLoader($templates));
122137
}
123138
}

0 commit comments

Comments
 (0)