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

Skip to content

Commit 3281487

Browse files
[VarDumper] Make use of Caster::PREFIX_* consts
1 parent 8bee26f commit 3281487

File tree

7 files changed

+207
-118
lines changed

7 files changed

+207
-118
lines changed

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

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AmqpCaster
4646

4747
public static function castConnection(\AMQPConnection $c, array $a, Stub $stub, $isNested)
4848
{
49-
$prefix = "\0~\0";
49+
$p = Caster::PREFIX_VIRTUAL;
5050

5151
// BC layer in the ampq lib
5252
if (method_exists($c, 'getReadTimeout')) {
@@ -56,87 +56,90 @@ public static function castConnection(\AMQPConnection $c, array $a, Stub $stub,
5656
}
5757

5858
$a += array(
59-
$prefix.'isConnected' => $c->isConnected(),
60-
$prefix.'login' => $c->getLogin(),
61-
$prefix.'password' => $c->getPassword(),
62-
$prefix.'host' => $c->getHost(),
63-
$prefix.'port' => $c->getPort(),
64-
$prefix.'vhost' => $c->getVhost(),
65-
$prefix.'readTimeout' => $timeout,
59+
$p.'isConnected' => $c->isConnected(),
60+
$p.'login' => $c->getLogin(),
61+
$p.'password' => $c->getPassword(),
62+
$p.'host' => $c->getHost(),
63+
$p.'port' => $c->getPort(),
64+
$p.'vhost' => $c->getVhost(),
65+
$p.'readTimeout' => $timeout,
6666
);
6767

6868
return $a;
6969
}
7070

7171
public static function castChannel(\AMQPChannel $c, array $a, Stub $stub, $isNested)
7272
{
73-
$prefix = "\0~\0";
73+
$p = Caster::PREFIX_VIRTUAL;
7474

7575
$a += array(
76-
$prefix.'isConnected' => $c->isConnected(),
77-
$prefix.'channelId' => $c->getChannelId(),
78-
$prefix.'prefetchSize' => $c->getPrefetchSize(),
79-
$prefix.'prefetchCount' => $c->getPrefetchCount(),
80-
$prefix.'connection' => $c->getConnection(),
76+
$p.'isConnected' => $c->isConnected(),
77+
$p.'channelId' => $c->getChannelId(),
78+
$p.'prefetchSize' => $c->getPrefetchSize(),
79+
$p.'prefetchCount' => $c->getPrefetchCount(),
80+
$p.'connection' => $c->getConnection(),
8181
);
8282

8383
return $a;
8484
}
8585

8686
public static function castQueue(\AMQPQueue $c, array $a, Stub $stub, $isNested)
8787
{
88-
$prefix = "\0~\0";
88+
$p = Caster::PREFIX_VIRTUAL;
8989

9090
$a += array(
91-
$prefix.'name' => $c->getName(),
92-
$prefix.'flags' => self::extractFlags($c->getFlags()),
93-
$prefix.'arguments' => $c->getArguments(),
94-
$prefix.'connection' => $c->getConnection(),
95-
$prefix.'channel' => $c->getChannel(),
91+
$p.'name' => $c->getName(),
92+
$p.'flags' => self::extractFlags($c->getFlags()),
93+
$p.'arguments' => $c->getArguments(),
94+
$p.'connection' => $c->getConnection(),
95+
$p.'channel' => $c->getChannel(),
9696
);
9797

9898
return $a;
9999
}
100100

101101
public static function castExchange(\AMQPExchange $c, array $a, Stub $stub, $isNested)
102102
{
103-
$prefix = "\0~\0";
103+
$p = Caster::PREFIX_VIRTUAL;
104104

105105
$a += array(
106-
$prefix.'name' => $c->getName(),
107-
$prefix.'flags' => self::extractFlags($c->getFlags()),
108-
$prefix.'type' => isset(self::$exchangeTypes[$c->getType()]) ? new ConstStub(self::$exchangeTypes[$c->getType()], $c->getType()) : $c->getType(),
109-
$prefix.'arguments' => $c->getArguments(),
110-
$prefix.'channel' => $c->getChannel(),
111-
$prefix.'connection' => $c->getConnection(),
106+
$p.'name' => $c->getName(),
107+
$p.'flags' => self::extractFlags($c->getFlags()),
108+
$p.'type' => isset(self::$exchangeTypes[$c->getType()]) ? new ConstStub(self::$exchangeTypes[$c->getType()], $c->getType()) : $c->getType(),
109+
$p.'arguments' => $c->getArguments(),
110+
$p.'channel' => $c->getChannel(),
111+
$p.'connection' => $c->getConnection(),
112112
);
113113

114114
return $a;
115115
}
116116

117-
public static function castEnvelope(\AMQPEnvelope $c, array $a, Stub $stub, $isNested)
117+
public static function castEnvelope(\AMQPEnvelope $c, array $a, Stub $stub, $isNested, $filter = 0)
118118
{
119-
$prefix = "\0~\0";
119+
$p = Caster::PREFIX_VIRTUAL;
120+
121+
if (!($filter & Caster::EXCLUDE_VERBOSE)) {
122+
$a += array($p.'body' => $c->getBody());
123+
}
120124

121125
$a += array(
122-
$prefix.'body' => $c->getBody(),
123-
$prefix.'routingKey' => $c->getRoutingKey(),
124-
$prefix.'deliveryTag' => $c->getDeliveryTag(),
125-
$prefix.'deliveryMode' => new ConstStub($c->getDeliveryMode().(2 === $c->getDeliveryMode() ? ' (persistent)' : ' (non-persistent)'), $c->getDeliveryMode()),
126-
$prefix.'exchangeName' => $c->getExchangeName(),
127-
$prefix.'isRedelivery' => $c->isRedelivery(),
128-
$prefix.'contentType' => $c->getContentType(),
129-
$prefix.'contentEncoding' => $c->getContentEncoding(),
130-
$prefix.'type' => $c->getType(),
131-
$prefix.'timestamp' => $c->getTimestamp(),
132-
$prefix.'priority' => $c->getPriority(),
133-
$prefix.'expiration' => $c->getExpiration(),
134-
$prefix.'userId' => $c->getUserId(),
135-
$prefix.'appId' => $c->getAppId(),
136-
$prefix.'messageId' => $c->getMessageId(),
137-
$prefix.'replyTo' => $c->getReplyTo(),
138-
$prefix.'correlationId' => $c->getCorrelationId(),
139-
$prefix.'headers' => $c->getHeaders(),
126+
$p.'routingKey' => $c->getRoutingKey(),
127+
$p.'deliveryTag' => $c->getDeliveryTag(),
128+
$p.'deliveryMode' => new ConstStub($c->getDeliveryMode().(2 === $c->getDeliveryMode() ? ' (persistent)' : ' (non-persistent)'), $c->getDeliveryMode()),
129+
$p.'exchangeName' => $c->getExchangeName(),
130+
$p.'isRedelivery' => $c->isRedelivery(),
131+
$p.'contentType' => $c->getContentType(),
132+
$p.'contentEncoding' => $c->getContentEncoding(),
133+
$p.'type' => $c->getType(),
134+
$p.'timestamp' => $c->getTimestamp(),
135+
$p.'priority' => $c->getPriority(),
136+
$p.'expiration' => $c->getExpiration(),
137+
$p.'userId' => $c->getUserId(),
138+
$p.'appId' => $c->getAppId(),
139+
$p.'messageId' => $c->getMessageId(),
140+
$p.'replyTo' => $c->getReplyTo(),
141+
$p.'correlationId' => $c->getCorrelationId(),
142+
$p.'headers' => $c->getHeaders(),
140143
);
141144

142145
return $a;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
/**
15+
* Helper for filtering out properties in casters.
16+
*
17+
* @author Nicolas Grekas <[email protected]>
18+
*/
19+
class Caster
20+
{
21+
const EXCLUDE_VERBOSE = 1;
22+
const EXCLUDE_VIRTUAL = 2;
23+
const EXCLUDE_DYNAMIC = 4;
24+
const EXCLUDE_PUBLIC = 8;
25+
const EXCLUDE_PROTECTED = 16;
26+
const EXCLUDE_PRIVATE = 32;
27+
const EXCLUDE_NULL = 64;
28+
const EXCLUDE_EMPTY = 128;
29+
const EXCLUDE_NOT_IMPORTANT = 256;
30+
31+
const PREFIX_VIRTUAL = "\0~\0";
32+
const PREFIX_DYNAMIC = "\0+\0";
33+
const PREFIX_PROTECTED = "\0*\0";
34+
35+
/**
36+
* Casts objects to arrays and adds the dynamic property prefix.
37+
*
38+
* @param object $obj The object to cast.
39+
* @param ReflectionClass $reflector The class reflector to use for inspecting the object definition.
40+
*
41+
* @return array The array-cast of the object, with prefixed dynamic properties.
42+
*/
43+
public static function castObject($obj, \ReflectionClass $reflector)
44+
{
45+
if ($reflector->hasMethod('__debugInfo')) {
46+
$v = $obj->__debugInfo();
47+
} else {
48+
$v = (array) $obj;
49+
}
50+
51+
$a = array();
52+
53+
foreach ($v as $k => $v) {
54+
if (!isset($k[0]) || ("\0" !== $k[0] && !$reflector->hasProperty($k))) {
55+
$a[self::PREFIX_DYNAMIC.$k] = $v;
56+
} else {
57+
$a[$k] = $v;
58+
}
59+
}
60+
61+
return $a;
62+
}
63+
64+
/**
65+
* Filters out the specified properties.
66+
*
67+
* @param array $a The array containing the properties to filter.
68+
* @param int $filter A bit field of Caster::EXCLUDE_* constants specifying which properties to filter out.
69+
* @param array $listedProperties List of properties to exclude when Caster::EXCLUDE_VERBOSE is set, and to preserve when Caster::EXCLUDE_NOT_IMPORTANT is set.
70+
*
71+
* @return array The filtered array
72+
*/
73+
public static function filter(array $a, $filter, array $listedProperties = array())
74+
{
75+
if (self::EXCLUDE_VERBOSE & $filter) {
76+
foreach ($listedProperties as $k) {
77+
unset($a[$k]);
78+
}
79+
}
80+
81+
foreach ($a as $k => $v) {
82+
$type = self::EXCLUDE_PUBLIC;
83+
84+
switch (true) {
85+
case (self::EXCLUDE_NULL & $filter) && null === $v:
86+
case (self::EXCLUDE_EMPTY & $filter) && empty($v):
87+
case (self::EXCLUDE_NOT_IMPORTANT & $filter) && !in_array($k, $listedProperties, true):
88+
$type = $filter;
89+
break;
90+
case isset($k[1]) && "\0" === $k[0]:
91+
switch ($k[1]) {
92+
case '~': $type = self::EXCLUDE_VIRTUAL; break;
93+
case '+': $type = self::EXCLUDE_DYNAMIC; break;
94+
case '*': $type = self::EXCLUDE_PROTECTED; break;
95+
default: $type = self::EXCLUDE_PRIVATE; break;
96+
}
97+
}
98+
99+
if ($type & $filter) {
100+
unset($a[$k]);
101+
}
102+
}
103+
104+
return $a;
105+
}
106+
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ class DOMCaster
6363

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

7071
return $a;
@@ -82,8 +83,8 @@ public static function castLength($dom, array $a, Stub $stub, $isNested)
8283
public static function castImplementation($dom, array $a, Stub $stub, $isNested)
8384
{
8485
$a += array(
85-
"\0~\0Core" => '1.0',
86-
"\0~\0XML" => '2.0',
86+
Caster::PREFIX_VIRTUAL.'Core' => '1.0',
87+
Caster::PREFIX_VIRTUAL.'XML' => '2.0',
8788
);
8889

8990
return $a;
@@ -129,7 +130,7 @@ public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, Stub
129130
return $a;
130131
}
131132

132-
public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $isNested)
133+
public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $isNested, $filter = 0)
133134
{
134135
$formatOutput = $dom->formatOutput;
135136
$dom->formatOutput = true;
@@ -154,8 +155,10 @@ public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $is
154155
'preserveWhiteSpace' => $dom->preserveWhiteSpace,
155156
'recover' => $dom->recover,
156157
'substituteEntities' => $dom->substituteEntities,
157-
"\0~\0xml" => $dom->saveXML(),
158158
);
159+
if (!($filter & Caster::EXCLUDE_VERBOSE)) {
160+
$a += array(Caster::PREFIX_VIRTUAL.'xml' => $dom->saveXML());
161+
}
159162

160163
$dom->formatOutput = $formatOutput;
161164

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ class MongoCaster
2222
{
2323
public static function castCursor(\MongoCursorInterface $cursor, array $a, Stub $stub, $isNested)
2424
{
25-
$prefix = "\0~\0";
26-
2725
if ($info = $cursor->info()) {
2826
foreach ($info as $k => $v) {
29-
$a[$prefix.$k] = $v;
27+
$a[Caster::PREFIX_VIRTUAL.$k] = $v;
3028
}
3129
}
32-
$a[$prefix.'dead'] = $cursor->dead();
30+
$a[Caster::PREFIX_VIRTUAL.'dead'] = $cursor->dead();
3331

3432
return $a;
3533
}

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,40 +59,40 @@ class PdoCaster
5959

6060
public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
6161
{
62-
$a = array();
62+
$attr = array();
6363
$errmode = $c->getAttribute(\PDO::ATTR_ERRMODE);
6464
$c->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
6565

66-
foreach (self::$pdoAttributes as $attr => $values) {
67-
if (!isset($attr[0])) {
68-
$attr = $values;
69-
$values = array();
66+
foreach (self::$pdoAttributes as $k => $v) {
67+
if (!isset($k[0])) {
68+
$k = $v;
69+
$v = array();
7070
}
7171

7272
try {
73-
$a[$attr] = 'ERRMODE' === $attr ? $errmode : $c->getAttribute(constant("PDO::ATTR_{$attr}"));
74-
if ($values && isset($values[$a[$attr]])) {
75-
$a[$attr] = new ConstStub($values[$a[$attr]], $a[$attr]);
73+
$attr[$k] = 'ERRMODE' === $k ? $errmode : $c->getAttribute(constant('PDO::ATTR_'.$k));
74+
if ($v && isset($v[$attr[$k]])) {
75+
$attr[$k] = new ConstStub($v[$attr[$k]], $attr[$k]);
7676
}
77-
} catch (\Exception $m) {
77+
} catch (\Exception $e) {
7878
}
7979
}
8080

81-
$m = "\0~\0";
82-
$a = (array) $c + array(
83-
$m.'inTransaction' => method_exists($c, 'inTransaction'),
84-
$m.'errorInfo' => $c->errorInfo(),
85-
$m.'attributes' => $a,
81+
$p = Caster::PREFIX_VIRTUAL;
82+
$a += array(
83+
$p.'inTransaction' => method_exists($c, 'inTransaction'),
84+
$p.'errorInfo' => $c->errorInfo(),
85+
$p.'attributes' => $attr,
8686
);
8787

88-
if ($a[$m.'inTransaction']) {
89-
$a[$m.'inTransaction'] = $c->inTransaction();
88+
if ($a[$p.'inTransaction']) {
89+
$a[$p.'inTransaction'] = $c->inTransaction();
9090
} else {
91-
unset($a[$m.'inTransaction']);
91+
unset($a[$p.'inTransaction']);
9292
}
9393

94-
if (!isset($a[$m.'errorInfo'][1], $a[$m.'errorInfo'][2])) {
95-
unset($a[$m.'errorInfo']);
94+
if (!isset($a[$p.'errorInfo'][1], $a[$p.'errorInfo'][2])) {
95+
unset($a[$p.'errorInfo']);
9696
}
9797

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

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

108-
if (!isset($a[$m.'errorInfo'][1], $a[$m.'errorInfo'][2])) {
109-
unset($a[$m.'errorInfo']);
108+
if (!isset($a[$p.'errorInfo'][1], $a[$p.'errorInfo'][2])) {
109+
unset($a[$p.'errorInfo']);
110110
}
111111

112112
return $a;

0 commit comments

Comments
 (0)