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
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $serverName; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Twig_Environment $environment |
29
|
|
|
* @param Stopwatch $stopwatch |
30
|
|
|
* @param string $serverName |
31
|
|
|
*/ |
32
|
|
|
public function __construct(\Twig_Environment $environment, Stopwatch $stopwatch, $serverName = 'localhost') |
33
|
|
|
{ |
34
|
|
|
$this->environment = $environment; |
35
|
|
|
$this->stopwatch = $stopwatch; |
36
|
|
|
$this->serverName = $serverName; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Renders a template. |
41
|
|
|
* |
42
|
|
|
* @param string $name The template name |
43
|
|
|
* @param array $context An array of parameters to pass to the template |
44
|
|
|
* |
45
|
|
|
* @return string The rendered template |
46
|
|
|
* |
47
|
|
|
* @throws \Twig_Error_Loader When the template cannot be found |
48
|
|
|
* @throws \Twig_Error_Syntax When an error occurred during compilation |
49
|
|
|
* @throws \Twig_Error_Runtime When an error occurred during rendering |
50
|
|
|
*/ |
51
|
|
|
public function render($name, array $context = array()) |
52
|
|
|
{ |
53
|
|
|
$e = $this->stopwatch->start(array( |
54
|
|
|
'server' => $this->serverName, |
55
|
|
|
'group' => 'twig::render', |
56
|
|
|
'twig_template' => (string)$name, |
57
|
|
|
)); |
58
|
|
|
|
59
|
|
|
$ret = $this->environment->render($name, $context); |
60
|
|
|
|
61
|
|
|
$e->stop(); |
62
|
|
|
|
63
|
|
|
return $ret; |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return \Twig_Environment |
69
|
|
|
*/ |
70
|
|
|
public function getEnvironment() |
71
|
|
|
{ |
72
|
|
|
return (method_exists($this->environment, 'getEnvironment'))? $this->environment->getEnvironment():$this->environment; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function getLoader() |
79
|
|
|
{ |
80
|
|
|
return $this->environment->getLoader(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* is triggered when invoking inaccessible methods in an object context. |
86
|
|
|
* |
87
|
|
|
* @param $name string |
88
|
|
|
* @param $arguments array |
89
|
|
|
* @return mixed |
90
|
|
|
* @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods |
91
|
|
|
*/ |
92
|
|
|
function __call($name, $arguments) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
return call_user_func_array(array($this->environment,$name),$arguments); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: