Skip to content

Commit 7ad44cf

Browse files
smnandrefabpot
authored andcommitted
Remove ComponentAttributeFactory and inject EscaperRuntime directly
1 parent f7183b7 commit 7ad44cf

File tree

7 files changed

+32
-29
lines changed

7 files changed

+32
-29
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"symfony/property-access": "^5.4.5|^6.0|^7.0",
3232
"symfony/property-info": "^5.4|^6.0|^7.0",
3333
"symfony/stimulus-bundle": "^2.9",
34-
"symfony/ux-twig-component": "^2.25",
34+
"symfony/ux-twig-component": "^2.25.1",
3535
"twig/twig": "^3.10.3"
3636
},
3737
"require-dev": {

src/DependencyInjection/LiveComponentExtension.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1616
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1717
use Symfony\Component\Config\Definition\ConfigurationInterface;
18-
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
1918
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
2019
use Symfony\Component\DependencyInjection\ChildDefinition;
2120
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -110,7 +109,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
110109
new Reference('ux.live_component.metadata_factory'),
111110
new Reference('serializer', ContainerInterface::NULL_ON_INVALID_REFERENCE),
112111
$config['secret'], // defaults to '%kernel.secret%'
113-
new Reference('ux.twig_component.component_attributes_factory'),
112+
new Reference('twig'),
114113
])
115114
;
116115

@@ -158,7 +157,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
158157
->setArguments([
159158
new Reference('ux.live_component.fingerprint_calculator'),
160159
new Reference('ux.live_component.attribute_helper_factory'),
161-
new Reference('ux.twig_component.component_attributes_factory'),
160+
new Reference('twig'),
162161
])
163162
->addTag('container.service_subscriber', ['key' => ComponentFactory::class, 'id' => 'ux.twig_component.component_factory'])
164163
->addTag('container.service_subscriber', ['key' => LiveComponentMetadataFactory::class, 'id' => 'ux.live_component.metadata_factory'])
@@ -219,7 +218,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
219218
->setArguments([
220219
new Reference('ux.twig_component.component_stack'),
221220
new Reference('ux.live_component.twig.template_mapper'),
222-
new Reference('ux.twig_component.component_attributes_factory'),
221+
new Reference('twig'),
223222
])
224223
->addTag('kernel.event_subscriber')
225224
->addTag('container.service_subscriber', ['key' => LiveControllerAttributesCreator::class, 'id' => 'ux.live_component.live_controller_attributes_creator'])

src/EventListener/AddLiveAttributesSubscriber.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
use Symfony\UX\LiveComponent\Twig\TemplateMap;
1818
use Symfony\UX\LiveComponent\Util\LiveControllerAttributesCreator;
1919
use Symfony\UX\TwigComponent\ComponentAttributes;
20-
use Symfony\UX\TwigComponent\ComponentAttributesFactory;
2120
use Symfony\UX\TwigComponent\ComponentMetadata;
2221
use Symfony\UX\TwigComponent\ComponentStack;
2322
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
2423
use Symfony\UX\TwigComponent\MountedComponent;
24+
use Twig\Environment;
25+
use Twig\Runtime\EscaperRuntime;
2526

