Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ final class TwigComponentPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
$componentConfig = [];
if ($container->hasDefinition('twig') && $container->hasDefinition('ux.twig_component.twig.lexer')) {
$container->getDefinition('twig')
->addMethodCall('setLexer', [new Reference('ux.twig_component.twig.lexer')]);
}

$componentConfig = [];
$componentReferences = [];
$componentClassMap = [];
$componentNames = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\UX\TwigComponent\DependencyInjection;

use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\SafeClassPass;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand All @@ -30,6 +31,7 @@
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\CacheWarmer\TwigComponentCacheWarmer;
use Symfony\UX\TwigComponent\Command\TwigComponentDebugCommand;
use Symfony\UX\TwigComponent\ComponentAttributes;
use Symfony\UX\TwigComponent\ComponentFactory;
use Symfony\UX\TwigComponent\ComponentProperties;
use Symfony\UX\TwigComponent\ComponentRenderer;
Expand Down Expand Up @@ -122,11 +124,17 @@ static function (ChildDefinition $definition, AsTwigComponent $attribute) {
->addTag('twig.runtime')
;

$container->register('ux.twig_component.twig.lexer', ComponentLexer::class);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This declaration was not used. A new ComponentLexer instance was created in the configurator.

$container->register('ux.twig_component.twig.lexer', ComponentLexer::class)
->setArguments([new Reference('twig')]);

$container->register('ux.twig_component.twig.environment_configurator', TwigEnvironmentConfigurator::class)
->setDecoratedService(new Reference('twig.configurator.environment'))
->setArguments([new Reference('ux.twig_component.twig.environment_configurator.inner')]);
if (class_exists(SafeClassPass::class)) {
$container->register(ComponentAttributes::class)
->addResourceTag('twig.safe_class', ['strategy' => 'html']);
} else {
$container->register('ux.twig_component.twig.environment_configurator', TwigEnvironmentConfigurator::class)
->setDecoratedService(new Reference('twig.configurator.environment'))
->setArguments([new Reference('ux.twig_component.twig.environment_configurator.inner')]);
}

$container->register('ux.twig_component.command.debug', TwigComponentDebugCommand::class)
->setArguments([
Expand Down
4 changes: 2 additions & 2 deletions src/TwigComponent/src/Twig/TwigEnvironmentConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

/**
* @final
*
* @internal since symfony/ux-twig-component 2.36, this class will be removed once symfony/twig-bundle >= 8.1 is required
*/
class TwigEnvironmentConfigurator
{
Expand All @@ -31,8 +33,6 @@ public function configure(Environment $environment): void
{
$this->decorated->configure($environment);

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

if (class_exists(EscaperRuntime::class)) {
$environment->getRuntime(EscaperRuntime::class)->addSafeClass(ComponentAttributes::class, ['html']);
} elseif ($environment->hasExtension(EscaperExtension::class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
namespace Symfony\UX\TwigComponent\Tests\Unit\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\SafeClassPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\UX\TwigComponent\ComponentAttributes;
use Symfony\UX\TwigComponent\DependencyInjection\TwigComponentExtension;
use Symfony\UX\TwigComponent\Twig\TwigEnvironmentConfigurator;
use Symfony\UX\TwigComponent\TwigComponentBundle;

/**
Expand Down Expand Up @@ -84,6 +87,45 @@ public function testDataCollectorWithDebugModeCanBeDisabled()
$this->assertFalse($container->hasDefinition('ux.twig_component.data_collector'));
}

public function testSafeClassPassIntegration()
{
if (!class_exists(SafeClassPass::class)) {
$this->markTestSkipped('Requires symfony/twig-bundle >= 8.1 with SafeClassPass support');
}

$container = $this->createContainer();
$container->registerExtension(new TwigComponentExtension());
$container->loadFromExtension('twig_component', [
'defaults' => [],
'anonymous_template_directory' => 'components/',
]);
$this->compileContainer($container);

$this->assertFalse($container->hasDefinition('ux.twig_component.twig.environment_configurator'));
$this->assertTrue($container->hasDefinition(ComponentAttributes::class));
$def = $container->getDefinition(ComponentAttributes::class);
$this->assertTrue($def->hasTag('twig.safe_class'));
$this->assertSame([['strategy' => 'html']], $def->getTag('twig.safe_class'));
}

public function testFallbackToEnvironmentConfiguratorWithoutSafeClassPass()
{
if (class_exists(SafeClassPass::class)) {
$this->markTestSkipped('Only relevant with symfony/twig-bundle < 8.1 without SafeClassPass support');
}

$container = $this->createContainer();
$container->registerExtension(new TwigComponentExtension());
$container->loadFromExtension('twig_component', [
'defaults' => [],
'anonymous_template_directory' => 'components/',
]);
$this->compileContainer($container);

$this->assertTrue($container->hasDefinition('ux.twig_component.twig.environment_configurator'));
$this->assertSame(TwigEnvironmentConfigurator::class, $container->getDefinition('ux.twig_component.twig.environment_configurator')->getClass());
}

private function createContainer()
{
$container = new ContainerBuilder(new ParameterBag([
Expand Down
Loading