Skip to content

[TwigComponent] Fix twig:lint bug with anonymous component tag #1199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/TwigComponent/src/ComponentTemplateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@
namespace Symfony\UX\TwigComponent;

use Twig\Environment;
use Twig\Loader\LoaderInterface;

/**
* @author Matheo Daninos <matheo.daninos@gmail.com>
*/
final class ComponentTemplateFinder implements ComponentTemplateFinderInterface
{
private readonly LoaderInterface $loader;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we, instead, replace the $environment constructor argument with $loader? We would just need a BC layer (remove the Environment type, then have an if statement that checks the type, and trigger deprecation if it is an Environmnet).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Environment|LoaderInterface wrong ? (real question, i'm not really used to BC break handling :) )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that will work - I still forget we can do this now :)

public function __construct(
private Environment $environment,
Environment|LoaderInterface $loader,
private readonly ?string $directory = null,
) {
if ($loader instanceof Environment) {
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);
$loader = $loader->getLoader();
}
$this->loader = $loader;
if (null === $this->directory) {
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__);
}
}

public function findAnonymousComponentTemplate(string $name): ?string
{
$loader = $this->environment->getLoader();
$loader = $this->loader;
$componentPath = rtrim(str_replace(':', '/', $name));

// Legacy auto-naming rules < 2.13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ public function load(array $configs, ContainerBuilder $container): void

$container->register('ux.twig_component.component_template_finder', ComponentTemplateFinder::class)
->setArguments([
new Reference('twig'),
new Reference('twig.loader'),
$config['anonymous_template_directory'],
])
;

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

$container->registerAttributeForAutoconfiguration(
Expand Down Expand Up @@ -101,8 +99,6 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %
])
;

$container->register(ComponentTemplateFinder::class, 'ux.twig_component.component_template_finder');

$container->register('ux.twig_component.twig.component_extension', ComponentExtension::class)
->addTag('twig.extension')
->addTag('container.service_subscriber', ['key' => ComponentRenderer::class, 'id' => 'ux.twig_component.component_renderer'])
Expand Down
29 changes: 22 additions & 7 deletions src/TwigComponent/tests/Unit/ComponentTemplateFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\UX\TwigComponent\ComponentTemplateFinder;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Loader\LoaderInterface;

/**
* @author Simon André <smn.andre@gmail.com>
Expand All @@ -36,8 +37,8 @@ public function testFindTemplate(): void
'components/b.html.twig',
'components/c',
];
$environment = $this->createEnvironment($templates);
$finder = new ComponentTemplateFinder($environment, 'components');
$loader = $this->createLoader($templates);
$finder = new ComponentTemplateFinder($loader, 'components');

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

/**
* @group legacy
*/
public function testTriggerDeprecationWhenEnvironmentAsFirstArgument(): void
{
$environment = $this->createEnvironment([]);

$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.');
$finder = new ComponentTemplateFinder($environment, 'foo');
}

/**
* @group legacy
*/
Expand All @@ -106,18 +118,21 @@ public function testFindTemplateWithinDirectory(): void
'bar/foo/bar.html.twig',
'foo/foo/bar.html.twig',
];
$environment = $this->createEnvironment($templates);
$finder = new ComponentTemplateFinder($environment, 'foo');
$loader = $this->createLoader($templates);
$finder = new ComponentTemplateFinder($loader, 'foo');

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

private function createEnvironment(array $templates): Environment
private function createLoader(array $templates): LoaderInterface
{
$loader = new ArrayLoader(array_combine($templates, $templates));
return new ArrayLoader(array_combine($templates, $templates));
}

return new Environment($loader);
private function createEnvironment(array $templates): Environment
{
return new Environment($this->createLoader($templates));
}
}