Skip to content

[TwigComponent] Register safe classes in the configurator #1579

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 1 commit into from
Mar 6, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/LiveComponent/src/Attribute/LiveProp.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function __construct(
*
* @var string|null
*/
private string|null $modifier = null,
private ?string $modifier = null,
) {
self::validateHydrationStrategy($this);
}
Expand Down Expand Up @@ -290,7 +290,7 @@ public function withUrl(bool $url): self
return $clone;
}

public function modifier(): string|null
public function modifier(): ?string
{
return $this->modifier;
}
Expand Down
2 changes: 0 additions & 2 deletions src/TwigComponent/src/BlockStack.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand Down
3 changes: 2 additions & 1 deletion src/TwigComponent/src/Command/TwigComponentDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ protected function configure(): void
->setDefinition([
new InputArgument('name', InputArgument::OPTIONAL, 'A component name or part of the component name'),
])
->setHelp(<<<'EOF'
->setHelp(
<<<'EOF'
The <info>%command.name%</info> display all the Twig components in your application.

To list all components:
Expand Down
2 changes: 1 addition & 1 deletion src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function getIterator(): \Traversable

public function has(string $attribute): bool
{
return array_key_exists($attribute, $this->attributes);
return \array_key_exists($attribute, $this->attributes);
}

public function count(): int
Expand Down
9 changes: 0 additions & 9 deletions src/TwigComponent/src/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
use Twig\Environment;
use Twig\Extension\EscaperExtension;

/**
* @author Kevin Bond <kevinbond@gmail.com>
Expand All @@ -27,8 +26,6 @@
*/
final class ComponentRenderer implements ComponentRendererInterface
{
private bool $safeClassesRegistered = false;

public function __construct(
private Environment $twig,
private EventDispatcherInterface $dispatcher,
Expand Down Expand Up @@ -108,12 +105,6 @@ public function finishEmbeddedComponentRender(): void

private function preRender(MountedComponent $mounted, array $context = []): PreRenderEvent
{
if (!$this->safeClassesRegistered) {
$this->twig->getExtension(EscaperExtension::class)->addSafeClass(ComponentAttributes::class, ['html']);

$this->safeClassesRegistered = true;
}

$component = $mounted->getComponent();
$metadata = $this->factory->metadataFor($mounted->getName());
// expose public properties and properties marked with ExposeInTemplate attribute
Expand Down
6 changes: 4 additions & 2 deletions src/TwigComponent/src/Test/InteractsWithTwigComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ protected function renderTwigComponent(string $name, array $data = [], ?string $
$blocks = array_filter(array_merge($blocks, ['content' => $content]));

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

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

return new RenderedComponent(self::getContainer()->get('twig')
return new RenderedComponent(
self::getContainer()->get('twig')
->createTemplate($template)
->render([
'data' => $data,
Expand Down
2 changes: 0 additions & 2 deletions src/TwigComponent/src/Twig/TemplateNameParser.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand Down
15 changes: 10 additions & 5 deletions src/TwigComponent/src/Twig/TwigEnvironmentConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@
namespace Symfony\UX\TwigComponent\Twig;

use Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator;
use Symfony\UX\TwigComponent\ComponentAttributes;
use Twig\Environment;
use Twig\Extension\EscaperExtension;

/**
* @final
*/
class TwigEnvironmentConfigurator
{
private EnvironmentConfigurator $decorated;

public function __construct(
EnvironmentConfigurator $decorated,
private readonly EnvironmentConfigurator $decorated,
) {
$this->decorated = $decorated;
}

public function configure(Environment $environment): void
{
$this->decorated->configure($environment);

$environment->setLexer(new ComponentLexer($environment));

if ($environment->hasExtension(EscaperExtension::class)) {
$environment->getExtension(EscaperExtension::class)->addSafeClass(ComponentAttributes::class, ['html']);
}
}
}
2 changes: 1 addition & 1 deletion src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function testCannotRenderNonStringAttribute(): void

$attributes->render('attr1');
}

public function testCanCheckIfAttributeExists(): void
{
$attributes = new ComponentAttributes(['foo' => 'bar']);
Expand Down
1 change: 0 additions & 1 deletion ux.symfony.com/src/LiveMemory/Component/ScoreRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace App\LiveMemory\Component;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;

Expand Down
1 change: 0 additions & 1 deletion ux.symfony.com/src/LiveMemory/Component/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\UX\LiveComponent\Attribute\PostHydrate;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\PostMount;

/**
Expand Down