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 TimedTwigDecorator extends \Twig_Environment{ |
13
|
|
|
/** |
14
|
|
|
* @var \Twig_Environment |
15
|
|
|
*/ |
16
|
|
|
private $environment; |
17
|
|
|
/** |
18
|
|
|
* @var Stopwatch |
19
|
|
|
*/ |
20
|
|
|
private $stopwatch; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \Twig_Environment $environment |
24
|
|
|
* @param Stopwatch $stopwatch |
25
|
|
|
*/ |
26
|
|
|
public function __construct(\Twig_Environment $environment, Stopwatch $stopwatch) |
27
|
|
|
{ |
28
|
|
|
$this->environment = $environment; |
29
|
|
|
$this->stopwatch = $stopwatch; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Renders a template. |
34
|
|
|
* |
35
|
|
|
* @param string $name The template name |
36
|
|
|
* @param array $context An array of parameters to pass to the template |
37
|
|
|
* |
38
|
|
|
* @return string The rendered template |
39
|
|
|
* |
40
|
|
|
* @throws \Twig_Error_Loader When the template cannot be found |
41
|
|
|
* @throws \Twig_Error_Syntax When an error occurred during compilation |
42
|
|
|
* @throws \Twig_Error_Runtime When an error occurred during rendering |
43
|
|
|
*/ |
44
|
|
|
public function render($name, array $context = array()) |
45
|
|
|
{ |
46
|
|
|
$e = $this->stopwatch->start(array( |
47
|
|
|
'server' => 'localhost', |
48
|
|
|
'group' => 'twig::render', |
49
|
|
|
'twig_template' => (string)$name, |
50
|
|
|
)); |
51
|
|
|
|
52
|
|
|
$ret = $this->environment->render($name, $context); |
53
|
|
|
|
54
|
|
|
$e->stop(); |
55
|
|
|
|
56
|
|
|
return $ret; |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* is triggered when invoking inaccessible methods in an object context. |
62
|
|
|
* |
63
|
|
|
* @param $name string |
64
|
|
|
* @param $arguments array |
65
|
|
|
* @return mixed |
66
|
|
|
* @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods |
67
|
|
|
*/ |
68
|
|
|
function __call($name, $arguments) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
return $this->environment->$name($arguments); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.