-
-
Notifications
You must be signed in to change notification settings - Fork 399
[TwigComponent] Fix loading Twig Components by FQCN #3196
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,8 @@ public function __construct( | |
|
|
||
| public function metadataFor(string $name): ComponentMetadata | ||
| { | ||
| $name = $this->classMap[$name] ?? $name; | ||
|
|
||
| if ($config = $this->config[$name] ?? null) { | ||
| return new ComponentMetadata($config); | ||
| } | ||
|
|
@@ -59,14 +61,6 @@ public function metadataFor(string $name): ComponentMetadata | |
| return new ComponentMetadata($this->config[$name]); | ||
| } | ||
|
|
||
| if ($mappedName = $this->classMap[$name] ?? null) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. classMap is usefull
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's why this PR proposes to evaluate it before, not after trying to load the component anonymously |
||
| if ($config = $this->config[$mappedName] ?? null) { | ||
| return new ComponentMetadata($config); | ||
| } | ||
|
|
||
| throw new \InvalidArgumentException(\sprintf('Unknown component "%s".', $name)); | ||
| } | ||
|
Comment on lines
-62
to
-68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand if you remove all of this this method only search for anonymous component then? It's mean something is wrong with the current architecture and we don't need to separate the finding of the template of the anonymous component and the normal component
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course we do! One is compilable once, the other is/may not be :)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No! The first thing it does is trying to create a "normal" component. (see https://github.com/symfony/ux/pull/3196/files#r2591624694) Only if this doesn't work, it tries to create an anonymous component. Opposed to how it worked before: First, it tried to create a "normal" component. If that didn't work using the identifier, it tried to load an anonymous component. And only if both of those didn't work, it had the same code for loading a "normal" component again, just this time with the resolved class map. Now we resolve the class map from the beginning on, so we don't need to repeat the code just to do the same thing again with the name from the resolved class map. |
||
|
|
||
| $this->throwUnknownComponentException($name); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,4 +93,32 @@ public function testMetadataForReuseAnonymousConfig() | |
| $this->assertSame('foo', $metadata->getName()); | ||
| $this->assertSame('foo.html.twig', $metadata->getTemplate()); | ||
| } | ||
|
|
||
| /** | ||
| * @testWith ["bar"] | ||
| * ["Foo\\Bar"] | ||
| */ | ||
|
Comment on lines
+97
to
+100
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can remove the comment here
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand. Should I replace
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment must be kept for PHPUnit 9 compatibility, but |
||
| public function testPrefersClassComponentOverAnonymous(string $name) | ||
| { | ||
| $templateFinder = $this->createMock(ComponentTemplateFinderInterface::class); | ||
| $templateFinder->expects($this->never()) | ||
| ->method('findAnonymousComponentTemplate'); | ||
|
|
||
| $factory = new ComponentFactory( | ||
| $templateFinder, | ||
| $this->createMock(ServiceLocator::class), | ||
| $this->createMock(PropertyAccessorInterface::class), | ||
| $this->createMock(EventDispatcherInterface::class), | ||
| [ | ||
| 'bar' => ['key' => 'bar', 'template' => 'bar.html.twig'], | ||
| ], | ||
| ['Foo\\Bar' => 'bar'], | ||
| $this->createMock(Environment::class), | ||
| ); | ||
|
|
||
| $metadata = $factory->metadataFor($name); | ||
|
|
||
| $this->assertSame('bar', $metadata->getName()); | ||
| $this->assertSame('bar.html.twig', $metadata->getTemplate()); | ||
| } | ||
| } | ||
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.
This is the place where "normal" (non-anonymous) components get created. If we resolve the classMap first and save it in
$name, there's no need for the duplication I removed.