2627
/**
2728
* Adds the extra attributes needed to activate a live controller.
@@ -37,7 +38,7 @@ final class AddLiveAttributesSubscriber implements EventSubscriberInterface, Ser
3738
public function __construct(
3839
private ComponentStack $componentStack,
3940
private TemplateMap $templateMap,
40-
private readonly ComponentAttributesFactory $componentAttributesFactory,
41+
private readonly Environment $twig,
4142
private ContainerInterface $container,
4243
) {
4344
}
@@ -107,6 +108,6 @@ private function getLiveAttributes(MountedComponent $mounted, ComponentMetadata
107108
$this->componentStack->hasParentComponent()
108109
);
109110

110-
return $this->componentAttributesFactory->create($attributesCollection->toArray());
111+
return new ComponentAttributes($attributesCollection->toArray(), $this->twig->getRuntime(EscaperRuntime::class));
111112
}
112113
}

src/LiveComponentHydrator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
use Symfony\UX\LiveComponent\Metadata\LivePropMetadata;
3333
use Symfony\UX\LiveComponent\Util\DehydratedProps;
3434
use Symfony\UX\TwigComponent\ComponentAttributes;
35-
use Symfony\UX\TwigComponent\ComponentAttributesFactory;
35+
use Twig\Environment;
36+
use Twig\Runtime\EscaperRuntime;
3637

3738
/**
3839
* @author Kevin Bond <kevinbond@gmail.com>
@@ -53,7 +54,7 @@ public function __construct(
5354
private LiveComponentMetadataFactory $liveComponentMetadataFactory,
5455
private NormalizerInterface|DenormalizerInterface|null $serializer,
5556
#[\SensitiveParameter] private string $secret,
56-
private readonly ComponentAttributesFactory $componentAttributesFactory,
57+
private readonly Environment $twig,
5758
) {
5859
if (!$secret) {
5960
throw new \InvalidArgumentException('A non-empty secret is required.');
@@ -146,7 +147,7 @@ public function hydrate(object $component, array $props, array $updatedProps, Li
146147
$dehydratedOriginalProps = $this->combineAndValidateProps($props, $updatedPropsFromParent);
147148
$dehydratedUpdatedProps = DehydratedProps::createFromUpdatedArray($updatedProps);
148149

149-
$attributes = $this->componentAttributesFactory->create($dehydratedOriginalProps->getPropValue(self::ATTRIBUTES_KEY, []));
150+
$attributes = new ComponentAttributes($dehydratedOriginalProps->getPropValue(self::ATTRIBUTES_KEY, []), $this->twig->getRuntime(EscaperRuntime::class));
150151
$dehydratedOriginalProps->removePropValue(self::ATTRIBUTES_KEY);
151152

152153
$needProcessOnUpdatedHooks = [];

src/Util/ChildComponentPartialRenderer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use Symfony\Contracts\Service\ServiceSubscriberInterface;
1616
use Symfony\UX\LiveComponent\LiveComponentHydrator;
1717
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
18-
use Symfony\UX\TwigComponent\ComponentAttributesFactory;
18+
use Symfony\UX\TwigComponent\ComponentAttributes;
1919
use Symfony\UX\TwigComponent\ComponentFactory;
20+
use Twig\Environment;
21+
use Twig\Runtime\EscaperRuntime;
2022

2123
/**
2224
* @author Ryan Weaver <ryan@symfonycasts.com>
@@ -28,7 +30,7 @@ class ChildComponentPartialRenderer implements ServiceSubscriberInterface
2830
public function __construct(
2931
private FingerprintCalculator $fingerprintCalculator,
3032
private TwigAttributeHelperFactory $attributeHelperFactory,
31-
private ComponentAttributesFactory $componentAttributesFactory,
33+
private Environment $twig,
3234
private ContainerInterface $container,
3335
) {
3436
}
@@ -85,7 +87,7 @@ public function renderChildComponent(string $deterministicId, string $currentPro
8587
private function createHtml(array $attributes, string $childTag): string
8688
{
8789
$attributes['data-live-preserve'] = true;
88-
$attributes = $this->componentAttributesFactory->create($attributes);
90+
$attributes = new ComponentAttributes($attributes, $this->twig->getRuntime(EscaperRuntime::class));
8991

9092
return \sprintf('<%s%s></%s>', $childTag, $attributes, $childTag);
9193
}

tests/Integration/LiveComponentHydratorTest.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343
use Symfony\UX\LiveComponent\Tests\Fixtures\Enum\ZeroIntEnum;
4444
use Symfony\UX\LiveComponent\Tests\LiveComponentTestHelper;
4545
use Symfony\UX\TwigComponent\ComponentAttributes;
46-
use Symfony\UX\TwigComponent\ComponentAttributesFactory;
4746
use Symfony\UX\TwigComponent\ComponentMetadata;
47+
use Twig\Environment;
48+
use Twig\Runtime\EscaperRuntime;
4849
use Zenstruck\Foundry\Test\Factories;
4950
use Zenstruck\Foundry\Test\ResetDatabase;
5051

@@ -76,9 +77,9 @@ private function executeHydrationTestCase(callable $testFactory, ?int $minPhpVer
7677
$metadataFactory = self::getContainer()->get('ux.live_component.metadata_factory');
7778
\assert($metadataFactory instanceof LiveComponentMetadataFactory);
7879
$testCase = $testBuilder->getTest($metadataFactory);
79-
80-
$componentAttributesFactory = self::getContainer()->get('ux.twig_component.component_attributes_factory');
81-
\assert($componentAttributesFactory instanceof ComponentAttributesFactory);
80+
81+
$twig = self::getContainer()->get('twig');
82+
\assert($twig instanceof Environment);
8283

8384
// keep a copy of the original, empty component object for hydration later
8485
$originalComponentWithData = clone $testCase->component;
@@ -94,7 +95,7 @@ private function executeHydrationTestCase(callable $testFactory, ?int $minPhpVer
9495

9596
$dehydratedProps = $this->hydrator()->dehydrate(
9697
$originalComponentWithData,
97-
$componentAttributesFactory->create([]), // not worried about testing these here
98+
new ComponentAttributes([], $twig->getRuntime(EscaperRuntime::class)), // not worried about testing these here
9899
$liveMetadata,
99100
);
100101

@@ -136,7 +137,7 @@ private function executeHydrationTestCase(callable $testFactory, ?int $minPhpVer
136137

137138
$dehydratedProps2 = $this->hydrator()->dehydrate(
138139
$componentAfterHydration,
139-
$componentAttributesFactory->create(),
140+
new ComponentAttributes([], $twig->getRuntime(EscaperRuntime::class)),
140141
$liveMetadata,
141142
);
142143
$this->hydrator()->hydrate(
@@ -1824,14 +1825,14 @@ public static function falseyValueProvider(): iterable
18241825
yield ['nullableBool', '', null];
18251826
yield 'fooey-o-booey-todo' => ['nullableBool', ' ', null];
18261827
}
1827-
1828+
18281829
private function createComponentAttributes(array $attributes = []): ComponentAttributes
18291830
{
1830-
$factory = self::getContainer()->get('ux.twig_component.component_attributes_factory');
1831-
\assert($factory instanceof ComponentAttributesFactory);
1832-
1833-
return $factory->create($attributes);
1834-
}
1831+
$twig = self::getContainer()->get('twig');
1832+
\assert($twig instanceof Environment);
1833+
1834+
return new ComponentAttributes($attributes, $twig->getRuntime(EscaperRuntime::class));
1835+
}
18351836

18361837
private function createLiveMetadata(object $component): LiveComponentMetadata
18371838
{

tests/Unit/LiveComponentHydratorTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Symfony\UX\LiveComponent\LiveComponentHydrator;
2121
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
2222
use Symfony\UX\LiveComponent\Metadata\LivePropMetadata;
23-
use Symfony\UX\TwigComponent\ComponentAttributesFactory;
2423
use Twig\Environment;
2524

2625
final class LiveComponentHydratorTest extends TestCase
@@ -36,7 +35,7 @@ public function testConstructWithEmptySecret(): void
3635
$this->createMock(LiveComponentMetadataFactory::class),
3736
$this->createMock(NormalizerInterface::class),
3837
'',
39-
new ComponentAttributesFactory($this->createMock(Environment::class)),
38+
$this->createMock(Environment::class),
4039
);
4140
}
4241

@@ -48,7 +47,7 @@ public function testItCanHydrateWithNullValues()
4847
$this->createMock(LiveComponentMetadataFactory::class),
4948
new Serializer(normalizers: [new ObjectNormalizer()]),
5049
'foo',
51-
new ComponentAttributesFactory($this->createMock(Environment::class)),
50+
$this->createMock(Environment::class),
5251
);
5352

5453
$hydratedValue = $hydrator->hydrateValue(

0 commit comments

Comments
 (0)