-
-
Notifications
You must be signed in to change notification settings - Fork 364
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,24 +12,32 @@ | |
namespace Symfony\UX\TwigComponent; | ||
|
||
use Twig\Environment; | ||
use Twig\Loader\LoaderInterface; | ||
|
||
/** | ||
* @author Matheo Daninos <[email protected]> | ||
*/ | ||
final class ComponentTemplateFinder implements ComponentTemplateFinderInterface | ||
{ | ||
private readonly LoaderInterface $loader; | ||
|
||
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
use Symfony\UX\TwigComponent\ComponentTemplateFinder; | ||
use Twig\Environment; | ||
use Twig\Loader\ArrayLoader; | ||
use Twig\Loader\LoaderInterface; | ||
|
||
/** | ||
* @author Simon André <[email protected]> | ||
|
@@ -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')); | ||
|
@@ -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 | ||
*/ | ||
|
@@ -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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theEnvironment
type, then have an if statement that checks the type, and trigger deprecation if it is anEnvironmnet
).There was a problem hiding this comment.
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 :) )
There was a problem hiding this comment.
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 :)