Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Fix StringExtension registration
Browse files Browse the repository at this point in the history
  • Loading branch information
wbloszyk authored and jordisala1991 committed Jun 29, 2020
1 parent 0ec504f commit c4b91e3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\NewsBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Twig\Extra\String\StringExtension;

final class TwigStringExtensionCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
foreach ($container->findTaggedServiceIds('twig.extension') as $id => $attributes) {
if (StringExtension::class === $container->getDefinition($id)->getClass()) {
return;
}
}

$definition = new Definition(StringExtension::class);
$definition->addTag('twig.extension');
$container->setDefinition(StringExtension::class, $definition);
}
}
12 changes: 0 additions & 12 deletions src/DependencyInjection/SonataNewsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Twig\Extra\String\StringExtension;

/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
Expand Down Expand Up @@ -104,7 +103,6 @@ public function load(array $configs, ContainerBuilder $container)

$this->configureClass($config, $container);
$this->configureAdmin($config, $container);
$this->configureStringExtension($container);
}

/**
Expand Down Expand Up @@ -263,14 +261,4 @@ public function registerDoctrineMapping(array $config)
'orphanRemoval' => false,
]);
}

private function configureStringExtension(ContainerBuilder $container): void
{
if (!$container->hasDefinition('twig.extension.string') || !is_a($container->getDefinition('twig.extension.string')->getClass(), StringExtension::class)) {
$definition = new Definition(StringExtension::class);
$definition->addTag('twig.extension');

$container->setDefinition(StringExtension::class, $definition);
}
}
}
3 changes: 3 additions & 0 deletions src/SonataNewsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
namespace Sonata\NewsBundle;

use Sonata\CoreBundle\Form\FormHelper;
use Sonata\NewsBundle\DependencyInjection\Compiler\TwigStringExtensionCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class SonataNewsBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new TwigStringExtensionCompilerPass());

$this->registerFormMapping();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\NewsBundle\Tests\DependencyInjection\Compiler;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Sonata\NewsBundle\DependencyInjection\Compiler\TwigStringExtensionCompilerPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigEnvironmentPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Twig\Extra\String\StringExtension;

class TwigStringExtensionCompilerPassTest extends AbstractCompilerPassTestCase
{
public function testLoadTwigStringExtension(): void
{
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag(StringExtension::class, 'twig.extension');
}

public function testLoadTwigStringExtensionWithExtraBundle(): void
{
$definition = new Definition(StringExtension::class);
$definition->addTag('twig.extension');
$this->container->setDefinition('twig.extension.string', $definition);
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag('twig.extension.string', 'twig.extension');
$this->assertContainerBuilderNotHasService(StringExtension::class);
}

protected function registerCompilerPass(ContainerBuilder $container): void
{
$container->addCompilerPass(new TwigEnvironmentPass());
$container->addCompilerPass(new TwigStringExtensionCompilerPass());
}
}
8 changes: 0 additions & 8 deletions tests/DependencyInjection/SonataNewsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Sonata\NewsBundle\Model\Comment;
use Sonata\NewsBundle\Model\Post;
use Sonata\NewsBundle\Tests\Fixtures\UserMock;
use Twig\Extra\String\StringExtension;

class SonataNewsExtensionTest extends AbstractExtensionTestCase
{
Expand Down Expand Up @@ -67,13 +66,6 @@ public function testLoadWithTagWithCollection(): void
$this->assertCount(1, $postOneToManyAssociation, 'The post model should have 1 one to many association (comment)');
}

public function testLoadTwigStringExtension(): void
{
$this->load($this->getConfigurationWithTagWithCollection());

$this->assertContainerBuilderHasServiceDefinitionWithTag(StringExtension::class, 'twig.extension');
}

protected function getMinimalConfiguration(): array
{
return [
Expand Down

0 comments on commit c4b91e3

Please sign in to comment.