Skip to content

Commit

Permalink
bug #50 Disable Twig services if templating is disabled (pamil)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.5 branch.

Discussion
----------



Commits
-------

8fd7c71 Disable Twig services if templating is disabled
  • Loading branch information
pamil authored May 5, 2020
2 parents 9a8de49 + 8fd7c71 commit 9db9d2b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/DependencyInjection/SyliusThemeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public function load(array $config, ContainerBuilder $container): void
*/
public function prepend(ContainerBuilder $container): void
{
$config = $container->getExtensionConfig($this->getAlias());
$config = $this->processConfiguration($this->getConfiguration([], $container), $config);

if ($config['templating']['enabled'] === false) {
return;
}

$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

$this->prependTwig($container, $loader);
Expand Down
51 changes: 51 additions & 0 deletions tests/DependencyInjection/SyliusThemeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Sylius\Bundle\ThemeBundle\DependencyInjection\SyliusThemeExtension;
use Symfony\Bundle\TwigBundle\DependencyInjection\TwigExtension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;

final class SyliusThemeExtensionTest extends AbstractExtensionTestCase
{
Expand Down Expand Up @@ -70,6 +72,30 @@ public function it_does_not_load_translations_support_if_its_disabled(): void
$this->assertContainerBuilderNotHasService('sylius.theme.translation.translator');
}

/**
* @test
*/
public function it_loads_twig_if_templating_is_enabled_and_twig_bundle_is_registered(): void
{
$this->container->registerExtension(new TwigExtension());

$this->load(['templating' => ['enabled' => true]]);

$this->assertContainerBuilderHasService('sylius.theme.twig.loader');
}

/**
* @test
*/
public function it_does_not_load_twig_if_templating_support_is_disabled_even_if_twig_bundle_is_registered(): void
{
$this->container->registerExtension(new TwigExtension());

$this->load(['templating' => ['enabled' => false]]);

$this->assertContainerBuilderNotHasService('sylius.theme.twig.loader');
}

/**
* {@inheritdoc}
*/
Expand All @@ -79,4 +105,29 @@ protected function getContainerExtensions(): array
new SyliusThemeExtension(),
];
}

/**
* More realistic loading of extensions.
* Load only SyliusThemeBundleExtension.
*
* {@inheritdoc}
*/
protected function load(array $configurationValues = []): void
{
$configs = [$this->getMinimalConfiguration(), $configurationValues];

foreach ($configs as $config) {
$this->container->prependExtensionConfig('sylius_theme', $config);
}

foreach ($this->container->getExtensions() as $extension) {
if ($extension instanceof PrependExtensionInterface) {
$extension->prepend($this->container);
}

if ($extension instanceof SyliusThemeExtension) {
$extension->load($configs, $this->container);
}
}
}
}

0 comments on commit 9db9d2b

Please sign in to comment.