Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[TwigBundle] Created stopwatch tag for profiling templates #8719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.4.0
-----

* added stopwatch tag to time templates with the WebProfilerBundle

2.3.0
-----

Expand Down
52 changes: 52 additions & 0 deletions 52 src/Symfony/Bridge/Twig/Extension/StopwatchExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <wouter@wouterj.nl>
*/
class StopwatchExtension extends \Twig_Extension
{
private $stopwatch;

public function __construct(Stopwatch $stopwatch = null)
{
$this->stopwatch = $stopwatch;
}

public function getStopwatch()
{
return $this->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';
}
}
44 changes: 44 additions & 0 deletions 44 src/Symfony/Bridge/Twig/Node/StopwatchNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <wouter@wouterj.nl>
*/
class StopwatchNode extends \Twig_Node
{
public function __construct(\Twig_NodeInterface $name, $body, \Twig_Node_Expression_AssignName $var, $lineno = 0, $tag = null)
{
parent::__construct(array('body' => $body, 'name' => $name, 'var' => $var), array(), $lineno, $tag);
}

public function compile(\Twig_Compiler $compiler)
{
$compiler
->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("\$this->env->getExtension('stopwatch')->getStopwatch()->stop(")
->subcompile($this->getNode('var'))
->raw(");\n")
;
}
}
83 changes: 83 additions & 0 deletions 83 src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 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('{% 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 %}{% stopwatch "foo" %}something else{% endstopwatch %}', array('foo', 'foo')),
);
}

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;
}
}
60 changes: 60 additions & 0 deletions 60 src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <wouter@wouterj.nl>
*/
class StopwatchTokenParser extends \Twig_TokenParser
{
protected $stopwatchIsAvailable;

public function __construct($stopwatchIsAvailable)
{
$this->stopwatchIsAvailable = $stopwatchIsAvailable;
}

public function parse(\Twig_Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();

// {% stopwatch 'bar' %}
$name = $this->parser->getExpressionParser()->parseExpression();

$stream->expect(\Twig_Token::BLOCK_END_TYPE);

// {% endstopwatch %}
$body = $this->parser->subparse(array($this, 'decideStopwatchEnd'), true);
$stream->expect(\Twig_Token::BLOCK_END_TYPE);

if ($this->stopwatchIsAvailable) {
return new StopwatchNode($name, $body, new \Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $lineno, $this->getTag());
}

return $body;
}

public function decideStopwatchEnd(\Twig_Token $token)
{
return $token->test('endstopwatch');
}

public function getTag()
{
return 'stopwatch';
}
}
6 changes: 6 additions & 0 deletions 6 src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<parameter key="twig.extension.yaml.class">Symfony\Bridge\Twig\Extension\YamlExtension</parameter>
<parameter key="twig.extension.form.class">Symfony\Bridge\Twig\Extension\FormExtension</parameter>
<parameter key="twig.extension.httpkernel.class">Symfony\Bridge\Twig\Extension\HttpKernelExtension</parameter>
<parameter key="twig.extension.debug.stopwatch.class">Symfony\Bridge\Twig\Extension\StopwatchExtension</parameter>
<parameter key="twig.form.engine.class">Symfony\Bridge\Twig\Form\TwigRendererEngine</parameter>
<parameter key="twig.form.renderer.class">Symfony\Bridge\Twig\Form\TwigRenderer</parameter>
<parameter key="twig.translation.extractor.class">Symfony\Bridge\Twig\Translation\TwigExtractor</parameter>
Expand Down Expand Up @@ -86,6 +87,11 @@
<tag name="twig.extension" />
</service>

<service id="twig.extension.debug.stopwatch" class="%twig.extension.debug.stopwatch.class%" public="false">
<tag name="twig.extension" />
<argument type="service" id="debug.stopwatch" on-invalid="ignore" />
</service>

<service id="twig.extension.httpkernel" class="%twig.extension.httpkernel.class%" public="false">
<argument type="service" id="fragment.handler" />
</service>
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.