Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3ddbf4b

Browse filesBrowse files
[VarDumper] add casters for per class/resource custom state extraction
1 parent 5b7ae28 commit 3ddbf4b
Copy full SHA for 3ddbf4b

File tree

Expand file treeCollapse file tree

3 files changed

+197
-14
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+197
-14
lines changed
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/**
15+
* Casts Reflector related classes to array representation.
16+
*
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class ReflectionCaster
20+
{
21+
public static function castReflector(\Reflector $c, array $a)
22+
{
23+
$a["\0~\0reflection"] = $c->__toString();
24+
25+
return $a;
26+
}
27+
28+
public static function castClosure(\Closure $c, array $a)
29+
{
30+
$a = static::castReflector(new \ReflectionFunction($c), $a);
31+
unset($a["\0+\0000"], $a['name']);
32+
33+
return $a;
34+
}
35+
}
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
/**
15+
* Casts common resource types to array representation.
16+
*
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class ResourceCaster
20+
{
21+
public static function castCurl($h, array $a)
22+
{
23+
return curl_getinfo($h);
24+
}
25+
26+
public static function castDba($dba, array $a)
27+
{
28+
$list = dba_list();
29+
$a['file'] = $list[substr((string) $dba, 13)];
30+
31+
return $a;
32+
}
33+
34+
public static function castProcess($process, array $a)
35+
{
36+
return proc_get_status($process);
37+
}
38+
39+
public static function castStream($stream, array $a)
40+
{
41+
return stream_get_meta_data($stream) + static::castStreamContext($stream, $a);
42+
}
43+
44+
public static function castStreamContext($stream, array $a)
45+
{
46+
return stream_context_get_params($stream);
47+
}
48+
49+
public static function castGd($gd, array $a)
50+
{
51+
$a['size'] = imagesx($gd).'x'.imagesy($gd);
52+
$a['trueColor'] = imageistruecolor($gd);
53+
54+
return $a;
55+
}
56+
57+
public static function castMysqlLink($h, array $a)
58+
{
59+
$a['host'] = mysql_get_host_info($h);
60+
$a['protocol'] = mysql_get_proto_info($h);
61+
$a['server'] = mysql_get_server_info($h);
62+
63+
return $a;
64+
}
65+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
+97-14Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,58 @@
2020
*/
2121
abstract class AbstractCloner implements ClonerInterface
2222
{
23-
protected $maxItems = 2500;
23+
public static $defaultCasters = array(
24+
'o:Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure',
25+
'o:Reflector' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castReflector',
26+
27+
'r:curl' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castCurl',
28+
'r:dba' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
29+
'r:dba persistent' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
30+
'r:gd' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castGd',
31+
'r:mysql link' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castMysqlLink',
32+
'r:process' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castProcess',
33+
'r:stream' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStream',
34+
'r:stream-context' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStreamContext',
35+
);
36+
37+
protected $maxItems = 250;
2438
protected $maxString = -1;
2539

40+
private $casters = array();
2641
private $data = array(array(null));
2742
private $prevErrorHandler;
43+
private $classInfo = array();
44+
45+
/**
46+
* @param callable[]|null $casters A map of casters.
47+
*
48+
* @see addCasters
49+
*/
50+
public function __construct(array $casters = null)
51+
{
52+
if (null === $casters) {
53+
$casters = static::$defaultCasters;
54+
}
55+
$this->addCasters($casters);
56+
}
57+
58+
/**
59+
* Adds casters for resources and objects.
60+
*
61+
* Maps resources or objects types to a callback.
62+
* Types are in the key, with a callable caster for value.
63+
* Objects class are to be prefixed with a `o:`,
64+
* resources type are to be prefixed with a `r:`,
65+
* see e.g. static::$defaultCasters.
66+
*
67+
* @param callable[] $casters A map of casters.
68+
*/
69+
public function addCasters(array $casters)
70+
{
71+
foreach ($casters as $type => $callback) {
72+
$this->casters[strtolower($type)][] = $callback;
73+
}
74+
}
2875

2976
/**
3077
* Sets the maximum number of items to clone past the first level in nested structures.
@@ -81,36 +128,71 @@ abstract protected function doClone($var);
81128
/**
82129
* Casts an object to an array representation.
83130
*
84-
* @param string $class The class of the object.
85-
* @param object $obj The object itself.
131+
* @param string $class The class of the object.
132+
* @param object $obj The object itself.
133+
* @param bool $isNested True if the object is nested in the dumped structure.
134+
* @param int &$cut After the cast, number of items removed from $obj.
86135
*
87136
* @return array The object casted as array.
88137
*/
89-
protected function castObject($class, $obj)
138+
protected function castObject($class, $obj, $isNested, &$cut)
90139
{
91-
if (method_exists($obj, '__debugInfo')) {
92-
if (!$a = $this->callCaster(array($this, '__debugInfo'), $obj, array())) {
93-
$a = (array) $obj;
94-
}
140+
if (isset($this->classInfo[$class])) {
141+
$classInfo = $this->classInfo[$class];
142+
} else {
143+
$classInfo = array(
144+
method_exists($class, '__debugInfo'),
145+
new \ReflectionClass($class),
146+
array_reverse(array($class => $class) + class_parents($class) + class_implements($class) + array('*' => '*')),
147+
);
148+
149+
$this->classInfo[$class] = $classInfo;
150+
}
151+
152+
if ($classInfo[0]) {
153+
$a = $this->callCaster(array($obj, '__debugInfo'), $obj, array(), $isNested);
95154
} else {
96155
$a = (array) $obj;
97156
}
157+
$cut = 0;
158+
159+
foreach ($a as $k => $p) {
160+
if (!isset($k[0]) || ("\0" !== $k[0] && !$classInfo[1]->hasProperty($k))) {
161+
unset($a[$k]);
162+
$a["\0+\0".$k] = $p;
163+
}
164+
}
165+
166+
foreach ($classInfo[2] as $p) {
167+
if (!empty($this->casters[$p = 'o:'.strtolower($p)])) {
168+
foreach ($this->casters[$p] as $p) {
169+
$a = $this->callCaster($p, $obj, $a, $isNested, $cut);
170+
}
171+
}
172+
}
98173

99174
return $a;
100175
}
101176

102177
/**
103178
* Casts a resource to an array representation.
104179
*
105-
* @param string $type The type of the resource.
106-
* @param resource $res The resource.
180+
* @param string $type The type of the resource.
181+
* @param resource $res The resource.
182+
* @param bool $isNested True if the object is nested in the dumped structure.
107183
*
108184
* @return array The resource casted as array.
109185
*/
110-
protected function castResource($type, $res)
186+
protected function castResource($type, $res, $isNested)
111187
{
112188
$a = array();
113189

190+
if (!empty($this->casters['r:'.$type])) {
191+
foreach ($this->casters['r:'.$type] as $c) {
192+
$a = $this->callCaster($c, $res, $a, $isNested);
193+
}
194+
}
195+
114196
return $a;
115197
}
116198

@@ -120,14 +202,15 @@ protected function castResource($type, $res)
120202
* @param callable $callback The caster.
121203
* @param object|resource $obj The object/resource being casted.
122204
* @param array $a The result of the previous cast for chained casters.
205+
* @param bool $isNested True if $obj is nested in the dumped structure.
206+
* @param int &$cut After the cast, number of items removed from $obj.
123207
*
124208
* @return array The casted object/resource.
125209
*/
126-
private function callCaster($callback, $obj, $a)
210+
private function callCaster($callback, $obj, $a, $isNested, &$cut = 0)
127211
{
128212
try {
129-
// Ignore invalid $callback
130-
$cast = @call_user_func($callback, $obj, $a);
213+
$cast = call_user_func_array($callback, array($obj, $a, $isNested, &$cut));
131214

132215
if (is_array($cast)) {
133216
$a = $cast;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.