Skip to content

Commit 0b3ee2d

Browse files
[HttpKernel][VarDumper] Truncate profiler data & optim perf
1 parent c3ec5c5 commit 0b3ee2d

File tree

6 files changed

+78
-86
lines changed

6 files changed

+78
-86
lines changed

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ public function collect(Request $request, Response $response, \Exception $except
127127
if ($this->accessDecisionManager instanceof TraceableAccessDecisionManager) {
128128
$this->data['access_decision_log'] = $this->accessDecisionManager->getDecisionLog();
129129
$this->data['voter_strategy'] = $this->accessDecisionManager->getStrategy();
130-
131-
foreach ($this->accessDecisionManager->getVoters() as $voter) {
132-
$this->data['voters'][] = $this->hasVarDumper ? new ClassStub(get_class($voter)) : get_class($voter);
133-
}
130+
$this->data['voters'] = $this->accessDecisionManager->getVoters();
134131
} else {
135132
$this->data['access_decision_log'] = array();
136133
$this->data['voter_strategy'] = 'unknown';

src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php

+25-62
Original file line numberDiff line numberDiff line change
@@ -20,11 +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;
24-
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
25-
use Symfony\Component\VarDumper\Cloner\Data;
2623
use Symfony\Component\VarDumper\Cloner\Stub;
27-
use Symfony\Component\VarDumper\Cloner\VarCloner;
2824

2925
/**
3026
* Data collector for {@link FormInterface} instances.
@@ -77,11 +73,6 @@ class FormDataCollector extends DataCollector implements FormDataCollectorInterf
7773
*/
7874
private $valueExporter;
7975

80-
/**
81-
* @var ClonerInterface
82-
*/
83-
private $cloner;
84-
8576
private $hasVarDumper;
8677

8778
public function __construct(FormDataExtractorInterface $dataExtractor)
@@ -255,61 +246,33 @@ public function serialize()
255246
/**
256247
* {@inheritdoc}
257248
*/
258-
protected function cloneVar($var, $isClass = false)
249+
protected function getCasters()
259250
{
260-
if ($var instanceof Data) {
261-
return $var;
262-
}
263-
if (null === $this->cloner) {
264-
if ($this->hasVarDumper) {
265-
$this->cloner = new VarCloner();
266-
$this->cloner->setMaxItems(-1);
267-
$this->cloner->addCasters(array(
268-
'*' => function ($v, array $a, Stub $s, $isNested) {
269-
foreach ($a as &$v) {
270-
if (is_object($v) && !$v instanceof \DateTimeInterface) {
271-
$v = new CutStub($v);
272-
}
273-
}
274-
275-
return $a;
276-
},
277-
\Exception::class => function (\Exception $e, array $a, Stub $s) {
278-
if (isset($a[$k = "\0Exception\0previous"])) {
279-
unset($a[$k]);
280-
++$s->cut;
281-
}
282-
283-
return $a;
284-
},
285-
FormInterface::class => function (FormInterface $f, array $a) {
286-
return array(
287-
Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
288-
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(get_class($f->getConfig()->getType()->getInnerType())),
289-
);
290-
},
291-
ConstraintViolationInterface::class => function (ConstraintViolationInterface $v, array $a) {
292-
return array(
293-
Caster::PREFIX_VIRTUAL.'root' => $v->getRoot(),
294-
Caster::PREFIX_VIRTUAL.'path' => $v->getPropertyPath(),
295-
Caster::PREFIX_VIRTUAL.'value' => $v->getInvalidValue(),
296-
);
297-
},
298-
));
299-
} else {
300-
@trigger_error(sprintf('Using the %s() method without the VarDumper component is deprecated since version 3.2 and won\'t be supported in 4.0. Install symfony/var-dumper version 3.2 or above.', __METHOD__), E_USER_DEPRECATED);
301-
$this->cloner = false;
302-
}
303-
}
304-
if (false !== $this->cloner) {
305-
return $this->cloner->cloneVar($var, Caster::EXCLUDE_VERBOSE);
306-
}
307-
308-
if (null === $this->valueExporter) {
309-
$this->valueExporter = new ValueExporter();
310-
}
251+
return parent::getCasters() + array(
252+
\Exception::class => function (\Exception $e, array $a, Stub $s) {
253+
foreach (array("\0Exception\0previous", "\0Exception\0trace") as $k) {
254+
if (isset($a[$k])) {
255+
unset($a[$k]);
256+
++$s->cut;
257+
}
258+
}
311259

312-
return $this->valueExporter->exportValue($var);
260+
return $a;
261+
},
262+
FormInterface::class => function (FormInterface $f, array $a) {
263+
return array(
264+
Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
265+
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(get_class($f->getConfig()->getType()->getInnerType())),
266+
);
267+
},
268+
ConstraintViolationInterface::class => function (ConstraintViolationInterface $v, array $a) {
269+
return array(
270+
Caster::PREFIX_VIRTUAL.'root' => $v->getRoot(),
271+
Caster::PREFIX_VIRTUAL.'path' => $v->getPropertyPath(),
272+
Caster::PREFIX_VIRTUAL.'value' => $v->getInvalidValue(),
273+
);
274+
},
275+
);
313276
}
314277

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

src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php

+34-9
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
namespace Symfony\Component\HttpKernel\DataCollector;
1313

1414
use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
15-
use Symfony\Component\VarDumper\Caster\ClassStub;
15+
use Symfony\Component\VarDumper\Caster\CutStub;
1616
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
1717
use Symfony\Component\VarDumper\Cloner\Data;
18+
use Symfony\Component\VarDumper\Cloner\Stub;
1819
use Symfony\Component\VarDumper\Cloner\VarCloner;
1920

2021
/**
@@ -37,7 +38,7 @@ abstract class DataCollector implements DataCollectorInterface, \Serializable
3738
/**
3839
* @var ClonerInterface
3940
*/
40-
private static $cloner;
41+
private $cloner;
4142

4243
public function serialize()
4344
{
@@ -61,24 +62,28 @@ public function unserialize($data)
6162
*/
6263
protected function cloneVar($var)
6364
{
64-
if (null === self::$cloner) {
65-
if (class_exists(ClassStub::class)) {
66-
self::$cloner = new VarCloner();
67-
self::$cloner->setMaxItems(-1);
65+
if ($var instanceof Data) {
66+
return $var;
67+
}
68+
if (null === $this->cloner) {
69+
if (class_exists(CutStub::class)) {
70+
$this->cloner = new VarCloner();
71+
$this->cloner->setMaxItems(-1);
72+
$this->cloner->addCasters(self::getCasters());
6873
} else {
6974
@trigger_error(sprintf('Using the %s() method without the VarDumper component is deprecated since version 3.2 and won\'t be supported in 4.0. Install symfony/var-dumper version 3.2 or above.', __METHOD__), E_USER_DEPRECATED);
70-
self::$cloner = false;
75+
$this->cloner = false;
7176
}
7277
}
73-
if (false === self::$cloner) {
78+
if (false === $this->cloner) {
7479
if (null === $this->valueExporter) {
7580
$this->valueExporter = new ValueExporter();
7681
}
7782

7883
return $this->valueExporter->exportValue($var);
7984
}
8085

81-
return self::$cloner->cloneVar($var);
86+
return $this->cloner->cloneVar($var);
8287
}
8388

8489
/**
@@ -100,4 +105,24 @@ protected function varToString($var)
100105

101106
return $this->valueExporter->exportValue($var);
102107
}
108+
109+
/**
110+
* @return callable[] The casters to add to the cloner
111+
*/
112+
protected function getCasters()
113+
{
114+
return array(
115+
'*' => function ($v, array $a, Stub $s, $isNested) {
116+
if (!$v instanceof Stub) {
117+
foreach ($a as &$v) {
118+
if (is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
119+
$v = new CutStub($v);
120+
}
121+
}
122+
}
123+
124+
return $a;
125+
},
126+
);
127+
}
103128
}

src/Symfony/Component/VarDumper/Caster/Caster.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,20 @@ public static function castObject($obj, $class, $hasDebugInfo = false)
6565
}
6666

6767
if ($a) {
68+
static $publicProperties = array();
69+
6870
$i = 0;
6971
$prefixedKeys = array();
7072
foreach ($a as $k => $v) {
71-
if (isset($k[0]) && "\0" !== $k[0] && !property_exists($class, $k)) {
72-
$prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k;
73+
if (isset($k[0]) && "\0" !== $k[0]) {
74+
if (!isset($publicProperties[$class])) {
75+
foreach (get_class_vars($class) as $prop => $v) {
76+
$publicProperties[$class][$prop] = true;
77+
}
78+
}
79+
if (!isset($publicProperties[$class][$k])) {
80+
$prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k;
81+
}
7382
} elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) {
7483
$prefixedKeys[$i] = "\0".get_parent_class($class).'@anonymous'.strrchr($k, "\0");
7584
}

src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static function castSilencedErrorContext(SilencedErrorContext $e, array $
102102
}
103103

104104
unset($a[$sPrefix.'file'], $a[$sPrefix.'line'], $a[$sPrefix.'trace']);
105-
$a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace);
105+
$a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
106106

107107
return $a;
108108
}
@@ -256,7 +256,7 @@ private static function filterExceptionArray($xClass, array $a, $xPrefix, $filte
256256
$trace = array();
257257
}
258258

259-
if (!($filter & Caster::EXCLUDE_VERBOSE)) {
259+
if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) {
260260
if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
261261
self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
262262
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,12 @@ public function cloneVar($var, $filter = 0)
210210
$this->filter = $filter;
211211

212212
try {
213+
gc_disable();
213214
$data = $this->doClone($var);
214-
} catch (\Exception $e) {
215-
}
216-
restore_error_handler();
217-
$this->prevErrorHandler = null;
218-
219-
if (isset($e)) {
220-
throw $e;
215+
} finally {
216+
gc_enable();
217+
restore_error_handler();
218+
$this->prevErrorHandler = null;
221219
}
222220

223221
return new Data($data);

0 commit comments

Comments
 (0)