Skip to content

Commit cbb342e

Browse files
[VarDumper] add Stub objects for cutting cleanly and dumping consts
1 parent 641f7c4 commit cbb342e

18 files changed

+482
-271
lines changed

Caster/CasterStub.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
use Symfony\Component\VarDumper\Cloner\Stub;
15+
16+
/**
17+
* Represents the main properties of a PHP variable, pre-casted by a caster.
18+
*
19+
* @author Nicolas Grekas <[email protected]>
20+
*/
21+
class CasterStub extends Stub
22+
{
23+
public function __construct($value, $class = '')
24+
{
25+
switch (gettype($value)) {
26+
case 'object':
27+
$this->type = self::TYPE_OBJECT;
28+
$this->class = get_class($value);
29+
$this->value = spl_object_hash($value);
30+
$this->cut = -1;
31+
break;
32+
33+
case 'array':
34+
$this->type = self::TYPE_ARRAY;
35+
$this->class = self::ARRAY_ASSOC;
36+
$this->cut = $this->value = count($value);
37+
break;
38+
39+
case 'resource':
40+
case 'unknown type':
41+
$this->type = self::TYPE_RESOURCE;
42+
$this->class = @get_resource_type($value);
43+
$this->value = (int) $value;
44+
$this->cut = -1;
45+
break;
46+
47+
case 'string':
48+
if ('' === $class) {
49+
$this->type = self::TYPE_STRING;
50+
$this->class = preg_match('//u', $value) ? self::STRING_UTF8 : self::STRING_BINARY;
51+
$this->cut = self::STRING_BINARY === $this->class ? strlen($value) : (function_exists('iconv_strlen') ? iconv_strlen($value, 'UTF-8') : -1);
52+
break;
53+
}
54+
// No break;
55+
56+
default:
57+
$this->class = $class;
58+
$this->value = $value;
59+
break;
60+
}
61+
}
62+
}

Caster/DOMCaster.php

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\VarDumper\Caster;
1313

