merged branch jfsimon/issue-6238 (PR #7373)

This PR was squashed before being merged into the master branch (closes #7373).

Commits
-------

e372183 [TwigBundle] Adds service check for extension loading

Discussion
----------

[TwigBundle] Adds service check for extension loading

This PR adds following checks:
*  `twig.extension.form` extension is loaded if `form.extension` service is defined
*  `twig.extension.trans` extension is loaded if `translator` service is defined

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #6238
This commit is contained in:
Fabien Potencier 2013-03-14 12:28:51 +01:00
commit b8b8cd247e
4 changed files with 37 additions and 5 deletions

View File

@ -0,0 +1,34 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
class ExtensionPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->has('form.extension')) {
$container->getDefinition('twig.extension.form')->addTag('twig.extension');
$reflClass = new \ReflectionClass('Symfony\Bridge\Twig\Extension\FormExtension');
$container->getDefinition('twig.loader')->addMethodCall('addPath', array(dirname(dirname($reflClass->getFileName())).'/Resources/views/Form'));
}
if (false === $container->has('translator')) {
$container->getDefinition('twig.extension.trans')->addTag('twig.extension');
}
}
}

View File

@ -57,9 +57,6 @@ class TwigExtension extends Extension
$container->setParameter('twig.form.resources', $config['form']['resources']);
$reflClass = new \ReflectionClass('Symfony\Bridge\Twig\Extension\FormExtension');
$container->getDefinition('twig.loader.filesystem')->addMethodCall('addPath', array(dirname(dirname($reflClass->getFileName())).'/Resources/views/Form'));
$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');
// register user-configured paths

View File

@ -58,7 +58,6 @@
</service>
<service id="twig.extension.trans" class="%twig.extension.trans.class%" public="false">
<tag name="twig.extension" />
<argument type="service" id="translator" />
</service>
@ -93,8 +92,8 @@
<argument type="service" id="fragment.handler" />
</service>
<service id="twig.extension.form" class="%twig.extension.form.class%" public="false">
<tag name="twig.extension" />
<argument type="service" id="twig.form.renderer" />
</service>

View File

@ -16,6 +16,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigEnvironmentPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigLoaderPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExceptionListenerPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExtensionPass;
/**
* Bundle.
@ -28,6 +29,7 @@ class TwigBundle extends Bundle
{
parent::build($container);
$container->addCompilerPass(new ExtensionPass());
$container->addCompilerPass(new TwigEnvironmentPass());
$container->addCompilerPass(new TwigLoaderPass());
$container->addCompilerPass(new ExceptionListenerPass());