Added Stopwatch Helper

This commit is contained in:
WouterJ 2013-09-09 17:26:18 +02:00
parent 599c86509e
commit 3ee2989287
4 changed files with 92 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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