Browse Source

[TwigBundle] converted the special Twig Environment class to a DIC compiler class

Fabien Potencier 14 years ago
parent
commit
8e6a3849ee

+ 37 - 0
src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Adds tagged twig.extension services to twig service
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class TwigEnvironmentPass implements CompilerPassInterface
+{
+    public function process(ContainerBuilder $container)
+    {
+        if (false === $container->hasDefinition('twig')) {
+            return;
+        }
+
+        $definition = $container->getDefinition('twig');
+
+        foreach ($container->findTaggedServiceIds('twig.extension') as $id => $attributes) {
+            $definition->addMethodCall('addExtension', array(new Reference($id)));
+        }
+    }
+}

+ 0 - 31
src/Symfony/Bundle/TwigBundle/Environment.php

@@ -1,31 +0,0 @@
-<?php
-
-namespace Symfony\Bundle\TwigBundle;
-
-use Symfony\Component\DependencyInjection\ContainerInterface;
-
-/*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
-
-/**
- * TwigExtension.
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
-class Environment extends \Twig_Environment
-{
-    public function __construct(ContainerInterface $container, \Twig_LoaderInterface $loader = null, $options = array())
-    {
-        parent::__construct($loader, $options);
-
-        foreach ($container->findTaggedServiceIds('twig.extension') as $id => $attributes) {
-            $this->addExtension($container->get($id));
-        }
-    }
-}

+ 1 - 2
src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

@@ -5,7 +5,7 @@
     xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
 
     <parameters>
-        <parameter key="twig.class">Symfony\Bundle\TwigBundle\Environment</parameter>
+        <parameter key="twig.class">Twig_Environment</parameter>
         <parameter key="twig.options" type="collection">
             <parameter key="charset">%kernel.charset%</parameter>
             <parameter key="debug">%kernel.debug%</parameter>
@@ -20,7 +20,6 @@
 
     <services>
         <service id="twig" class="%twig.class%">
-            <argument type="service" id="service_container" />
             <argument type="service" id="twig.loader" />
             <argument>%twig.options%</argument>
         </service>

+ 1 - 1
src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

@@ -23,7 +23,7 @@ class TwigExtensionTest extends TestCase
         $loader = new TwigExtension();
 
         $loader->configLoad(array(), $container);
-        $this->assertEquals('Symfony\\Bundle\\TwigBundle\\Environment', $container->getParameter('twig.class'), '->configLoad() loads the twig.xml file if not already loaded');
+        $this->assertEquals('Twig_Environment', $container->getParameter('twig.class'), '->configLoad() loads the twig.xml file if not already loaded');
 
         $loader->configLoad(array('charset' => 'ISO-8859-1'), $container);
         $options = $container->getParameter('twig.options');

+ 8 - 0
src/Symfony/Bundle/TwigBundle/TwigBundle.php

@@ -3,6 +3,8 @@
 namespace Symfony\Bundle\TwigBundle;
 
 use Symfony\Component\HttpKernel\Bundle\Bundle;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigEnvironmentPass;
 
 /*
  * This file is part of the Symfony package.
@@ -20,4 +22,10 @@ use Symfony\Component\HttpKernel\Bundle\Bundle;
  */
 class TwigBundle extends Bundle
 {
+    public function registerExtensions(ContainerBuilder $container)
+    {
+        parent::registerExtensions($container);
+
+        $container->addCompilerPass(new TwigEnvironmentPass());
+    }
 }