-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel][VarDumper] Truncate profiler data & optim perf #23465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,10 @@ | |
namespace Symfony\Component\HttpKernel\DataCollector; | ||
|
||
use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter; | ||
use Symfony\Component\VarDumper\Caster\ClassStub; | ||
use Symfony\Component\VarDumper\Caster\CutStub; | ||
use Symfony\Component\VarDumper\Cloner\ClonerInterface; | ||
use Symfony\Component\VarDumper\Cloner\Data; | ||
use Symfony\Component\VarDumper\Cloner\Stub; | ||
use Symfony\Component\VarDumper\Cloner\VarCloner; | ||
|
||
/** | ||
|
@@ -37,7 +38,7 @@ abstract class DataCollector implements DataCollectorInterface, \Serializable | |
/** | ||
* @var ClonerInterface | ||
*/ | ||
private static $cloner; | ||
private $cloner; | ||
|
||
public function serialize() | ||
{ | ||
|
@@ -61,24 +62,28 @@ public function unserialize($data) | |
*/ | ||
protected function cloneVar($var) | ||
{ | ||
if (null === self::$cloner) { | ||
if (class_exists(ClassStub::class)) { | ||
self::$cloner = new VarCloner(); | ||
self::$cloner->setMaxItems(-1); | ||
if ($var instanceof Data) { | ||
return $var; | ||
} | ||
if (null === $this->cloner) { | ||
if (class_exists(CutStub::class)) { | ||
$this->cloner = new VarCloner(); | ||
$this->cloner->setMaxItems(-1); | ||
$this->cloner->addCasters(self::getCasters()); | ||
} else { | ||
@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); | ||
self::$cloner = false; | ||
$this->cloner = false; | ||
} | ||
} | ||
if (false === self::$cloner) { | ||
if (false === $this->cloner) { | ||
if (null === $this->valueExporter) { | ||
$this->valueExporter = new ValueExporter(); | ||
} | ||
|
||
return $this->valueExporter->exportValue($var); | ||
} | ||
|
||
return self::$cloner->cloneVar($var); | ||
return $this->cloner->cloneVar($var); | ||
} | ||
|
||
/** | ||
|
@@ -100,4 +105,24 @@ protected function varToString($var) | |
|
||
return $this->valueExporter->exportValue($var); | ||
} | ||
|
||
/** | ||
* @return callable[] The casters to add to the cloner | ||
*/ | ||
protected function getCasters() | ||
{ | ||
return array( | ||
'*' => function ($v, array $a, Stub $s, $isNested) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be the main fix: nested objects are truncated, which prevent deep recursion into nested structures by default (eg "object" of the security decision log, requests attributes, etc.) |
||
if (!$v instanceof Stub) { | ||
foreach ($a as $k => $v) { | ||
if (is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) { | ||
$a[$k] = new CutStub($v); | ||
} | ||
} | ||
} | ||
|
||
return $a; | ||
}, | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,11 +65,20 @@ public static function castObject($obj, $class, $hasDebugInfo = false) | |
} | ||
|
||
if ($a) { | ||
static $publicProperties = array(); | ||
|
||
$i = 0; | ||
$prefixedKeys = array(); | ||
foreach ($a as $k => $v) { | ||
if (isset($k[0]) && "\0" !== $k[0] && !property_exists($class, $k)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as shown by profiles in linked issue, this is a hot code path and calling |
||
$prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k; | ||
if (isset($k[0]) && "\0" !== $k[0]) { | ||
if (!isset($publicProperties[$class])) { | ||
foreach (get_class_vars($class) as $prop => $v) { | ||
$publicProperties[$class][$prop] = true; | ||
} | ||
} | ||
if (!isset($publicProperties[$class][$k])) { | ||
$prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k; | ||
} | ||
} elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) { | ||
$prefixedKeys[$i] = "\0".get_parent_class($class).'@anonymous'.strrchr($k, "\0"); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add this caster in the new Validator panel in 3.4 too ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's for you @ogizanagi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll check this once this is merged on 3.4.