11
11
12
12
namespace Cache \CacheBundle \DataCollector ;
13
13
14
- use Cache \CacheBundle \Cache \Recording \TraceableAdapterEvent ;
15
14
use Symfony \Component \HttpFoundation \Request ;
16
15
use Symfony \Component \HttpFoundation \Response ;
17
16
use Symfony \Component \HttpKernel \DataCollector \DataCollector ;
17
+ use Symfony \Component \VarDumper \Caster \CutStub ;
18
+ use Symfony \Component \VarDumper \Cloner \Stub ;
19
+ use Symfony \Component \VarDumper \Cloner \VarCloner ;
18
20
19
21
/**
20
22
* @author Aaron Scherer <[email protected] >
@@ -29,6 +31,11 @@ class CacheDataCollector extends DataCollector
29
31
*/
30
32
private $ instances = [];
31
33
34
+ /**
35
+ * @type VarCloner
36
+ */
37
+ private $ cloner = null ;
38
+
32
39
/**
33
40
* @param string $name
34
41
* @param CacheProxyInterface $instance
@@ -46,13 +53,43 @@ public function collect(Request $request, Response $response, \Exception $except
46
53
$ empty = ['calls ' => [], 'config ' => [], 'options ' => [], 'statistics ' => []];
47
54
$ this ->data = ['instances ' => $ empty , 'total ' => $ empty ];
48
55
foreach ($ this ->instances as $ name => $ instance ) {
49
- $ this ->data ['instances ' ]['calls ' ][$ name ] = $ instance ->__getCalls ();
56
+ $ calls = $ instance ->__getCalls ();
57
+ foreach ($ calls as $ call ) {
58
+ if (isset ($ call ->result )) {
59
+ $ call ->result = $ this ->cloneData ($ call ->result );
60
+ }
61
+ if (isset ($ call ->argument )) {
62
+ $ call ->argument = $ this ->cloneData ($ call ->argument );
63
+ }
64
+ }
65
+ $ this ->data ['instances ' ]['calls ' ][$ name ] = $ calls ;
50
66
}
51
67
52
68
$ this ->data ['instances ' ]['statistics ' ] = $ this ->calculateStatistics ();
53
69
$ this ->data ['total ' ]['statistics ' ] = $ this ->calculateTotalStatistics ();
54
70
}
55
71
72
+ /**
73
+ * To be compatible with many versions of Symfony.
74
+ *
75
+ * @param $var
76
+ */
77
+ private function cloneData ($ var )
78
+ {
79
+ if (method_exists ($ this , 'cloneVar ' )) {
80
+ // Symfony 3.2 or higher
81
+ return $ this ->cloneVar ($ var );
82
+ }
83
+
84
+ if (null === $ this ->cloner ) {
85
+ $ this ->cloner = new VarCloner ();
86
+ $ this ->cloner ->setMaxItems (-1 );
87
+ $ this ->cloner ->addCasters ($ this ->getCasters ());
88
+ }
89
+
90
+ return $ this ->cloner ->cloneVar ($ var );
91
+ }
92
+
56
93
/**
57
94
* {@inheritdoc}
58
95
*/
@@ -174,4 +211,24 @@ private function calculateTotalStatistics()
174
211
175
212
return $ totals ;
176
213
}
214
+
215
+ /**
216
+ * @return callable[] The casters to add to the cloner
217
+ */
218
+ private function getCasters ()
219
+ {
220
+ return [
221
+ '* ' => function ($ v , array $ a , Stub $ s , $ isNested ) {
222
+ if (!$ v instanceof Stub) {
223
+ foreach ($ a as $ k => $ v ) {
224
+ if (is_object ($ v ) && !$ v instanceof \DateTimeInterface && !$ v instanceof Stub) {
225
+ $ a [$ k ] = new CutStub ($ v );
226
+ }
227
+ }
228
+ }
229
+
230
+ return $ a ;
231
+ },
232
+ ];
233
+ }
177
234
}
0 commit comments