8000 feature #13960 [VarDumper] Add Caster for XML-parser resources (nicol… · symfony/symfony@83c6d22 · GitHub
[go: up one dir, main page]

Skip to content

Commit 83c6d22

Browse files
committed
feature #13960 [VarDumper] Add Caster for XML-parser resources (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [VarDumper] Add Caster for XML-parser resources | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 00701cb [VarDumper] Add Caster for XML-parser resources
2 parents 7eef5f2 + 00701cb commit 83c6d22

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
* Casts XML resources to array representation.
18+
*
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*/
21+
class XmlResourceCaster
22+
{
23+
private static $xmlErrors = array(
24+
XML_ERROR_NONE => 'XML_ERROR_NONE',
25+
XML_ERROR_NO_MEMORY => 'XML_ERROR_NO_MEMORY',
26+
XML_ERROR_SYNTAX => 'XML_ERROR_SYNTAX',
27+
XML_ERROR_NO_ELEMENTS => 'XML_ERROR_NO_ELEMENTS',
28+
XML_ERROR_INVALID_TOKEN => 'XML_ERROR_INVALID_TOKEN',
29+
XML_ERROR_UNCLOSED_TOKEN => 'XML_ERROR_UNCLOSED_TOKEN',
30+
XML_ERROR_PARTIAL_CHAR => 'XML_ERROR_PARTIAL_CHAR',
31+
XML_ERROR_TAG_MISMATCH => 'XML_ERROR_TAG_MISMATCH',
32+
XML_ERROR_DUPLICATE_ATTRIBUTE => 'XML_ERROR_DUPLICATE_ATTRIBUTE',
33+
XML_ERROR_JUNK_AFTER_DOC_ELEMENT => 'XML_ERROR_JUNK_AFTER_DOC_ELEMENT',
34+
XML_ERROR_PARAM_ENTITY_REF => 'XML_ERROR_PARAM_ENTITY_REF',
35+
XML_ERROR_UNDEFINED_ENTITY => 'XML_ERROR_UNDEFINED_ENTITY',
36+
XML_ERROR_RECURSIVE_ENTITY_REF => 'XML_ERROR_RECURSIVE_ENTITY_REF',
37+
XML_ERROR_ASYNC_ENTITY => 'XML_ERROR_ASYNC_ENTITY',
38+
XML_ERROR_BAD_CHAR_REF => 'XML_ERROR_BAD_CHAR_REF',
39+
XML_ERROR_BINARY_ENTITY_REF => 'XML_ERROR_BINARY_ENTITY_REF',
40+
XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF => 'XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF',
41+
XML_ERROR_MISPLACED_XML_PI => 'XML_ERROR_MISPLACED_XML_PI',
42+
XML_ERROR_UNKNOWN_ENCODING => 'XML_ERROR_UNKNOWN_ENCODING',
43+
XML_ERROR_INCORRECT_ENCODING => 'XML_ERROR_INCORRECT_ENCODING',
44+
XML_ERROR_UNCLOSED_CDATA_SECTION => 'XML_ERROR_UNCLOSED_CDATA_SECTION',
45+
XML_ERROR_EXTERNAL_ENTITY_HANDLING => 'XML_ERROR_EXTERNAL_ENTITY_HANDLING',
46+
);
47+
48+
public static function castXml($h, array $a, Stub $stub, $isNested)
49+
{
50+
$a['current_byte_index'] = xml_get_current_byte_index($h);
51+
$a['current_column_number'] = xml_get_current_column_number($h);
52+
$a['current_line_number'] = xml_get_current_line_number($h);
53+
$a['error_code'] = xml_get_error_code($h);
54+
55+
if (isset(self::$xmlErrors[$a['error_code']])) {
56+
$a['error_code'] = new ConstStub(self::$xmlErrors[$a['error_code']], $a['error_code']);
57+
}
58+
59+
return $a;
60+
}
61+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ abstract class AbstractCloner implements ClonerInterface
8484
':process' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castProcess',
8585
':stream' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStream',
8686
':stream-context' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStreamContext',
87+
':xml' => 'Symfony\Component\VarDumper\Caster\XmlResourceCaster::castXml',
8788
);
8889

8990
protected $maxItems = 2500;

src/Symfony/Component/VarDumper/Tests/CliDumperTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,40 @@ public function testGet()
102102
b"bin-key-é" => ""
103103
]
104104
105+
EOTXT
106+
,
107+
$out
108+
);
109+
}
110+
111+
public function testXmlResource()
112+
{
113+
if (!extension_loaded('xml')) {
114+
$this->markTestSkipped('xml extension is required');
115+
}
116+
117+
$var = xml_parser_create();
118+
$ref = (int) $var;
119+
120+
$dumper = new CliDumper();
121+
$dumper->setColors(false);
122+
$cloner = new VarCloner();
123+
124+
$data = $cloner->cloneVar($var);
125+
$out = fopen('php://memory', 'r+b');
126+
$dumper->dump($data, $out);
127+
rewind($out);
128+
$out = stream_get_contents($out);
129+
130+
$this->assertSame(
131+
<<<EOTXT
132+
:xml {@{$ref}
133+
current_byte_index: 0
134+
current_column_number: 1
135+
current_line_number: 1
136+
error_code: XML_ERROR_NONE
137+
}
138+
105139
EOTXT
106140
,
107141
$out

0 commit comments

Comments
 (0)
0