Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[VarDumper] Add and use Caster::PREFIX_* consts #14079

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

Merged
merged 1 commit into from
Mar 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Symfony/Component/VarDumper/Caster/AmqpCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AmqpCaster

public static function castConnection(\AMQPConnection $c, array $a, Stub $stub, $isNested)
{
$prefix = "\0~\0";
$prefix = Caster::PREFIX_VIRTUAL;

// BC layer in the ampq lib
if (method_exists($c, 'getReadTimeout')) {
Expand All @@ -70,7 +70,7 @@ public static function castConnection(\AMQPConnection $c, array $a, Stub $stub,

public static function castChannel(\AMQPChannel $c, array $a, Stub $stub, $isNested)
{
$prefix = "\0~\0";
$prefix = Caster::PREFIX_VIRTUAL;

$a += array(
$prefix.'isConnected' => $c->isConnected(),
Expand All @@ -85,7 +85,7 @@ public static function castChannel(\AMQPChannel $c, array $a, Stub $stub, $isNes

public static function castQueue(\AMQPQueue $c, array $a, Stub $stub, $isNested)
{
$prefix = "\0~\0";
$prefix = Caster::PREFIX_VIRTUAL;

$a += array(
$prefix.'name' => $c->getName(),
Expand All @@ -100,7 +100,7 @@ public static function castQueue(\AMQPQueue $c, array $a, Stub $stub, $isNested)

public static function castExchange(\AMQPExchange $c, array $a, Stub $stub, $isNested)
{
$prefix = "\0~\0";
$prefix = Caster::PREFIX_VIRTUAL;

$a += array(
$prefix.'name' => $c->getName(),
Expand All @@ -114,12 +114,15 @@ public static function castExchange(\AMQPExchange $c, array $a, Stub $stub, $isN
return $a;
}

public static function castEnvelope(\AMQPEnvelope $c, array $a, Stub $stub, $isNested)
public static function castEnvelope(\AMQPEnvelope $c, array $a, Stub $stub, $isNested, $filter = 0)
{
$prefix = "\0~\0";
$prefix = Caster::PREFIX_VIRTUAL;

if (!($filter & Caster::EXCLUDE_VERBOSE)) {
$a += array($prefix.'body' => $c->getBody());
}

$a += array(
$prefix.'body' => $c->getBody(),
$prefix.'routingKey' => $c->getRoutingKey(),
$prefix.'deliveryTag' => $c->getDeliveryTag(),
$prefix.'deliveryMode' => new ConstStub($c->getDeliveryMode().(2 === $c->getDeliveryMode() ? ' (persistent)' : ' (non-persistent)'), $c->getDeliveryMode()),
Expand Down
33 changes: 33 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/Caster.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ class Caster
const EXCLUDE_NOT_IMPORTANT = 256;
const EXCLUDE_STRICT = 512;

const PREFIX_VIRTUAL = "\0~\0";
const PREFIX_DYNAMIC = "\0+\0";
const PREFIX_PROTECTED = "\0*\0";

/**
* Casts objects to arrays and adds the dynamic property prefix.
*
* @param object $obj The object to cast.
* @param ReflectionClass $reflector The class reflector to use for inspecting the object definition.
*
* @return array The array-cast of the object, with prefixed dynamic properties.
*/
public static function castObject($obj, \ReflectionClass $reflector)
{
if ($reflector->hasMethod('__debugInfo')) {
$a = $obj->__debugInfo();
} else {
$a = (array) $obj;
}

if ($a) {
$p = array_keys($a);
foreach ($p as $i => $k) {
if (!isset($k[0]) || ("\0" !== $k[0] && !$reflector->hasProperty($k))) {
$p[$i] = self::PREFIX_DYNAMIC.$k;
}
}
$a = array_combine($p, $a);
}

return $a;
}

/**
* Filters out the specified properties.
*
Expand Down
24 changes: 13 additions & 11 deletions src/Symfony/Component/VarDumper/Caster/DOMCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ class DOMCaster

public static function castException(\DOMException $e, array $a, Stub $stub, $isNested)
{
if (isset($a["\0*\0code"], self::$errorCodes[$a["\0*\0code"]])) {
$a["\0*\0code"] = new ConstStub(self::$errorCodes[$a["\0*\0code"]], $a["\0*\0code"]);
$k = Caster::PREFIX_PROTECTED.'code';
if (isset($a[$k], self::$errorCodes[$a[$k]])) {
$a[$k] = new ConstStub(self::$errorCodes[$a[$k]], $a[$k]);
}

return $a;
Expand All @@ -82,8 +83,8 @@ public static function castLength($dom, array $a, Stub $stub, $isNested)
public static function castImplementation($dom, array $a, Stub $stub, $isNested)
{
$a += array(
"\0~\0Core" => '1.0',
"\0~\0XML" => '2.0',
Caster::PREFIX_VIRTUAL.'Core' => '1.0',
Caster::PREFIX_VIRTUAL.'XML' => '2.0',
);

return $a;
Expand Down Expand Up @@ -129,11 +130,8 @@ public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, Stub
return $a;
}

public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $isNested)
public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $isNested, $filter = 0)
{
$formatOutput = $dom->formatOutput;
$dom->formatOutput = true;

$a += array(
'doctype' => $dom->doctype,
'implementation' => $dom->implementation,
Expand All @@ -148,16 +146,20 @@ public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $is
'strictErrorChecking' => $dom->strictErrorChecking,
'documentURI' => $dom->documentURI,
'config' => $dom->config,
'formatOutput' => $formatOutput,
'formatOutput' => $dom->formatOutput,
'validateOnParse' => $dom->validateOnParse,
'resolveExternals' => $dom->resolveExternals,
'preserveWhiteSpace' => $dom->preserveWhiteSpace,
'recover' => $dom->recover,
'substituteEntities' => $dom->substituteEntities,
"\0~\0xml" => $dom->saveXML(),
);

$dom->formatOutput = $formatOutput;
if (!($filter & Caster::EXCLUDE_VERBOSE)) {
$formatOutput = $dom->formatOutput;
$dom->formatOutput = true;
$a += array(Caster::PREFIX_VIRTUAL.'xml' => $dom->saveXML());
$dom->formatOutput = $formatOutput;
}

return $a;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Component/VarDumper/Caster/MongoCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ class MongoCaster
{
public static function castCursor(\MongoCursorInterface $cursor, array $a, Stub $stub, $isNested)
{
$prefix = "\0~\0";

if ($info = $cursor->info()) {
foreach ($info as $k => $v) {
$a[$prefix.$k] = $v;
$a[Caster::PREFIX_VIRTUAL.$k] = $v;
}
}
$a[$prefix.'dead'] = $cursor->dead();
$a[Caster::PREFIX_VIRTUAL.'dead'] = $cursor->dead();

return $a;
}
Expand Down
26 changes: 13 additions & 13 deletions src/Symfony/Component/VarDumper/Caster/PdoCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,21 @@ public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
}
}

$m = "\0~\0";
$prefix = Caster::PREFIX_VIRTUAL;
$a += array(
$m.'inTransaction' => method_exists($c, 'inTransaction'),
$m.'errorInfo' => $c->errorInfo(),
$m.'attributes' => $attr,
$prefix.'inTransaction' => method_exists($c, 'inTransaction'),
$prefix.'errorInfo' => $c->errorInfo(),
$prefix.'attributes' => $attr,
);

if ($a[$m.'inTransaction']) {
$a[$m.'inTransaction'] = $c->inTransaction();
if ($a[$prefix.'inTransaction']) {
$a[$prefix.'inTransaction'] = $c->inTransaction();
} else {
unset($a[$m.'inTransaction']);
unset($a[$prefix.'inTransaction']);
}

if (!isset($a[$m.'errorInfo'][1], $a[$m.'errorInfo'][2])) {
unset($a[$m.'errorInfo']);
if (!isset($a[$prefix.'errorInfo'][1], $a[$prefix.'errorInfo'][2])) {
unset($a[$prefix.'errorInfo']);
}

$c->setAttribute(\PDO::ATTR_ERRMODE, $errmode);
Expand All @@ -102,11 +102,11 @@ public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)

public static function castPdoStatement(\PDOStatement $c, array $a, Stub $stub, $isNested)
{
$m = "\0~\0";
$a[$m.'errorInfo'] = $c->errorInfo();
$prefix = Caster::PREFIX_VIRTUAL;
$a[$prefix.'errorInfo'] = $c->errorInfo();

if (!isset($a[$m.'errorInfo'][1], $a[$m.'errorInfo'][2])) {
unset($a[$m.'errorInfo']);
if (!isset($a[$prefix.'errorInfo'][1], $a[$prefix.'errorInfo'][2])) {
unset($a[$prefix.'errorInfo']);
}

return $a;
Expand Down
34 changes: 13 additions & 21 deletions src/Symfony/Component/VarDumper/Caster/SplCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,23 @@ class SplCaster
{
public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;
$class = $stub->class;
$flags = $c->getFlags();

$b = array(
"\0~\0flag::STD_PROP_LIST" => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
"\0~\0flag::ARRAY_AS_PROPS" => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
"\0~\0iteratorClass" => $c->getIteratorClass(),
"\0~\0storage" => $c->getArrayCopy(),
$prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
$prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
$prefix.'iteratorClass' => $c->getIteratorClass(),
$prefix.'storage' => $c->getArrayCopy(),
);

if ($class === 'ArrayObject') {
$a = $b;
} else {
if (!($flags & \ArrayObject::STD_PROP_LIST)) {
$c->setFlags(\ArrayObject::STD_PROP_LIST);

if ($a = (array) $c) {
$class = new \ReflectionClass($class);
foreach ($a as $k => $p) {
if (!isset($k[0]) || ("\0" !== $k[0] && !$class->hasProperty($k))) {
unset($a[$k]);
$a["\0+\0".$k] = $p;
}
}
}

$a = Caster::castObject($c, new \ReflectionClass($class));
$c->setFlags($flags);
}

Expand All @@ -60,20 +51,21 @@ public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $i
public static function castHeap(\Iterator $c, array $a, Stub $stub, $isNested)
{
$a += array(
"\0~\0heap" => iterator_to_array(clone $c),
Caster::PREFIX_VIRTUAL.'heap' => iterator_to_array(clone $c),
);

return $a;
}

public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;
$mode = $c->getIteratorMode();
$c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE);

$a += array(
"\0~\0mode" => new ConstStub((($mode & \SplDoublyLinkedList::IT_MODE_LIFO) ? 'IT_MODE_LIFO' : 'IT_MODE_FIFO').' | '.(($mode & \SplDoublyLinkedList::IT_MODE_KEEP) ? 'IT_MODE_KEEP' : 'IT_MODE_DELETE'), $mode),
"\0~\0dllist" => iterator_to_array($c),
$prefix.'mode' => new ConstStub((($mode & \SplDoublyLinkedList::IT_MODE_LIFO) ? 'IT_MODE_LIFO' : 'IT_MODE_FIFO').' | '.(($mode & \SplDoublyLinkedList::IT_MODE_KEEP) ? 'IT_MODE_KEEP' : 'IT_MODE_DELETE'), $mode),
$prefix.'dllist' => iterator_to_array($c),
);
$c->setIteratorMode($mode);

Expand All @@ -83,7 +75,7 @@ public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, S
public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
{
$a += array(
"\0~\0storage" => $c->toArray(),
Caster::PREFIX_VIRTUAL.'storage' => $c->toArray(),
);

return $a;
Expand All @@ -92,7 +84,7 @@ public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $
public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, $isNested)
{
$storage = array();
unset($a["\0+\0\0gcdata"]); // Don't hit https://bugs.php.net/65967
unset($a[Caster::PREFIX_DYNAMIC."\0gcdata"]); // Don't hit https://bugs.php.net/65967

foreach ($c as $obj) {
$storage[spl_object_hash($obj)] = array(
Expand All @@ -102,7 +94,7 @@ public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $s
}

$a += array(
"\0~\0storage" => $storage,
Caster::PREFIX_VIRTUAL.'storage' => $storage,
);

return $a;
Expand Down
22 changes: 4 additions & 18 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\VarDumper\Cloner;

use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Exception\ThrowingCasterException;

/**
Expand Down Expand Up @@ -206,31 +207,16 @@ protected function castObject(Stub $stub, $isNested)
} else {
$classInfo = array(
$class,
method_exists($class, '__debugInfo'),
new \ReflectionClass($class),
array_reverse(array('*' => '*', $class => $class) + class_parents($class) + class_implements($class)),
);

$this->classInfo[$class] = $classInfo;
}

if ($classInfo[1]) {
$a = $this->callCaster(function ($obj) {return $obj->__debugInfo();}, $obj, array(), null, $isNested);
} else {
$a = (array) $obj;
}

if ($a) {
$p = array_keys($a);
foreach ($p as $i => $k) {
if (!isset($k[0]) || ("\0" !== $k[0] && !$classInfo[2]->hasProperty($k))) {
$p[$i] = "\0+\0".$k;
}
}
$a = array_combine($p, $a);
}
$a = $this->callCaster('Symfony\Component\VarDumper\Caster\Caster::castObject', $obj, $classInfo[1], null, $isNested);

foreach ($classInfo[3] as $p) {
foreach ($classInfo[2] as $p) {
if (!empty($this->casters[$p = strtolower($p)])) {
foreach ($this->casters[$p] as $p) {
$a = $this->callCaster($p, $obj, $a, $stub, $isNested);
Expand Down Expand Up @@ -284,7 +270,7 @@ private function callCaster($callback, $obj, $a, $stub, $isNested)
$a = $cast;
}
} catch (\Exception $e) {
$a[(Stub::TYPE_OBJECT === $stub->type ? "\0~\0" : '').'⚠'] = new ThrowingCasterException($callback, $e);
$a[(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠'] = new ThrowingCasterException($callback, $e);
}

return $a;
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/VarDumper/Test/VarDumperTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ abstract class VarDumperTestCase extends \PHPUnit_Framework_TestCase
{
public function assertDumpEquals($dump, $data, $message = '')
{
$this->assertSame($dump, $this->getVarDumperDump($data), $message);
$this->assertSame(rtrim($dump), $this->getVarDumperDump($data), $message);
}

public function assertDumpMatchesFormat($dump, $data, $message = '')
{
$this->assertStringMatchesFormat($dump, $this->getVarDumperDump($data), $message);
$this->assertStringMatchesFormat(rtrim($dump), $this->getVarDumperDump($data), $message);
}

private function getVarDumperDump($data)
Expand All @@ -40,6 +40,6 @@ private function getVarDumperDump($data)
$data = stream_get_contents($h);
fclose($h);

return $data;
return rtrim($data);
}
}
Loading