change the stopwatch argument to be any valid expression

This commit is contained in:
Fabien Potencier 2013-08-12 08:31:36 +02:00
parent 459097413d
commit 29a58e7a03
3 changed files with 24 additions and 24 deletions

View File

@ -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")
;
}
}

View File

@ -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')),
);
}

View File

@ -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;