Skip to content

Commit 186b216

Browse files
smnandreweaverryan
authored andcommitted
[TwigComponent] Register safe classes in the configurator
1 parent 8e3cabe commit 186b216

File tree

11 files changed

+20
-27
lines changed

11 files changed

+20
-27
lines changed

src/LiveComponent/src/Attribute/LiveProp.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function __construct(
111111
*
112112
* @var string|null
113113
*/
114-
private string|null $modifier = null,
114+
private ?string $modifier = null,
115115
) {
116116
self::validateHydrationStrategy($this);
117117
}
@@ -290,7 +290,7 @@ public function withUrl(bool $url): self
290290
return $clone;
291291
}
292292

293-
public function modifier(): string|null
293+
public function modifier(): ?string
294294
{
295295
return $this->modifier;
296296
}

src/TwigComponent/src/BlockStack.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
/*
64
* This file is part of the Symfony package.
75
*

src/TwigComponent/src/Command/TwigComponentDebugCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ protected function configure(): void
5050
->setDefinition([
5151
new InputArgument('name', InputArgument::OPTIONAL, 'A component name or part of the component name'),
5252
])
53-
->setHelp(<<<'EOF'
53+
->setHelp(
54+
<<<'EOF'
5455
The <info>%command.name%</info> display all the Twig components in your application.
5556
5657
To list all components:

src/TwigComponent/src/ComponentAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public function getIterator(): \Traversable
195195

196196
public function has(string $attribute): bool
197197
{
198-
return array_key_exists($attribute, $this->attributes);
198+
return \array_key_exists($attribute, $this->attributes);
199199
}
200200

201201
public function count(): int

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;
1919
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
2020
use Twig\Environment;
21-
use Twig\Extension\EscaperExtension;
2221

2322
/**
2423
* @author Kevin Bond <kevinbond@gmail.com>
@@ -27,8 +26,6 @@
2726
*/
2827
final class ComponentRenderer implements ComponentRendererInterface
2928
{
30-
private bool $safeClassesRegistered = false;
31-
3229
public function __construct(
3330
private Environment $twig,
3431
private EventDispatcherInterface $dispatcher,
@@ -108,12 +105,6 @@ public function finishEmbeddedComponentRender(): void
108105

109106
private function preRender(MountedComponent $mounted, array $context = []): PreRenderEvent
110107
{
111-
if (!$this->safeClassesRegistered) {
112-
$this->twig->getExtension(EscaperExtension::class)->addSafeClass(ComponentAttributes::class, ['html']);
113-
114-
$this->safeClassesRegistered = true;
115-
}
116-
117108
$component = $mounted->getComponent();
118109
$metadata = $this->factory->metadataFor($mounted->getName());
119110
// expose public properties and properties marked with ExposeInTemplate attribute

src/TwigComponent/src/Test/InteractsWithTwigComponents.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ protected function renderTwigComponent(string $name, array $data = [], ?string $
3939
$blocks = array_filter(array_merge($blocks, ['content' => $content]));
4040

4141
if (!$blocks) {
42-
return new RenderedComponent(self::getContainer()->get('twig')
42+
return new RenderedComponent(
43+
self::getContainer()->get('twig')
4344
->createTemplate('{{ component(name, data) }}')
4445
->render([
4546
'name' => $name,
@@ -56,7 +57,8 @@ protected function renderTwigComponent(string $name, array $data = [], ?string $
5657

5758
$template .= '{% endcomponent %}';
5859

59-
return new RenderedComponent(self::getContainer()->get('twig')
60+
return new RenderedComponent(
61+
self::getContainer()->get('twig')
6062
->createTemplate($template)
6163
->render([
6264
'data' => $data,

src/TwigComponent/src/Twig/TemplateNameParser.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
/*
64
* This file is part of the Symfony package.
75
*

src/TwigComponent/src/Twig/TwigEnvironmentConfigurator.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,27 @@
1212
namespace Symfony\UX\TwigComponent\Twig;
1313

1414
use Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator;
15+
use Symfony\UX\TwigComponent\ComponentAttributes;
1516
use Twig\Environment;
17+
use Twig\Extension\EscaperExtension;
1618

19+
/**
20+
* @final
21+
*/
1722
class TwigEnvironmentConfigurator
1823
{
19-
private EnvironmentConfigurator $decorated;
20-
2124
public function __construct(
22-
EnvironmentConfigurator $decorated,
25+
private readonly EnvironmentConfigurator $decorated,
2326
) {
24-
$this->decorated = $decorated;
2527
}
2628

2729
public function configure(Environment $environment): void
2830
{
2931
$this->decorated->configure($environment);
30-
3132
$environment->setLexer(new ComponentLexer($environment));
33+
34+
if ($environment->hasExtension(EscaperExtension::class)) {
35+
$environment->getExtension(EscaperExtension::class)->addSafeClass(ComponentAttributes::class, ['html']);
36+
}
3237
}
3338
}

src/TwigComponent/tests/Unit/ComponentAttributesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function testCannotRenderNonStringAttribute(): void
224224

225225
$attributes->render('attr1');
226226
}
227-
227+
228228
public function testCanCheckIfAttributeExists(): void
229229
{
230230
$attributes = new ComponentAttributes(['foo' => 'bar']);

ux.symfony.com/src/LiveMemory/Component/ScoreRow.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace App\LiveMemory\Component;
1313

14-
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1514
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
1615
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
1716

ux.symfony.com/src/LiveMemory/Component/Timer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\UX\LiveComponent\Attribute\PostHydrate;
1818
use Symfony\UX\LiveComponent\ComponentToolsTrait;
1919
use Symfony\UX\LiveComponent\DefaultActionTrait;
20-
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
2120
use Symfony\UX\TwigComponent\Attribute\PostMount;
2221

2322
/**

0 commit comments

Comments
 (0)