1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EmanueleMinotto\TwigCacheBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Asm89\Twig\CacheExtension\CacheStrategyInterface; |
6
|
|
|
use Asm89\Twig\CacheExtension\Extension as Asm89_Extension; |
7
|
|
|
use EmanueleMinotto\TwigCacheBundle\Strategy\ProfilerStrategy; |
8
|
|
|
use Exception; |
9
|
|
|
use Serializable; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
|
|
class ProfilerExtension extends Asm89_Extension implements DataCollectorInterface, Serializable |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Data about fetchBlock requests. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $fetchBlock = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Data about generateKey requests. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $generateKey = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Cache hits. |
35
|
|
|
* |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private $hits = 0; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Caching strategy used. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $strategyClass; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param CacheStrategyInterface $cacheStrategy |
49
|
|
|
*/ |
50
|
|
|
public function __construct(CacheStrategyInterface $cacheStrategy) |
51
|
|
|
{ |
52
|
|
|
parent::__construct(new ProfilerStrategy($cacheStrategy, $this)); |
53
|
|
|
|
54
|
|
|
$this->strategyClass = get_class($cacheStrategy); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Collects data for the given Request and Response. |
59
|
|
|
* |
60
|
|
|
* @param Request $request A Request instance |
61
|
|
|
* @param Response $response A Response instance |
62
|
|
|
* @param Exception $exception An Exception instance |
63
|
|
|
*/ |
64
|
|
|
public function collect(Request $request, Response $response, Exception $exception = null) |
65
|
|
|
{ |
66
|
|
|
// nothing to do here |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Store a fetch request. |
71
|
|
|
* |
72
|
|
|
* @param mixed $key |
73
|
|
|
* @param string $output |
74
|
|
|
*/ |
75
|
|
|
public function addFetchBlock($key, $output) |
76
|
|
|
{ |
77
|
|
|
$this->fetchBlock[] = [$key, $output]; |
78
|
|
|
|
79
|
|
|
if ($output) { |
80
|
|
|
++$this->hits; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Store a generateKey request. |
86
|
|
|
* |
87
|
|
|
* @param string $annotation |
88
|
|
|
* @param mixed $value |
89
|
|
|
*/ |
90
|
|
|
public function addGenerateKey($annotation, $value) |
91
|
|
|
{ |
92
|
|
|
$this->generateKey[] = [ |
93
|
|
|
'annotation' => $annotation, |
94
|
|
|
'value' => $value, |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get data stored in this profiler. |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function getData() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
|
|
'fetchBlock' => $this->fetchBlock, |
107
|
|
|
'generateKey' => $this->generateKey, |
108
|
|
|
'hits' => $this->hits, |
109
|
|
|
'strategyClass' => $this->strategyClass, |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* String representation of object. |
115
|
|
|
* |
116
|
|
|
* @link http://php.net/manual/en/serializable.serialize.php |
117
|
|
|
* |
118
|
|
|
* @return string The string representation of the object or null. |
119
|
|
|
*/ |
120
|
|
|
public function serialize() |
121
|
|
|
{ |
122
|
|
|
return serialize($this->getData()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Constructs the object. |
127
|
|
|
* |
128
|
|
|
* @link http://php.net/manual/en/serializable.unserialize.php |
129
|
|
|
* |
130
|
|
|
* @param string $serialized The string representation of the object. |
131
|
|
|
*/ |
132
|
|
|
public function unserialize($serialized) |
133
|
|
|
{ |
134
|
|
|
$data = unserialize($serialized); |
135
|
|
|
|
136
|
|
|
if (is_array($data)) { |
137
|
|
|
$this->fetchBlock = $data['fetchBlock']; |
138
|
|
|
$this->generateKey = $data['generateKey']; |
139
|
|
|
$this->hits = $data['hits']; |
140
|
|
|
$this->strategyClass = $data['strategyClass']; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Reset profiler data |
146
|
|
|
*/ |
147
|
|
|
public function reset() |
148
|
|
|
{ |
149
|
|
|
$this->fetchBlock = []; |
150
|
|
|
$this->generateKey = []; |
151
|
|
|
$this->hits = 0; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|