diff --git a/src/Symfony/Bridge/Twig/Node/StopwatchNode.php b/src/Symfony/Bridge/Twig/Node/StopwatchNode.php index eac3fce9a4..cc12abd05d 100644 --- a/src/Symfony/Bridge/Twig/Node/StopwatchNode.php +++ b/src/Symfony/Bridge/Twig/Node/StopwatchNode.php @@ -18,17 +18,27 @@ namespace Symfony\Bridge\Twig\Node; */ class StopwatchNode extends \Twig_Node { - public function __construct($name, $body, $lineno = 0, $tag = null) + public function __construct(\Twig_NodeInterface $name, $body, \Twig_Node_Expression_AssignName $var, $lineno = 0, $tag = null) { - parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag); + parent::__construct(array('body' => $body, 'name' => $name, 'var' => $var), array(), $lineno, $tag); } public function compile(\Twig_Compiler $compiler) { $compiler - ->write(sprintf("\$this->env->getExtension('stopwatch')->getStopwatch()->start('%s', 'template');\n", $this->getAttribute('name'))) + ->addDebugInfo($this) + ->write('') + ->subcompile($this->getNode('var')) + ->raw(' = ') + ->subcompile($this->getNode('name')) + ->write(";\n") + ->write("\$this->env->getExtension('stopwatch')->getStopwatch()->start(") + ->subcompile($this->getNode('var')) + ->raw(", 'template');\n") ->subcompile($this->getNode('body')) - ->write(sprintf("\$this->env->getExtension('stopwatch')->getStopwatch()->stop('%s');\n", $this->getAttribute('name'))) + ->write("\$this->env->getExtension('stopwatch')->getStopwatch()->stop(") + ->subcompile($this->getNode('var')) + ->raw(");\n") ; } } diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php index 9ffb6d9329..626131de25 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php @@ -31,7 +31,7 @@ class StopwatchExtensionTest extends TestCase */ public function testFailIfStoppingWrongEvent() { - $this->testTiming('{% stopwatch foo %}{% endstopwatch bar %}', array()); + $this->testTiming('{% stopwatch "foo" %}{% endstopwatch "bar" %}', array()); } /** @@ -52,11 +52,12 @@ class StopwatchExtensionTest extends TestCase public function getTimingTemplates() { return array( - array('{% stopwatch foo %}something{% endstopwatch %}', 'foo'), - array('{% stopwatch foo %}symfony2 is fun{% endstopwatch %}{% stopwatch bar %}something{% endstopwatch %}', array('foo', 'bar')), - array('{% stopwatch foo %}something{% endstopwatch foo %}', 'foo'), + array('{% stopwatch "foo" %}something{% endstopwatch %}', 'foo'), + array('{% stopwatch "foo" %}symfony2 is fun{% endstopwatch %}{% stopwatch "bar" %}something{% endstopwatch %}', array('foo', 'bar')), + array('{% set foo = "foo" %}{% stopwatch foo %}something{% endstopwatch %}', 'foo'), + array('{% set foo = "foo" %}{% stopwatch foo %}something {% set foo = "bar" %}{% endstopwatch %}', 'foo'), array('{% stopwatch "foo.bar" %}something{% endstopwatch %}', 'foo.bar'), - array('{% stopwatch foo %}something{% endstopwatch foo %}{% stopwatch foo %}something else{% endstopwatch foo %}', array('foo', 'foo')), + array('{% stopwatch "foo" %}something{% endstopwatch %}{% stopwatch "foo" %}something else{% endstopwatch %}', array('foo', 'foo')), ); } diff --git a/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php b/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php index ddbaeb4958..2983e4cb6b 100644 --- a/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php +++ b/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php @@ -32,28 +32,17 @@ class StopwatchTokenParser extends \Twig_TokenParser $lineno = $token->getLine(); $stream = $this->parser->getStream(); - // {% stopwatch bar %} - if ($stream->test(\Twig_Token::NAME_TYPE)) { - $name = $stream->expect(\Twig_Token::NAME_TYPE)->getValue(); - } else { - $name = $stream->expect(\Twig_Token::STRING_TYPE)->getValue(); - } + // {% stopwatch 'bar' %} + $name = $this->parser->getExpressionParser()->parseExpression(); $stream->expect(\Twig_Token::BLOCK_END_TYPE); - // {% endstopwatch %} or {% endstopwatch bar %} + // {% endstopwatch %} $body = $this->parser->subparse(array($this, 'decideStopwatchEnd'), true); - if ($stream->test(\Twig_Token::NAME_TYPE) || $stream->test(\Twig_Token::STRING_TYPE)) { - $value = $stream->next()->getValue(); - - if ($name != $value) { - throw new \Twig_Error_Syntax(sprintf('Expected endstopwatch for event "%s" (but "%s" given).', $name, $value)); - } - } $stream->expect(\Twig_Token::BLOCK_END_TYPE); if ($this->stopwatchIsAvailable) { - return new StopwatchNode($name, $body, $lineno, $this->getTag()); + return new StopwatchNode($name, $body, new \Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $lineno, $this->getTag()); } return $body;