소스 검색

[TwigBundle] optimized calls to helpers

Fabien Potencier 14 년 전
부모
커밋
416bd7872e
2개의 변경된 파일57개의 추가작업 그리고 11개의 파일을 삭제
  1. 53 0
      src/Symfony/Bundle/TwigBundle/Node/HelperNode.php
  2. 4 11
      src/Symfony/Bundle/TwigBundle/TokenParser/HelperTokenParser.php

+ 53 - 0
src/Symfony/Bundle/TwigBundle/Node/HelperNode.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace Symfony\Bundle\TwigBundle\Node;
+
+/*
+ * 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.
+ */
+
+/**
+ * 
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class HelperNode extends \Twig_Node
+{
+    public function __construct($helper, $method, \Twig_Node_Expression_Array $values, $lineno, $tag = null)
+    {
+        parent::__construct(array('values' => $values), array('helper' => $helper, 'method' => $method), $lineno, $tag);
+    }
+
+    /**
+     * Compiles the node to PHP.
+     *
+     * @param \Twig_Compiler A Twig_Compiler instance
+     */
+    public function compile($compiler)
+    {
+        $compiler
+            ->addDebugInfo($this)
+            ->raw("\$this->env->getExtension(")
+            ->string('symfony.helpers')
+            ->raw(")->getContainer()->get(")
+            ->string($this['helper'])
+            ->raw(")->")
+            ->raw($this['method'])
+            ->raw("(")
+        ;
+
+        foreach ($this->values as $i => $value) {
+            $compiler->subcompile($value);
+            if ($i !== count($this->values) - 1) {
+                $compiler->raw(', ');
+            }
+        }
+
+        $compiler->raw(")");
+    }
+}

+ 4 - 11
src/Symfony/Bundle/TwigBundle/TokenParser/HelperTokenParser.php

@@ -2,6 +2,8 @@
 
 namespace Symfony\Bundle\TwigBundle\TokenParser;
 
+use Symfony\Bundle\TwigBundle\Node\HelperNode;
+
 /*
  * This file is part of the Symfony package.
  *
@@ -55,16 +57,7 @@ class HelperTokenParser extends \Twig_SimpleTokenParser
     {
         return $this->output(
             $this->markAsSafe(
-                $this->call(
-                    $this->call(
-                        $this->call(new \Twig_Node_Expression_ExtensionReference('symfony.helpers', 0), 'getContainer'),
-                        'get',
-                        array(new \Twig_Node_Expression_Constant($this->helper, 0))
-                    ),
-                    $this->method,
-                    $this->getNodeValues($values)
-                )
-            )
-        );
+                new HelperNode($this->helper, $this->method, new \Twig_Node_Expression_Array($this->getNodeValues($values), $line), $line)
+        ));
     }
 }