Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e370f6

Browse files
committed
merged branch WouterJ/stopwatch_php (PR #8968)
This PR was merged into the master branch. Discussion ---------- Added Stopwatch helper for PHP templates | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | symfony/symfony-docs#2960 Commits ------- 3ee2989 Added Stopwatch Helper
2 parents 7be1870 + 3ee2989 commit 3e370f6

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

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

910
2.3.0
1011
-----

src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<parameter key="templating.helper.code.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper</parameter>
1616
<parameter key="templating.helper.translator.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper</parameter>
1717
<parameter key="templating.helper.form.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper</parameter>
18+
<parameter key="templating.helper.stopwatch.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\StopwatchHelper</parameter>
1819
<parameter key="templating.form.engine.class">Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine</parameter>
1920
<parameter key="templating.form.renderer.class">Symfony\Component\Form\FormRenderer</parameter>
2021
<parameter key="templating.globals.class">Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables</parameter>
@@ -101,6 +102,11 @@
101102
<argument type="service" id="templating.form.renderer" />
102103
</service>
103104

105+
<service id="templating.helper.stopwatch" class="%templating.helper.stopwatch.class%">
106+
<tag name="templating.helper" alias="stopwatch" />
107+
<argument type="service" id="debug.stopwatch" on-invalid="ignore" />
108+
</service>
109+
104110
<service id="templating.form.engine" class="%templating.form.engine.class%" public="false">
105111
<argument type="service" id="templating.engine.php" />
106112
<argument>%templating.helper.form.resources%</argument>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
13+
14+
use Symfony\Component\Stopwatch\Stopwatch;
15+
use Symfony\Component\Templating\Helper\Helper;
16+
17+
/**
18+
* StopwatchHelper provides methods time your PHP templates.
19+
*
20+
* @author Wouter J <[email protected]>
21+
*/
22+
class StopwatchHelper extends Helper
23+
{
24+
private $stopwatch;
25+
26+
public function __construct(Stopwatch $stopwatch = null)
27+
{
28+
$this->stopwatch = $stopwatch;
29+
}
30+
31+
public function getName()
32+
{
33+
return 'stopwatch';
34+
}
35+
36+
public function __call($method, $arguments = array())
37+
{
38+
if (null !== $this->stopwatch) {
39+
if (method_exists($this->stopwatch, $method)) {
40+
return call_user_func_array(array($this->stopwatch, $method), $arguments);
41+
}
42+
43+
throw new \BadMethodCallException(sprintf('Method "%s" of Stopwatch does not exist', $method));
44+
}
45+
}
46+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
13+
14+
use Symfony\Bundle\FrameworkBundle\Templating\Helper\StopwatchHelper;
15+
16+
class StopwatchHelperTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testDevEnvironment()
19+
{
20+
$stopwatch = $this->getMock('Symfony\Component\Stopwatch\Stopwatch');
21+
$stopwatch->expects($this->once())
22+
->method('start')
23+
->with('foo');
24+
25+
$helper = new StopwatchHelper($stopwatch);
26+
$helper->start('foo');
27+
}
28+
29+
public function testProdEnvironment()
30+
{
31+
$helper = new StopwatchHelper(null);
32+
33+
try {
34+
$helper->start('foo');
35+
} catch (\BadMethodCallException $e) {
36+
$this->fail('Assumed stopwatch is not called when not provided');
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)