Skip to content

Commit 7bafab8

Browse files
authored
Properly clone variables (#82)
* Properly clone variables * Applied changes from StyleCI
1 parent 8850baa commit 7bafab8

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"require": {
2727
"php": "^5.6 || ^7.0",
2828
"symfony/framework-bundle": "^2.7 || ^3.0",
29+
"symfony/var-dumper": "^3.3",
2930
"cache/taggable-cache": "^1.0",
3031
"cache/session-handler": "^1.0",
3132
"nyholm/nsa": "^1.1"

src/DataCollector/CacheDataCollector.php

+59-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace Cache\CacheBundle\DataCollector;
1313

14-
use Cache\CacheBundle\Cache\Recording\TraceableAdapterEvent;
1514
use Symfony\Component\HttpFoundation\Request;
1615
use Symfony\Component\HttpFoundation\Response;
1716
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;
1820

1921
/**
2022
* @author Aaron Scherer <[email protected]>
@@ -29,6 +31,11 @@ class CacheDataCollector extends DataCollector
2931
*/
3032
private $instances = [];
3133

34+
/**
35+
* @type VarCloner
36+
*/
37+
private $cloner = null;
38+
3239
/**
3340
* @param string $name
3441
* @param CacheProxyInterface $instance
@@ -46,13 +53,43 @@ public function collect(Request $request, Response $response, \Exception $except
4653
$empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []];
4754
$this->data = ['instances' => $empty, 'total' => $empty];
4855
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;
5066
}
5167

5268
$this->data['instances']['statistics'] = $this->calculateStatistics();
5369
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
5470
}
5571

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+
5693
/**
5794
* {@inheritdoc}
5895
*/
@@ -174,4 +211,24 @@ private function calculateTotalStatistics()
174211

175212
return $totals;
176213
}
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+
}
177234
}

0 commit comments

Comments
 (0)