-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeasureTwigExtension.php
More file actions
70 lines (59 loc) · 1.54 KB
/
MeasureTwigExtension.php
File metadata and controls
70 lines (59 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace DebugBar\Bridge\Twig;
use DebugBar\Bridge\Twig\MeasureTwigTokenParser;
use DebugBar\DataCollector\TimeDataCollector;
use Twig\Extension\AbstractExtension;
/**
* Access debugbar timeline measure in your Twig templates.
* Based on Symfony\Bridge\Twig\Extension\StopwatchExtension
*
* @package DebugBar\Bridge\Twig
*/
class MeasureTwigExtension extends AbstractExtension
{
protected ?TimeDataCollector $timeCollector;
/**
* @var string
*/
protected string $tagName;
public function __construct(?TimeDataCollector $timeCollector, string $tagName = 'measure')
{
$this->timeCollector = $timeCollector;
$this->tagName = $tagName;
}
/**
* {@inheritDoc}
*/
public function getName()
{
return static::class;
}
/**
* @return \Twig\TokenParser\TokenParserInterface[]
*/
public function getTokenParsers()
{
return [
/*
* {% measure foo %}
* Some stuff which will be recorded on the timeline
* {% endmeasure %}
*/
new MeasureTwigTokenParser(!is_null($this->timeCollector), $this->tagName, $this->getName()),
];
}
public function startMeasure(...$arg)
{
if (!$this->timeCollector) {
return;
}
$this->timeCollector->startMeasure(...$arg);
}
public function stopMeasure(...$arg)
{
if (!$this->timeCollector) {
return;
}
$this->timeCollector->stopMeasure(...$arg);
}
}