Skip to content

Commit 8b4225e

Browse files
[VarDumper] Allow seamless use of Data clones
1 parent 2d1b83d commit 8b4225e

File tree

2 files changed

+24
-61
lines changed

2 files changed

+24
-61
lines changed

Extension/DataCollector/FormDataCollector.php

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Validator\ConstraintViolationInterface;
2121
use Symfony\Component\VarDumper\Caster\Caster;
2222
use Symfony\Component\VarDumper\Caster\ClassStub;
23+
use Symfony\Component\VarDumper\Caster\CutStub;
2324
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
2425
use Symfony\Component\VarDumper\Cloner\Data;
2526
use Symfony\Component\VarDumper\Cloner\Stub;
@@ -80,7 +81,8 @@ class FormDataCollector extends DataCollector implements FormDataCollectorInterf
8081
* @var ClonerInterface
8182
*/
8283
private $cloner;
83-
private $clonerCache = array();
84+
85+
private $hasVarDumper;
8486

8587
public function __construct(FormDataExtractorInterface $dataExtractor)
8688
{
@@ -90,6 +92,7 @@ public function __construct(FormDataExtractorInterface $dataExtractor)
9092
'forms_by_hash' => array(),
9193
'nb_errors' => 0,
9294
);
95+
$this->hasVarDumper = class_exists(ClassStub::class);
9396
}
9497

9598
/**
@@ -238,38 +241,15 @@ public function getData()
238241

239242
public function serialize()
240243
{
241-
$cloneVar = array($this, 'cloneVar');
242-
243-
foreach ($this->data['forms_by_hash'] as &$form) {
244-
foreach ($form as $k => $v) {
245-
switch ($k) {
246-
case 'type_class':
247-
$form[$k] = $cloneVar($v, true);
248-
break;
249-
case 'synchronized':
250-
$form[$k] = $cloneVar($v);
251-
break;
252-
case 'view_vars':
253-
case 'passed_options':
254-
case 'resolved_options':
255-
case 'default_data':
256-
case 'submitted_data':
257-
if ($v && is_array($v)) {
258-
$form[$k] = array_map($cloneVar, $v);
259-
}
260-
break;
261-
case 'errors':
262-
foreach ($v as $i => $e) {
263-
if (!empty($e['trace'])) {
264-
$form['errors'][$i]['trace'] = array_map($cloneVar, $e['trace']);
265-
}
266-
}
267-
break;
244+
if ($this->hasVarDumper) {
245+
foreach ($this->data['forms_by_hash'] as &$form) {
246+
if (isset($form['type_class']) && !$form['type_class'] instanceof ClassStub) {
247+
$form['type_class'] = new ClassStub($form['type_class']);
268248
}
269249
}
270250
}
271251

272-
return serialize($this->data);
252+
return serialize($this->cloneVar($this->data));
273253
}
274254

275255
/**
@@ -281,14 +261,15 @@ protected function cloneVar($var, $isClass = false)
281261
return $var;
282262
}
283263
if (null === $this->cloner) {
284-
if (class_exists(ClassStub::class)) {
264+
if ($this->hasVarDumper) {
285265
$this->cloner = new VarCloner();
286-
$this->cloner->setMaxItems(25);
266+
$this->cloner->setMaxItems(-1);
287267
$this->cloner->addCasters(array(
288268
'*' => function ($v, array $a, Stub $s, $isNested) {
289-
if ($isNested && !$v instanceof \DateTimeInterface) {
290-
$s->cut = -1;
291-
$a = array();
269+
foreach ($a as &$v) {
270+
if (is_object($v) && !$v instanceof \DateTimeInterface) {
271+
$v = new CutStub($v);
272+
}
292273
}
293274

294275
return $a;
@@ -320,34 +301,15 @@ protected function cloneVar($var, $isClass = false)
320301
$this->cloner = false;
321302
}
322303
}
323-
if (false === $this->cloner) {
324-
if (null === $this->valueExporter) {
325-
$this->valueExporter = new ValueExporter();
326-
}
327-
328-
return $this->valueExporter->exportValue($var);
329-
}
330-
if (null === $var) {
331-
$type = $hash = 'null';
332-
} elseif (array() === $var) {
333-
$type = $hash = 'array';
334-
} elseif ('object' === $type = gettype($var)) {
335-
$hash = spl_object_hash($var);
336-
} elseif ('double' === $type) {
337-
$hash = (string) $var;
338-
} elseif ('integer' === $type || 'string' === $type) {
339-
$hash = $var;
340-
} else {
341-
$type = null;
304+
if (false !== $this->cloner) {
305+
return $this->cloner->cloneVar($var, Caster::EXCLUDE_VERBOSE);
342306
}
343-
if (null !== $type && null !== $cache = &$this->clonerCache[$type][$hash]) {
344-
return $cache;
345-
}
346-
if ($isClass) {
347-
return $cache = $this->cloner->cloneVar(array(new ClassStub($var)))->seek(0);
307+
308+
if (null === $this->valueExporter) {
309+
$this->valueExporter = new ValueExporter();
348310
}
349311

350-
return $cache = $this->cloner->cloneVar($var);
312+
return $this->valueExporter->exportValue($var);
351313
}
352314

353315
private function &recursiveBuildPreliminaryFormTree(FormInterface $form, array &$outputByHash)

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
"symfony/http-kernel": "~2.8|~3.0",
3333
"symfony/security-csrf": "~2.8|~3.0",
3434
"symfony/translation": "~2.8|~3.0",
35-
"symfony/var-dumper": "~3.2"
35+
"symfony/var-dumper": "~3.3"
3636
},
3737
"conflict": {
3838
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
3939
"symfony/doctrine-bridge": "<2.7",
4040
"symfony/framework-bundle": "<2.7",
41-
"symfony/twig-bridge": "<2.7"
41+
"symfony/twig-bridge": "<2.7",
42+
"symfony/var-dumper": "~3.3"
4243
},
4344
"suggest": {
4445
"symfony/validator": "For form validation.",

0 commit comments

Comments
 (0)