浏览代码

[TwigBundle] reverted to lazy-loading of templating helpers (fixes #1066)

Kris Wallsmith 14 年之前
父节点
当前提交
4018040bbb

+ 7 - 10
src/Symfony/Bundle/TwigBundle/Extension/ActionsExtension.php

@@ -12,7 +12,7 @@
 namespace Symfony\Bundle\TwigBundle\Extension;
 
 use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
-use Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Twig extension for Symfony actions helper
@@ -21,19 +21,16 @@ use Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper;
  */
 class ActionsExtension extends \Twig_Extension
 {
-    /**
-     * @var Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper
-     */
-    private $helper;
+    private $container;
 
     /**
-     * Construct the extension with a HTTP Kernel
+     * Constructor.
      *
-     * @param HttpKernel $kernel The kernel to use for sub-requests
+     * @param ContainerInterface $container The service container
      */
-    public function __construct(ActionsHelper $helper)
+    public function __construct(ContainerInterface $container)
     {
-        $this->helper = $helper;
+        $this->container = $container;
     }
 
     /**
@@ -47,7 +44,7 @@ class ActionsExtension extends \Twig_Extension
      */
     public function renderAction($controller, array $attributes = array(), array $options = array())
     {
-        return $this->helper->render($controller, $attributes, $options);
+        return $this->container->get('templating.helper.actions')->render($controller, $attributes, $options);
     }
 
     /**

+ 6 - 9
src/Symfony/Bundle/TwigBundle/Extension/AssetsExtension.php

@@ -11,21 +11,18 @@
 
 namespace Symfony\Bundle\TwigBundle\Extension;
 
-use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * @author Fabien Potencier <fabien@symfony.com>
  */
 class AssetsExtension extends \Twig_Extension
 {
-    /**
-     * @var Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper
-     */
-    private $helper;
+    private $container;
 
-    public function __construct(AssetsHelper $helper)
+    public function __construct(ContainerInterface $container)
     {
-        $this->helper = $helper;
+        $this->container = $container;
     }
 
     /**
@@ -53,7 +50,7 @@ class AssetsExtension extends \Twig_Extension
      */
     public function getAssetUrl($path, $packageName = null)
     {
-        return $this->helper->getUrl($path, $packageName);
+        return $this->container->get('templating.helper.assets')->getUrl($path, $packageName);
     }
 
     /**
@@ -64,7 +61,7 @@ class AssetsExtension extends \Twig_Extension
      */
     public function getAssetsVersion($packageName = null)
     {
-        return $this->helper->getVersion($packageName);
+        return $this->container->get('templating.helper.assets')->getVersion($packageName);
     }
 
     /**

+ 12 - 12
src/Symfony/Bundle/TwigBundle/Extension/CodeExtension.php

@@ -11,7 +11,7 @@
 
 namespace Symfony\Bundle\TwigBundle\Extension;
 
-use Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  *
@@ -19,16 +19,16 @@ use Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper;
  */
 class CodeExtension extends \Twig_Extension
 {
-    private $helper;
+    private $container;
 
     /**
      * Constructor of Twig Extension to provide functions for code formatting
      *
      * @param Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper $helper Helper to use
      */
-    public function __construct(CodeHelper $helper)
+    public function __construct(ContainerInterface $container)
     {
-        $this->helper = $helper;
+        $this->container = $container;
     }
 
     /**
@@ -50,42 +50,42 @@ class CodeExtension extends \Twig_Extension
 
     public function abbrClass($class)
     {
-        return $this->helper->abbrClass($class);
+        return $this->container->get('templating.helper.code')->abbrClass($class);
     }
 
     public function abbrMethod($method)
     {
-        return $this->helper->abbrMethod($method);
+        return $this->container->get('templating.helper.code')->abbrMethod($method);
     }
 
     public function formatArgs($args)
     {
-        return $this->helper->formatArgs($args);
+        return $this->container->get('templating.helper.code')->formatArgs($args);
     }
 
     public function formatArgsAsText($args)
     {
-        return $this->helper->formatArgsAsText($args);
+        return $this->container->get('templating.helper.code')->formatArgsAsText($args);
     }
 
     public function fileExcerpt($file, $line)
     {
-        return $this->helper->fileExcerpt($file, $line);
+        return $this->container->get('templating.helper.code')->fileExcerpt($file, $line);
     }
 
     public function formatFile($file, $line, $text = null)
     {
-        return $this->helper->formatFile($file, $line, $text);
+        return $this->container->get('templating.helper.code')->formatFile($file, $line, $text);
     }
 
     public function getFileLink($file, $line)
     {
-        return $this->helper->getFileLink($file, $line);
+        return $this->container->get('templating.helper.code')->getFileLink($file, $line);
     }
 
     public function formatFileFromText($text)
     {
-        return $this->helper->formatFileFromText($text);
+        return $this->container->get('templating.helper.code')->formatFileFromText($text);
     }
 
     public function getName()

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

@@ -49,17 +49,17 @@
 
         <service id="twig.extension.assets" class="%twig.extension.assets.class%" public="false">
             <tag name="twig.extension" />
-            <argument type="service" id="templating.helper.assets" />
+            <argument type="service" id="service_container" />
         </service>
 
         <service id="twig.extension.actions" class="%twig.extension.actions.class%" public="false">
             <tag name="twig.extension" />
-            <argument type="service" id="templating.helper.actions" />
+            <argument type="service" id="service_container" />
         </service>
 
         <service id="twig.extension.code" class="%twig.extension.code.class%" public="false">
             <tag name="twig.extension" />
-            <argument type="service" id="templating.helper.code" />
+            <argument type="service" id="service_container" />
         </service>
 
         <service id="twig.extension.routing" class="%twig.extension.routing.class%" public="false">