Quellcode durchsuchen

[TwigBundle] Move the {% render ... %} node to a dedicated extension + Remove service container

The purpose of the TemplatingExtension is ambigous. This first step move the actions logical
to a dedicated extension and reduce the dependency to the HTTP Kernel.
alexandresalome vor 14 Jahren
Ursprung
Commit
3cdf371c2b

+ 68 - 0
src/Symfony/Bundle/TwigBundle/Extension/ActionsExtension.php

@@ -0,0 +1,68 @@
+<?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\Extension;
+
+use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
+use Symfony\Component\HttpKernel\HttpKernel;
+
+/**
+ * Twig extension for Symfony actions helper
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Alexandre Salomé <alexandre.salome@gmail.com>
+ */
+class ActionsExtension extends \Twig_Extension
+{
+    /**
+     * @var Symfony\Component\HttpKernel\HttpKernel
+     */
+    private $kernel;
+
+    public function __construct(HttpKernel $kernel)
+    {
+        $this->kernel = $kernel;
+    }
+
+    /**
+     * Returns the Response content for a given controller or URI.
+     *
+     * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
+     * @param array  $attributes An array of request attributes
+     * @param array  $options    An array of options
+     *
+     * @see Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver::render()
+     */
+    public function renderAction($controller, array $attributes = array(), array $options = array())
+    {
+        $options['attributes'] = $attributes;
+
+        return $this->kernel->render($controller, $options);
+    }
+
+    /**
+     * Returns the token parser instance to add to the existing list.
+     *
+     * @return array An array of Twig_TokenParser instances
+     */
+    public function getTokenParsers()
+    {
+        return array(
+            // {% render 'BlogBundle:Post:list' with { 'limit': 2 }, { 'alt': 'BlogBundle:Post:error' } %}
+            new RenderTokenParser(),
+        );
+    }
+
+    public function getName()
+    {
+        return 'actions';
+    }
+}

+ 0 - 30
src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php

@@ -12,7 +12,6 @@
 namespace Symfony\Bundle\TwigBundle\Extension;
 
 use Symfony\Component\DependencyInjection\ContainerInterface;
-use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
 
 /**
  *
@@ -83,35 +82,6 @@ class TemplatingExtension extends \Twig_Extension
         return $this->container->get('templating.helper.assets')->getVersion($packageName);
     }
 
-    /**
-     * Returns the Response content for a given controller or URI.
-     *
-     * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
-     * @param array  $attributes An array of request attributes
-     * @param array  $options    An array of options
-     *
-     * @see Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver::render()
-     */
-    public function renderAction($controller, array $attributes = array(), array $options = array())
-    {
-        $options['attributes'] = $attributes;
-
-        return $this->container->get('http_kernel')->render($controller, $options);
-    }
-
-    /**
-     * Returns the token parser instance to add to the existing list.
-     *
-     * @return array An array of Twig_TokenParser instances
-     */
-    public function getTokenParsers()
-    {
-        return array(
-            // {% render 'BlogBundle:Post:list' with { 'limit': 2 }, { 'alt': 'BlogBundle:Post:error' } %}
-            new RenderTokenParser(),
-        );
-    }
-
     public function abbrClass($class)
     {
         return $this->container->get('templating.helper.code')->abbrClass($class);

+ 1 - 1
src/Symfony/Bundle/TwigBundle/Node/RenderNode.php

@@ -32,7 +32,7 @@ class RenderNode extends \Twig_Node
     {
         $compiler
             ->addDebugInfo($this)
-            ->write("echo \$this->env->getExtension('templating')->renderAction(")
+            ->write("echo \$this->env->getExtension('actions')->renderAction(")
             ->subcompile($this->getNode('expr'))
             ->raw(', ')
             ->subcompile($this->getNode('attributes'))

+ 5 - 0
src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

@@ -43,6 +43,11 @@
             <argument type="service" id="service_container" />
         </service>
 
+        <service id="twig.extension.actions" class="Symfony\Bundle\TwigBundle\Extension\ActionsExtension" public="false">
+            <tag name="twig.extension" />
+            <argument type="service" id="http_kernel" />
+        </service>
+
         <service id="twig.extension.routing" class="Symfony\Bridge\Twig\Extension\RoutingExtension" public="false">
             <tag name="twig.extension" />
             <argument type="service" id="router" />