Skip to content
Closed
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
10 changes: 2 additions & 8 deletions src/TwigComponent/src/ComponentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Comment on lines 51 to 53
Copy link
Author

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.

Expand All @@ -59,14 +61,6 @@ public function metadataFor(string $name): ComponentMetadata
return new ComponentMetadata($this->config[$name]);
}

if ($mappedName = $this->classMap[$name] ?? null) {
Copy link
Member

Choose a reason for hiding this comment

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

classMap is usefull

Copy link
Author

@janopae janopae Dec 1, 2025

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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 :)

Copy link
Author

@janopae janopae Dec 2, 2025

Choose a reason for hiding this comment

The 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?

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);
}

Expand Down
28 changes: 28 additions & 0 deletions src/TwigComponent/tests/Unit/ComponentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

you can remove the comment here

Copy link
Author

@janopae janopae Dec 1, 2025

Choose a reason for hiding this comment

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

I don't understand. Should I replace @testWith with an attribute or a dataprovider?

Copy link
Member

Choose a reason for hiding this comment

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

The comment must be kept for PHPUnit 9 compatibility, but #[TestWith(...)] must be added for PHPUnit 10+ (UX 3.x)

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());
}
}
Loading