Skip to content

Added Stopwatch helper for PHP templates #8968

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 1 commit into from
Sep 12, 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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* allowed multiple IP addresses in profiler matcher settings
* added stopwatch helper to time templates with the WebProfilerBundle

2.3.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<parameter key="templating.helper.code.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper</parameter>
<parameter key="templating.helper.translator.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper</parameter>
<parameter key="templating.helper.form.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper</parameter>
<parameter key="templating.helper.stopwatch.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\StopwatchHelper</parameter>
<parameter key="templating.form.engine.class">Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine</parameter>
<parameter key="templating.form.renderer.class">Symfony\Component\Form\FormRenderer</parameter>
<parameter key="templating.globals.class">Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables</parameter>
Expand Down Expand Up @@ -101,6 +102,11 @@
<argument type="service" id="templating.form.renderer" />
</service>

<service id="templating.helper.stopwatch" class="%templating.helper.stopwatch.class%">
<tag name="templating.helper" alias="stopwatch" />
<argument type="service" id="debug.stopwatch" on-invalid="ignore" />
</service>

<service id="templating.form.engine" class="%templating.form.engine.class%" public="false">
<argument type="service" id="templating.engine.php" />
<argument>%templating.helper.form.resources%</argument>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Bundle\FrameworkBundle\Templating\Helper;

use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Templating\Helper\Helper;

/**
* StopwatchHelper provides methods time your PHP templates.
*
* @author Wouter J <wouter@wouterj.nl>
*/
class StopwatchHelper extends Helper
{
private $stopwatch;

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

public function getName()
{
return 'stopwatch';
}

public function __call($method, $arguments = array())
{
if (null !== $this->stopwatch) {
if (method_exists($this->stopwatch, $method)) {
return call_user_func_array(array($this->stopwatch, $method), $arguments);
}

throw new \BadMethodCallException(sprintf('Method "%s" of Stopwatch does not exist', $method));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Bundle\FrameworkBundle\Tests\Templating\Helper;

use Symfony\Bundle\FrameworkBundle\Templating\Helper\StopwatchHelper;

class StopwatchHelperTest extends \PHPUnit_Framework_TestCase
{
public function testDevEnvironment()
{
$stopwatch = $this->getMock('Symfony\Component\Stopwatch\Stopwatch');
$stopwatch->expects($this->once())
->method('start')
->with('foo');

$helper = new StopwatchHelper($stopwatch);
$helper->start('foo');
}

public function testProdEnvironment()
{
$helper = new StopwatchHelper(null);

try {
$helper->start('foo');
} catch (\BadMethodCallException $e) {
$this->fail('Assumed stopwatch is not called when not provided');
}
}
}