1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Mikhail Dolgov <[email protected]> |
4
|
|
|
* @date 09.03.2016 15:28 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SilexPinbaProvider\Twig; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
use Intaro\PinbaBundle\Stopwatch\Stopwatch; |
11
|
|
|
|
12
|
|
|
class TimedTwigEnvironment extends \Twig_Environment{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Stopwatch |
16
|
|
|
*/ |
17
|
|
|
private $stopwatch = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $serverName = 'localhost'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Stopwatch $stopwatch |
26
|
|
|
* @return $this |
27
|
|
|
*/ |
28
|
|
|
public function setStopwatch($stopwatch) |
29
|
|
|
{ |
30
|
|
|
$this->stopwatch = $stopwatch; |
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $serverName |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
public function setServerName($serverName) |
39
|
|
|
{ |
40
|
|
|
$this->serverName = $serverName; |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Renders a template. |
46
|
|
|
* |
47
|
|
|
* @param string $name The template name |
48
|
|
|
* @param array $context An array of parameters to pass to the template |
49
|
|
|
* |
50
|
|
|
* @return string The rendered template |
51
|
|
|
* |
52
|
|
|
* @throws \Twig_Error_Loader When the template cannot be found |
53
|
|
|
* @throws \Twig_Error_Syntax When an error occurred during compilation |
54
|
|
|
* @throws \Twig_Error_Runtime When an error occurred during rendering |
55
|
|
|
*/ |
56
|
|
|
public function render($name, array $context = array()) |
57
|
|
|
{ |
58
|
|
|
if(null === $this->stopwatch) { |
59
|
|
|
return parent::render($name, $context); |
60
|
|
|
} |
61
|
|
|
$e = $this->stopwatch->start(array( |
62
|
|
|
'server' => $this->serverName, |
63
|
|
|
'group' => 'twig::render', |
64
|
|
|
'twig_template' => (string)$name, |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
$ret = parent::render($name, $context); |
68
|
|
|
$e->stop(); |
69
|
|
|
return $ret; |
70
|
|
|
} |
71
|
|
|
} |