From f39ed5706d49de3be1cccadb2200a39828be8224 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Mon, 6 May 2013 21:11:53 +0200 Subject: [PATCH 1/5] Created stopwatch tag --- src/Symfony/Bridge/Twig/CHANGELOG.md | 5 + .../Twig/Extension/StopwatchExtension.php | 61 +++++++++++++ .../Bridge/Twig/Node/StopwatchNode.php | 36 ++++++++ .../Extension/StopwatchExtensionTest.php | 91 +++++++++++++++++++ .../Twig/TokenParser/StopwatchTokenParser.php | 77 ++++++++++++++++ .../TwigBundle/Resources/config/twig.xml | 6 ++ 6 files changed, 276 insertions(+) create mode 100644 src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php create mode 100644 src/Symfony/Bridge/Twig/Node/StopwatchNode.php create mode 100644 src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php create mode 100644 src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php diff --git a/src/Symfony/Bridge/Twig/CHANGELOG.md b/src/Symfony/Bridge/Twig/CHANGELOG.md index ad22216e40b87..346d52a5ad41e 100644 --- a/src/Symfony/Bridge/Twig/CHANGELOG.md +++ b/src/Symfony/Bridge/Twig/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +2.4.0 +----- + + * added stopwatch tag to time templates with the WebProfilerBundle + 2.3.0 ----- diff --git a/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php b/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php new file mode 100644 index 0000000000000..66f9b3a0e2572 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Extension; + +use Symfony\Component\Stopwatch\Stopwatch; +use Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser; + +/** + * Twig extension for the stopwatch helper. + * + * @author Wouter J + */ +class StopwatchExtension extends \Twig_Extension +{ + private $stopwatch; + + public function __construct(Stopwatch $stopwatch = null) + { + $this->stopwatch = $stopwatch; + } + + public function getTokenParsers() + { + return array( + /* + * {% stopwatch foo %} + * Some stuff which will be recorded on the timeline + * {% endstopwatch %} + */ + new StopwatchTokenParser($this->stopwatch !== null), + ); + } + + public function getName() + { + return 'stopwatch'; + } + + public function startEvent($name) + { + if ($this->stopwatch instanceof Stopwatch) { + $this->stopwatch->start($name, 'template'); + } + } + + public function stopEvent($name) + { + if ($this->stopwatch instanceof Stopwatch) { + $this->stopwatch->stop($name); + } + } +} diff --git a/src/Symfony/Bridge/Twig/Node/StopwatchNode.php b/src/Symfony/Bridge/Twig/Node/StopwatchNode.php new file mode 100644 index 0000000000000..f217bc0bc09ae --- /dev/null +++ b/src/Symfony/Bridge/Twig/Node/StopwatchNode.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Node; + +/** + * Represents a stopwatch node. + * + * @author Wouter J + */ +class StopwatchNode extends \Twig_Node +{ + public function __construct($name, $body, $lineno = 0, $tag = null) + { + parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag); + } + + public function compile(\Twig_Compiler $compiler) + { + $name = $this->getAttribute('name'); + + $compiler + ->write('$this->env->getExtension(\'stopwatch\')->startEvent(\''.$name.'\');') + ->subcompile($this->getNode('body')) + ->write('$this->env->getExtension(\'stopwatch\')->stopEvent(\''.$name.'\');') + ->raw("\n"); + } +} diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php new file mode 100644 index 0000000000000..b8cadf2af30b0 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php @@ -0,0 +1,91 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Tests\Extension; + +use Symfony\Bridge\Twig\Extension\StopwatchExtension; +use Symfony\Component\Stopwatch\Stopwatch; +use Symfony\Bridge\Twig\Tests\TestCase; + +class StopwatchExtensionTest extends TestCase +{ + protected function setUp() + { + parent::setUp(); + + if (!class_exists('Symfony\Component\Stopwatch\Stopwatch')) { + $this->markTestSkipped('The "Stopwatch" component is not available'); + } + } + + /** + * @expectedException \Twig_Error_Syntax + */ + public function testFailIfNameAlreadyExists() + { + $this->testTiming('{% stopwatch foo %}{% endstopwatch %}{% stopwatch foo %}{% endstopwatch %}', array()); + } + + /** + * @expectedException \Twig_Error_Syntax + */ + public function testFailIfStoppingWrongEvent() + { + $this->testTiming('{% stopwatch foo %}{% endstopwatch bar %}', array()); + } + + /** + * @dataProvider getTimingTemplates + */ + public function testTiming($template, $events) + { + $twig = new \Twig_Environment(new \Twig_Loader_String(), array('debug' => true, 'cache' => false, 'autoescape' => true, 'optimizations' => 0)); + $twig->addExtension(new StopwatchExtension($this->getStopwatch($events))); + + try { + $nodes = $twig->render($template); + } catch (\Twig_Error_Runtime $e) { + throw $e->getPrevious(); + } + } + + 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.bar' %}something{% endstopwatch %}", 'foo.bar'), + ); + } + + protected function getStopwatch($events = array()) + { + $events = is_array($events) ? $events : array($events); + $stopwatch = $this->getMock('Symfony\Component\Stopwatch\Stopwatch'); + + $i = -1; + foreach ($events as $eventName) { + $stopwatch->expects($this->at(++$i)) + ->method('start') + ->with($this->equalTo($eventName), 'template') + ; + $stopwatch->expects($this->at(++$i)) + ->method('stop') + ->with($this->equalTo($eventName)) + ; + } + + return $stopwatch; + } +} diff --git a/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php b/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php new file mode 100644 index 0000000000000..bdba8f50cb7d3 --- /dev/null +++ b/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\TokenParser; + +use Symfony\Bridge\Twig\Node\StopwatchNode; + +/** + * Token Parser for the stopwatch tag. + * + * @author Wouter J + */ +class StopwatchTokenParser extends \Twig_TokenParser +{ + protected $stopwatchIsAvailable; + protected $_events = array(); + + public function __construct($stopwatchIsAvailable) + { + $this->stopwatchIsAvailable = $stopwatchIsAvailable; + } + + public function parse(\Twig_Token $token) + { + $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(); + } + + if (in_array($name, $this->_events)) { + throw new \Twig_Error_Syntax(sprintf("The stopwatch event '%s' has already been defined", $name)); + } + $this->_events[] = $name; + + $stream->expect(\Twig_Token::BLOCK_END_TYPE); + + // {% endstopwatch %} or {% endstopwatch bar %} + $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 $body; + } + + public function decideStopwatchEnd(\Twig_Token $token) + { + return $token->test('endstopwatch'); + } + + public function getTag() + { + return 'stopwatch'; + } +} diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 971f4f17ebefd..48c0055d9cfea 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -18,6 +18,7 @@ Symfony\Bridge\Twig\Extension\YamlExtension Symfony\Bridge\Twig\Extension\FormExtension Symfony\Bridge\Twig\Extension\HttpKernelExtension + Symfony\Bridge\Twig\Extension\StopwatchExtension Symfony\Bridge\Twig\Form\TwigRendererEngine Symfony\Bridge\Twig\Form\TwigRenderer Symfony\Bridge\Twig\Translation\TwigExtractor @@ -86,6 +87,11 @@ + + + + + From bbad387e4b5d6ef83514bde99bd8157586d89592 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 11 Aug 2013 20:06:26 +0200 Subject: [PATCH 2/5] fixed CS --- .../Bridge/Twig/Extension/StopwatchExtension.php | 10 +++++----- src/Symfony/Bridge/Twig/Node/StopwatchNode.php | 4 ++-- .../Tests/Extension/StopwatchExtensionTest.php | 4 +--- .../Twig/TokenParser/StopwatchTokenParser.php | 14 +++++++------- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php b/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php index 66f9b3a0e2572..6379f1a73d044 100644 --- a/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php @@ -22,12 +22,12 @@ class StopwatchExtension extends \Twig_Extension { private $stopwatch; - + public function __construct(Stopwatch $stopwatch = null) { $this->stopwatch = $stopwatch; } - + public function getTokenParsers() { return array( @@ -39,19 +39,19 @@ public function getTokenParsers() new StopwatchTokenParser($this->stopwatch !== null), ); } - + public function getName() { return 'stopwatch'; } - + public function startEvent($name) { if ($this->stopwatch instanceof Stopwatch) { $this->stopwatch->start($name, 'template'); } } - + public function stopEvent($name) { if ($this->stopwatch instanceof Stopwatch) { diff --git a/src/Symfony/Bridge/Twig/Node/StopwatchNode.php b/src/Symfony/Bridge/Twig/Node/StopwatchNode.php index f217bc0bc09ae..e38fd4e6e92d3 100644 --- a/src/Symfony/Bridge/Twig/Node/StopwatchNode.php +++ b/src/Symfony/Bridge/Twig/Node/StopwatchNode.php @@ -22,11 +22,11 @@ public function __construct($name, $body, $lineno = 0, $tag = null) { parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag); } - + public function compile(\Twig_Compiler $compiler) { $name = $this->getAttribute('name'); - + $compiler ->write('$this->env->getExtension(\'stopwatch\')->startEvent(\''.$name.'\');') ->subcompile($this->getNode('body')) diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php index b8cadf2af30b0..86ab498a05bbf 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php @@ -62,10 +62,8 @@ 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.bar' %}something{% endstopwatch %}", 'foo.bar'), + array('{% stopwatch "foo.bar" %}something{% endstopwatch %}', 'foo.bar'), ); } diff --git a/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php b/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php index bdba8f50cb7d3..fc178946fb9e9 100644 --- a/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php +++ b/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php @@ -32,7 +32,7 @@ public function parse(\Twig_Token $token) { $lineno = $token->getLine(); $stream = $this->parser->getStream(); - + // {% stopwatch bar %} if ($stream->test(\Twig_Token::NAME_TYPE)) { $name = $stream->expect(\Twig_Token::NAME_TYPE)->getValue(); @@ -41,35 +41,35 @@ public function parse(\Twig_Token $token) } if (in_array($name, $this->_events)) { - throw new \Twig_Error_Syntax(sprintf("The stopwatch event '%s' has already been defined", $name)); + throw new \Twig_Error_Syntax(sprintf('The stopwatch event "%s" has already been defined.', $name)); } $this->_events[] = $name; $stream->expect(\Twig_Token::BLOCK_END_TYPE); - + // {% endstopwatch %} or {% endstopwatch bar %} $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)); + 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 $body; } - + public function decideStopwatchEnd(\Twig_Token $token) { return $token->test('endstopwatch'); } - + public function getTag() { return 'stopwatch'; From 2f67776a66c2cdf8505696bfa225e95396da1b57 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 11 Aug 2013 20:24:06 +0200 Subject: [PATCH 3/5] removed unneeded safeguard as it's already done during compilation --- .../Twig/Extension/StopwatchExtension.php | 19 +++++-------------- .../Bridge/Twig/Node/StopwatchNode.php | 8 +++----- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php b/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php index 6379f1a73d044..1055b5bac8db8 100644 --- a/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php @@ -28,6 +28,11 @@ public function __construct(Stopwatch $stopwatch = null) $this->stopwatch = $stopwatch; } + public function getStopwatch() + { + return $this->stopwatch; + } + public function getTokenParsers() { return array( @@ -44,18 +49,4 @@ public function getName() { return 'stopwatch'; } - - public function startEvent($name) - { - if ($this->stopwatch instanceof Stopwatch) { - $this->stopwatch->start($name, 'template'); - } - } - - public function stopEvent($name) - { - if ($this->stopwatch instanceof Stopwatch) { - $this->stopwatch->stop($name); - } - } } diff --git a/src/Symfony/Bridge/Twig/Node/StopwatchNode.php b/src/Symfony/Bridge/Twig/Node/StopwatchNode.php index e38fd4e6e92d3..eac3fce9a4975 100644 --- a/src/Symfony/Bridge/Twig/Node/StopwatchNode.php +++ b/src/Symfony/Bridge/Twig/Node/StopwatchNode.php @@ -25,12 +25,10 @@ public function __construct($name, $body, $lineno = 0, $tag = null) public function compile(\Twig_Compiler $compiler) { - $name = $this->getAttribute('name'); - $compiler - ->write('$this->env->getExtension(\'stopwatch\')->startEvent(\''.$name.'\');') + ->write(sprintf("\$this->env->getExtension('stopwatch')->getStopwatch()->start('%s', 'template');\n", $this->getAttribute('name'))) ->subcompile($this->getNode('body')) - ->write('$this->env->getExtension(\'stopwatch\')->stopEvent(\''.$name.'\');') - ->raw("\n"); + ->write(sprintf("\$this->env->getExtension('stopwatch')->getStopwatch()->stop('%s');\n", $this->getAttribute('name'))) + ; } } From 459097413db6daf45817da2f4d93fded78a2dc45 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 11 Aug 2013 22:43:28 +0200 Subject: [PATCH 4/5] removed code that prevents the stopwatch to work properly --- .../Twig/Tests/Extension/StopwatchExtensionTest.php | 9 +-------- .../Bridge/Twig/TokenParser/StopwatchTokenParser.php | 6 ------ 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php index 86ab498a05bbf..9ffb6d9329d46 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php @@ -26,14 +26,6 @@ protected function setUp() } } - /** - * @expectedException \Twig_Error_Syntax - */ - public function testFailIfNameAlreadyExists() - { - $this->testTiming('{% stopwatch foo %}{% endstopwatch %}{% stopwatch foo %}{% endstopwatch %}', array()); - } - /** * @expectedException \Twig_Error_Syntax */ @@ -64,6 +56,7 @@ public function getTimingTemplates() array('{% stopwatch foo %}symfony2 is fun{% endstopwatch %}{% stopwatch bar %}something{% endstopwatch %}', array('foo', 'bar')), array('{% stopwatch foo %}something{% endstopwatch foo %}', 'foo'), array('{% stopwatch "foo.bar" %}something{% endstopwatch %}', 'foo.bar'), + array('{% stopwatch foo %}something{% endstopwatch foo %}{% stopwatch foo %}something else{% endstopwatch foo %}', array('foo', 'foo')), ); } diff --git a/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php b/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php index fc178946fb9e9..ddbaeb4958a91 100644 --- a/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php +++ b/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php @@ -21,7 +21,6 @@ class StopwatchTokenParser extends \Twig_TokenParser { protected $stopwatchIsAvailable; - protected $_events = array(); public function __construct($stopwatchIsAvailable) { @@ -40,11 +39,6 @@ public function parse(\Twig_Token $token) $name = $stream->expect(\Twig_Token::STRING_TYPE)->getValue(); } - if (in_array($name, $this->_events)) { - throw new \Twig_Error_Syntax(sprintf('The stopwatch event "%s" has already been defined.', $name)); - } - $this->_events[] = $name; - $stream->expect(\Twig_Token::BLOCK_END_TYPE); // {% endstopwatch %} or {% endstopwatch bar %} From 29a58e7a03ac2606dd17aa15f67c2a9554492a50 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 12 Aug 2013 08:31:36 +0200 Subject: [PATCH 5/5] change the stopwatch argument to be any valid expression --- .../Bridge/Twig/Node/StopwatchNode.php | 18 ++++++++++++++---- .../Extension/StopwatchExtensionTest.php | 11 ++++++----- .../Twig/TokenParser/StopwatchTokenParser.php | 19 ++++--------------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Node/StopwatchNode.php b/src/Symfony/Bridge/Twig/Node/StopwatchNode.php index eac3fce9a4975..cc12abd05d06e 100644 --- a/src/Symfony/Bridge/Twig/Node/StopwatchNode.php +++ b/src/Symfony/Bridge/Twig/Node/StopwatchNode.php @@ -18,17 +18,27 @@ */ 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 9ffb6d9329d46..626131de251e1 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php @@ -31,7 +31,7 @@ protected function setUp() */ public function testFailIfStoppingWrongEvent() { - $this->testTiming('{% stopwatch foo %}{% endstopwatch bar %}', array()); + $this->testTiming('{% stopwatch "foo" %}{% endstopwatch "bar" %}', array()); } /** @@ -52,11 +52,12 @@ public function testTiming($template, $events) 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 ddbaeb4958a91..2983e4cb6b03b 100644 --- a/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php +++ b/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php @@ -32,28 +32,17 @@ public function parse(\Twig_Token $token) $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; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy