Skip to content

Commit

Permalink
Make twig/extra-bundle optional
Browse files Browse the repository at this point in the history
  • Loading branch information
wbloszyk committed Jun 30, 2020
1 parent 335a175 commit e56741f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 6 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"symfony/twig-bridge": "^4.3",
"symfony/twig-bundle": "^4.3",
"symfony/validator": "^4.3",
"twig/extra-bundle": "^3.0",
"twig/string-extra": "^3.0",
"twig/twig": "^2.12.1"
},
Expand All @@ -81,7 +80,8 @@
},
"suggest": {
"jms/translation-bundle": "Extract message keys from Admins",
"kunicmarko/sonata-auto-configure-bundle": "Auto configures Admin classes"
"kunicmarko/sonata-auto-configure-bundle": "Auto configures Admin classes",
"twig/extra-bundle": "Auto configures the Twig Intl extension"
},
"config": {
"sort-packages": true
Expand Down
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 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\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);
}
}
2 changes: 2 additions & 0 deletions src/SonataAdminBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Sonata\AdminBundle\DependencyInjection\Compiler\GlobalVariablesCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\ModelManagerCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\ObjectAclManipulatorCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\TwigStringExtensionCompilerPass;
use Sonata\AdminBundle\Form\Type\AdminType;
use Sonata\AdminBundle\Form\Type\ChoiceFieldMaskType;
use Sonata\AdminBundle\Form\Type\CollectionType;
Expand Down Expand Up @@ -52,6 +53,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new GlobalVariablesCompilerPass());
$container->addCompilerPass(new ModelManagerCompilerPass());
$container->addCompilerPass(new ObjectAclManipulatorCompilerPass());
$container->addCompilerPass(new TwigStringExtensionCompilerPass());

$this->registerFormMapping();
}
Expand Down
8 changes: 6 additions & 2 deletions tests/App/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Twig\Extra\TwigExtraBundle\TwigExtraBundle;
use Twig\Extra\String\StringExtension;

final class AppKernel extends Kernel
{
Expand All @@ -44,7 +45,6 @@ public function registerBundles()
$bundles = [
new FrameworkBundle(),
new TwigBundle(),
new TwigExtraBundle(),
new SecurityBundle(),
new KnpMenuBundle(),
new SonataBlockBundle(),
Expand Down Expand Up @@ -103,6 +103,10 @@ protected function configureContainer(ContainerBuilder $containerBuilder, Loader
]);

$loader->load($this->getProjectDir().'/config/services.yml');

$definition = new Definition(StringExtension::class);
$definition->addTag('twig.extension');
$containerBuilder->setDefinition(StringExtension::class, $definition);
}

private function getBaseDir(): string
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 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Tests\DependencyInjection\Compiler;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Sonata\AdminBundle\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());
}
}
10 changes: 8 additions & 2 deletions tests/SonataAdminBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Sonata\AdminBundle\DependencyInjection\Compiler\GlobalVariablesCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\ModelManagerCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\ObjectAclManipulatorCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\TwigStringExtensionCompilerPass;
use Sonata\AdminBundle\SonataAdminBundle;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
Expand All @@ -36,7 +37,7 @@ public function testBuild(): void
->setMethods(['addCompilerPass'])
->getMock();

$containerBuilder->expects($this->exactly(6))
$containerBuilder->expects($this->exactly(7))
->method('addCompilerPass')
->willReturnCallback(function (CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION): void {
if ($pass instanceof AddDependencyCallsCompilerPass) {
Expand All @@ -63,14 +64,19 @@ public function testBuild(): void
return;
}

if ($pass instanceof TwigStringExtensionCompilerPass) {
return;
}

$this->fail(sprintf(
'CompilerPass is not one of the expected types. Expects "%s", "%s", "%s", "%s", "%s" or "%s", but got "%s".',
'CompilerPass is not one of the expected types. Expects "%s", "%s", "%s", "%s", "%s", "%s" or "%s", but got "%s".',
AddDependencyCallsCompilerPass::class,
AddFilterTypeCompilerPass::class,
ExtensionCompilerPass::class,
GlobalVariablesCompilerPass::class,
ModelManagerCompilerPass::class,
ObjectAclManipulatorCompilerPass::class,
TwigStringExtensionCompilerPass::class,
\get_class($pass)
));
});
Expand Down

0 comments on commit e56741f

Please sign in to comment.