Skip to content

Commit 1a27ae0

Browse files
authored
Lazy load twig collectors (#1600)
1 parent c3f7489 commit 1a27ae0

File tree

3 files changed

+45
-32
lines changed

3 files changed

+45
-32
lines changed

src/Twig/Extension/Debug.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,32 @@
44

55
use DebugBar\Bridge\Twig\DebugTwigExtension;
66
use Illuminate\Foundation\Application;
7+
use Twig\Environment;
78

89
/**
910
* Access debugbar debug in your Twig templates.
1011
*/
1112
class Debug extends DebugTwigExtension
1213
{
14+
protected $app;
15+
1316
/**
1417
* Create a new debug extension.
1518
*
1619
* @param \Illuminate\Foundation\Application $app
1720
*/
1821
public function __construct(Application $app)
1922
{
20-
$messagesCollector = null;
21-
if ($app->bound('debugbar') && $app['debugbar']->hasCollector('messages')) {
22-
$messagesCollector = $app['debugbar']['messages'];
23+
$this->app = $app;
24+
parent::__construct(null);
25+
}
26+
27+
public function debug(Environment $env, $context)
28+
{
29+
if ($this->app->bound('debugbar') && $this->app['debugbar']->hasCollector('messages')) {
30+
$this->messagesCollector = $this->app['debugbar']['messages'];
2331
}
2432

25-
parent::__construct($messagesCollector);
33+
return parent::debug($env, $context);
2634
}
2735
}

src/Twig/Extension/Extension.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Twig/Extension/Stopwatch.php

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Barryvdh\Debugbar\Twig\Extension;
44

55
use DebugBar\Bridge\Twig\MeasureTwigExtension;
6+
use DebugBar\Bridge\Twig\MeasureTwigTokenParser;
67
use Illuminate\Foundation\Application;
78

89
/**
@@ -23,28 +24,46 @@ class Stopwatch extends MeasureTwigExtension
2324
*/
2425
public function __construct(Application $app)
2526
{
26-
$timeCollector = null;
2727
if ($app->bound('debugbar')) {
2828
$this->debugbar = $app['debugbar'];
29-
30-
if ($app['debugbar']->hasCollector('time')) {
31-
$timeCollector = $app['debugbar']['time'];
32-
}
3329
}
34-
35-
parent::__construct($timeCollector, 'stopwatch');
36-
}
3730

38-
/**
39-
* {@inheritDoc}
40-
*/
41-
public function getName()
42-
{
43-
return static::class;
31+
parent::__construct(null, 'stopwatch');
4432
}
4533

34+
4635
public function getDebugbar()
4736
{
4837
return $this->debugbar;
4938
}
39+
40+
public function getTokenParsers()
41+
{
42+
return [
43+
/*
44+
* {% measure foo %}
45+
* Some stuff which will be recorded on the timeline
46+
* {% endmeasure %}
47+
*/
48+
new MeasureTwigTokenParser(!is_null($this->debugbar), $this->tagName, $this->getName()),
49+
];
50+
}
51+
52+
public function startMeasure(...$arg)
53+
{
54+
if (!$this->debugbar || !$this->debugbar->hasCollector('time')) {
55+
return;
56+
}
57+
58+
$this->debugbar->getCollector('time')->startMeasure(...$arg);
59+
}
60+
61+
public function stopMeasure(...$arg)
62+
{
63+
if (!$this->debugbar || !$this->debugbar->hasCollector('time')) {
64+
return;
65+
}
66+
67+
$this->debugbar->getCollector('time')->stopMeasure(...$arg);
68+
}
5069
}

0 commit comments

Comments
 (0)