14+
use Symfony\Component\VarDumper\Cloner\Stub;
15+
1416
/**
1517
* Casts DOM related classes to array representation.
1618
*
@@ -38,16 +40,37 @@ class DOMCaster
3840
DOM_VALIDATION_ERR => 'DOM_VALIDATION_ERR',
3941
);
4042

41-
public static function castException(\DOMException $e, array $a, $isNested, &$cut)
43+
private static $nodeTypes = array(
44+
XML_ELEMENT_NODE => 'XML_ELEMENT_NODE',
45+
XML_ATTRIBUTE_NODE => 'XML_ATTRIBUTE_NODE',
46+
XML_TEXT_NODE => 'XML_TEXT_NODE',
47+
XML_CDATA_SECTION_NODE => 'XML_CDATA_SECTION_NODE',
48+
XML_ENTITY_REF_NODE => 'XML_ENTITY_REF_NODE',
49+
XML_ENTITY_NODE => 'XML_ENTITY_NODE',
50+
XML_PI_NODE => 'XML_PI_NODE',
51+
XML_COMMENT_NODE => 'XML_COMMENT_NODE',
52+
XML_DOCUMENT_NODE => 'XML_DOCUMENT_NODE',
53+
XML_DOCUMENT_TYPE_NODE => 'XML_DOCUMENT_TYPE_NODE',
54+
XML_DOCUMENT_FRAG_NODE => 'XML_DOCUMENT_FRAG_NODE',
55+
XML_NOTATION_NODE => 'XML_NOTATION_NODE',
56+
XML_HTML_DOCUMENT_NODE => 'XML_HTML_DOCUMENT_NODE',
57+
XML_DTD_NODE => 'XML_DTD_NODE',
58+
XML_ELEMENT_DECL_NODE => 'XML_ELEMENT_DECL_NODE',
59+
XML_ATTRIBUTE_DECL_NODE => 'XML_ATTRIBUTE_DECL_NODE',
60+
XML_ENTITY_DECL_NODE => 'XML_ENTITY_DECL_NODE',
61+
XML_NAMESPACE_DECL_NODE => 'XML_NAMESPACE_DECL_NODE',
62+
);
63+
64+
public static function castException(\DOMException $e, array $a, Stub $stub, $isNested)
4265
{
4366
if (isset($a["\0*\0code"], self::$errorCodes[$a["\0*\0code"]])) {
44-
$a["\0*\0code"] .= ' ('.self::$errorCodes[$a["\0*\0code"]].')';
67+
$a["\0*\0code"] = new CasterStub(self::$errorCodes[$a["\0*\0code"]], 'const');
4568
}
4669

4770
return $a;
4871
}
4972

50-
public static function castLength($dom, array $a, $isNested, &$cut)
73+
public static function castLength($dom, array $a, Stub $stub, $isNested)
5174
{
5275
$a += array(
5376
'length' => $dom->length,
@@ -56,7 +79,7 @@ public static function castLength($dom, array $a, $isNested, &$cut)
5679
return $a;
5780
}
5881

59-
public static function castImplementation($dom, array $a, $isNested, &$cut)
82+
public static function castImplementation($dom, array $a, Stub $stub, $isNested)
6083
{
6184
$a += array(
6285
"\0~\0Core" => '1.0',
@@ -66,61 +89,57 @@ public static function castImplementation($dom, array $a, $isNested, &$cut)
6689
return $a;
6790
}
6891

69-
public static function castNode(\DOMNode $dom, array $a, $isNested, &$cut)
92+
public static function castNode(\DOMNode $dom, array $a, Stub $stub, $isNested)
7093
{
71-
// Commented lines denote properties that exist but are better not dumped for clarity.
72-
7394
$a += array(
7495
'nodeName' => $dom->nodeName,
75-
//'nodeValue' => $dom->nodeValue,
76-
'nodeType' => $dom->nodeType,
77-
//'parentNode' => $dom->parentNode,
96+
'nodeValue' => new CasterStub($dom->nodeValue),
97+
'nodeType' => new CasterStub(self::$nodeTypes[$dom->nodeType], 'const'),
98+
'parentNode' => new CasterStub($dom->parentNode),
7899
'childNodes' => $dom->childNodes,
79-
//'firstChild' => $dom->firstChild,
80-
//'lastChild' => $dom->lastChild,
81-
//'previousSibling' => $dom->previousSibling,
82-
//'nextSibling' => $dom->nextSibling,
100+
'firstChild' => new CasterStub($dom->firstChild),
101+
'lastChild' => new CasterStub($dom->lastChild),
102+
'previousSibling' => new CasterStub($dom->previousSibling),
103+
'nextSibling' => new CasterStub($dom->nextSibling),
83104
'attributes' => $dom->attributes,
84-
//'ownerDocument' => $dom->ownerDocument,
105+
'ownerDocument' => new CasterStub($dom->ownerDocument),
85106
'namespaceURI' => $dom->namespaceURI,
86107
'prefix' => $dom->prefix,
87108
'localName' => $dom->localName,
88109
'baseURI' => $dom->baseURI,
89-
//'textContent' => $dom->textContent,
110+
'textContent' => new CasterStub($dom->textContent),
90111
);
91-
$cut += 8;
92112

93113
return $a;
94114
}
95115

96-
public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, $isNested, &$cut)
116+
public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, Stub $stub, $isNested)
97117
{
98118
// Commented lines denote properties that exist but are better not dumped for clarity.
99119

100120
$a += array(
101121
'nodeName' => $dom->nodeName,
102-
//'nodeValue' => $dom->nodeValue,
103-
'nodeType' => $dom->nodeType,
122+
'nodeValue' => new CasterStub($dom->nodeValue),
123+
'nodeType' => new CasterStub(self::$nodeTypes[$dom->nodeType], 'const'),
104124
'prefix' => $dom->prefix,
105125
'localName' => $dom->localName,
106126
'namespaceURI' => $dom->namespaceURI,
107-
//'ownerDocument' => $dom->ownerDocument,
108-
//'parentNode' => $dom->parentNode,
127+
'ownerDocument' => new CasterStub($dom->ownerDocument),
128+
'parentNode' => new CasterStub($dom->parentNode),
109129
);
110-
$cut += 3;
111130

112131
return $a;
113132
}
114133

115-
public static function castDocument(\DOMDocument $dom, array $a, $isNested, &$cut)
134+
public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $isNested)
116135
{
117136
$formatOutput = $dom->formatOutput;
118137
$dom->formatOutput = true;
119138

120139
$a += array(
121140
'doctype' => $dom->doctype,
122141
'implementation' => $dom->implementation,
123-
'documentElement' => $dom->documentElement,
142+
'documentElement' => new CasterStub($dom->documentElement),
124143
'actualEncoding' => $dom->actualEncoding,
125144
'encoding' => $dom->encoding,
126145
'xmlEncoding' => $dom->xmlEncoding,
@@ -145,7 +164,7 @@ public static function castDocument(\DOMDocument $dom, array $a, $isNested, &$cu
145164
return $a;
146165
}
147166

148-
public static function castCharacterData(\DOMCharacterData $dom, array $a, $isNested, &$cut)
167+
public static function castCharacterData(\DOMCharacterData $dom, array $a, Stub $stub, $isNested)
149168
{
150169
$a += array(
151170
'data' => $dom->data,
@@ -155,7 +174,7 @@ public static function castCharacterData(\DOMCharacterData $dom, array $a, $isNe
155174
return $a;
156175
}
157176

158-
public static function castAttr(\DOMAttr $dom, array $a, $isNested, &$cut)
177+
public static function castAttr(\DOMAttr $dom, array $a, Stub $stub, $isNested)
159178
{
160179
$a += array(
161180
'name' => $dom->name,
@@ -168,7 +187,7 @@ public static function castAttr(\DOMAttr $dom, array $a, $isNested, &$cut)
168187
return $a;
169188
}
170189

171-
public static function castElement(\DOMElement $dom, array $a, $isNested, &$cut)
190+
public static function castElement(\DOMElement $dom, array $a, Stub $stub, $isNested)
172191
{
173192
$a += array(
174193
'tagName' => $dom->tagName,
@@ -178,7 +197,7 @@ public static function castElement(\DOMElement $dom, array $a, $isNested, &$cut)
178197
return $a;
179198
}
180199

181-
public static function castText(\DOMText $dom, array $a, $isNested, &$cut)
200+
public static function castText(\DOMText $dom, array $a, Stub $stub, $isNested)
182201
{
183202
$a += array(
184203
'wholeText' => $dom->wholeText,
@@ -187,7 +206,7 @@ public static function castText(\DOMText $dom, array $a, $isNested, &$cut)
187206
return $a;
188207
}
189208

190-
public static function castTypeinfo(\DOMTypeinfo $dom, array $a, $isNested, &$cut)
209+
public static function castTypeinfo(\DOMTypeinfo $dom, array $a, Stub $stub, $isNested)
191210
{
192211
$a += array(
193212
'typeName' => $dom->typeName,
@@ -197,7 +216,7 @@ public static function castTypeinfo(\DOMTypeinfo $dom, array $a, $isNested, &$cu
197216
return $a;
198217
}
199218

200-
public static function castDomError(\DOMDomError $dom, array $a, $isNested, &$cut)
219+
public static function castDomError(\DOMDomError $dom, array $a, Stub $stub, $isNested)
201220
{
202221
$a += array(
203222
'severity' => $dom->severity,
@@ -211,7 +230,7 @@ public static function castDomError(\DOMDomError $dom, array $a, $isNested, &$cu
211230
return $a;
212231
}
213232

214-
public static function castLocator(\DOMLocator $dom, array $a, $isNested, &$cut)
233+
public static function castLocator(\DOMLocator $dom, array $a, Stub $stub, $isNested)
215234
{
216235
$a += array(
217236
'lineNumber' => $dom->lineNumber,
@@ -224,7 +243,7 @@ public static function castLocator(\DOMLocator $dom, array $a, $isNested, &$cut)
224243
return $a;
225244
}
226245

227-
public static function castDocumentType(\DOMDocumentType $dom, array $a, $isNested, &$cut)
246+
public static function castDocumentType(\DOMDocumentType $dom, array $a, Stub $stub, $isNested)
228247
{
229248
$a += array(
230249
'name' => $dom->name,
@@ -238,7 +257,7 @@ public static function castDocumentType(\DOMDocumentType $dom, array $a, $isNest
238257
return $a;
239258
}
240259

241-
public static function castNotation(\DOMNotation $dom, array $a, $isNested, &$cut)
260+
public static function castNotation(\DOMNotation $dom, array $a, Stub $stub, $isNested)
242261
{
243262
$a += array(
244263
'publicId' => $dom->publicId,
@@ -248,7 +267,7 @@ public static function castNotation(\DOMNotation $dom, array $a, $isNested, &$cu
248267
return $a;
249268
}
250269

251-
public static function castEntity(\DOMEntity $dom, array $a, $isNested, &$cut)
270+
public static function castEntity(\DOMEntity $dom, array $a, Stub $stub, $isNested)
252271
{
253272
$a += array(
254273
'publicId' => $dom->publicId,
@@ -262,7 +281,7 @@ public static function castEntity(\DOMEntity $dom, array $a, $isNested, &$cut)
262281
return $a;
263282
}
264283

265-
public static function castProcessingInstruction(\DOMProcessingInstruction $dom, array $a, $isNested, &$cut)
284+
public static function castProcessingInstruction(\DOMProcessingInstruction $dom, array $a, Stub $stub, $isNested)
266285
{
267286
$a += array(
268287
'target' => $dom->target,
@@ -272,7 +291,7 @@ public static function castProcessingInstruction(\DOMProcessingInstruction $dom,
272291
return $a;
273292
}
274293

275-
public static function castXPath(\DOMXPath $dom, array $a, $isNested, &$cut)
294+
public static function castXPath(\DOMXPath $dom, array $a, Stub $stub, $isNested)
276295
{
277296
$a += array(
278297
'document' => $dom->document,

Caster/DoctrineCaster.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace Symfony\Component\VarDumper\Caster;
1313

14-
use Doctrine\Common\Persistence\ObjectManager;
1514
use Doctrine\Common\Proxy\Proxy as CommonProxy;
1615
use Doctrine\ORM\Proxy\Proxy as OrmProxy;
1716
use Doctrine\ORM\PersistentCollection;
17+
use Symfony\Component\VarDumper\Cloner\Stub;
1818

1919
/**
2020
* Casts Doctrine related classes to array representation.
@@ -23,50 +23,36 @@
2323
*/
2424
class DoctrineCaster
2525
{
26-
public static function castCommonProxy(CommonProxy $proxy, array $a, $isNested, &$cut)
26+
public static function castCommonProxy(CommonProxy $proxy, array $a, Stub $stub, $isNested)
2727
{
2828
unset(
2929
$a['__cloner__'],
3030
$a['__initializer__']
3131
);
32-
$cut += 2;
32+
$stub->cut += 2;
3333

3434
return $a;
3535
}
3636

37-
public static function castOrmProxy(OrmProxy $proxy, array $a, $isNested, &$cut)
37+
public static function castOrmProxy(OrmProxy $proxy, array $a, Stub $stub, $isNested)
3838
{
3939
$prefix = "\0Doctrine\\ORM\\Proxy\\Proxy\0";
4040
unset(
4141
$a[$prefix.'_entityPersister'],
4242
$a[$prefix.'_identifier']
4343
);
44-
$cut += 2;
44+
$stub->cut += 2;
4545

4646
return $a;
4747
}
4848

49-
public static function castObjectManager(ObjectManager $manager, array $a, $isNested, &$cut)
50-
{
51-
if ($isNested) {
52-
$cut += count($a);
53-
54-
return array();
55-
}
56-
57-
return $a;
58-
}
59-
60-
public static function castPersistentCollection(PersistentCollection $coll, array $a, $isNested, &$cut)
49+
public static function castPersistentCollection(PersistentCollection $coll, array $a, Stub $stub, $isNested)
6150
{
6251
$prefix = "\0Doctrine\\ORM\\PersistentCollection\0";
63-
unset(
64-
$a[$prefix.'snapshot'],
65-
$a[$prefix.'association'],
66-
$a[$prefix.'em'],
67-
$a[$prefix.'typeClass']
68-
);
69-
$cut += 4;
52+
53+
$a[$prefix.'snapshot'] = new CasterStub($a[$prefix.'snapshot']);
54+
$a[$prefix.'association'] = new CasterStub($a[$prefix.'association']);
55+
$a[$prefix.'typeClass'] = new CasterStub($a[$prefix.'typeClass']);
7056

7157
return $a;
7258
}

0 commit comments

Comments
 (0)