[VarDumper] add casters for per class/resource custom state extraction
This commit is contained in:
parent
5b7ae2862f
commit
3ddbf4b6b9
35
src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
Normal file
35
src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\VarDumper\Caster;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Casts Reflector related classes to array representation.
|
||||||
|
*
|
||||||
|
* @author Nicolas Grekas <p@tchwork.com>
|
||||||
|
*/
|
||||||
|
class ReflectionCaster
|
||||||
|
{
|
||||||
|
public static function castReflector(\Reflector $c, array $a)
|
||||||
|
{
|
||||||
|
$a["\0~\0reflection"] = $c->__toString();
|
||||||
|
|
||||||
|
return $a;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function castClosure(\Closure $c, array $a)
|
||||||
|
{
|
||||||
|
$a = static::castReflector(new \ReflectionFunction($c), $a);
|
||||||
|
unset($a["\0+\0000"], $a['name']);
|
||||||
|
|
||||||
|
return $a;
|
||||||
|
}
|
||||||
|
}
|
65
src/Symfony/Component/VarDumper/Caster/ResourceCaster.php
Normal file
65
src/Symfony/Component/VarDumper/Caster/ResourceCaster.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\VarDumper\Caster;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Casts common resource types to array representation.
|
||||||
|
*
|
||||||
|
* @author Nicolas Grekas <p@tchwork.com>
|
||||||
|
*/
|
||||||
|
class ResourceCaster
|
||||||
|
{
|
||||||
|
public static function castCurl($h, array $a)
|
||||||
|
{
|
||||||
|
return curl_getinfo($h);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function castDba($dba, array $a)
|
||||||
|
{
|
||||||
|
$list = dba_list();
|
||||||
|
$a['file'] = $list[substr((string) $dba, 13)];
|
||||||
|
|
||||||
|
return $a;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function castProcess($process, array $a)
|
||||||
|
{
|
||||||
|
return proc_get_status($process);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function castStream($stream, array $a)
|
||||||
|
{
|
||||||
|
return stream_get_meta_data($stream) + static::castStreamContext($stream, $a);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function castStreamContext($stream, array $a)
|
||||||
|
{
|
||||||
|
return stream_context_get_params($stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function castGd($gd, array $a)
|
||||||
|
{
|
||||||
|
$a['size'] = imagesx($gd).'x'.imagesy($gd);
|
||||||
|
$a['trueColor'] = imageistruecolor($gd);
|
||||||
|
|
||||||
|
return $a;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function castMysqlLink($h, array $a)
|
||||||
|
{
|
||||||
|
$a['host'] = mysql_get_host_info($h);
|
||||||
|
$a['protocol'] = mysql_get_proto_info($h);
|
||||||
|
$a['server'] = mysql_get_server_info($h);
|
||||||
|
|
||||||
|
return $a;
|
||||||
|
}
|
||||||
|
}
|
@ -20,11 +20,58 @@ use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
|
|||||||
*/
|
*/
|
||||||
abstract class AbstractCloner implements ClonerInterface
|
abstract class AbstractCloner implements ClonerInterface
|
||||||
{
|
{
|
||||||
protected $maxItems = 2500;
|
public static $defaultCasters = array(
|
||||||
|
'o:Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure',
|
||||||
|
'o:Reflector' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castReflector',
|
||||||
|
|
||||||
|
'r:curl' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castCurl',
|
||||||
|
'r:dba' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
|
||||||
|
'r:dba persistent' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
|
||||||
|
'r:gd' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castGd',
|
||||||
|
'r:mysql link' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castMysqlLink',
|
||||||
|
'r:process' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castProcess',
|
||||||
|
'r:stream' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStream',
|
||||||
|
'r:stream-context' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStreamContext',
|
||||||
|
);
|
||||||
|
|
||||||
|
protected $maxItems = 250;
|
||||||
protected $maxString = -1;
|
protected $maxString = -1;
|
||||||
|
|
||||||
|
private $casters = array();
|
||||||
private $data = array(array(null));
|
private $data = array(array(null));
|
||||||
private $prevErrorHandler;
|
private $prevErrorHandler;
|
||||||
|
private $classInfo = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param callable[]|null $casters A map of casters.
|
||||||
|
*
|
||||||
|
* @see addCasters
|
||||||
|
*/
|
||||||
|
public function __construct(array $casters = null)
|
||||||
|
{
|
||||||
|
if (null === $casters) {
|
||||||
|
$casters = static::$defaultCasters;
|
||||||
|
}
|
||||||
|
$this->addCasters($casters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds casters for resources and objects.
|
||||||
|
*
|
||||||
|
* Maps resources or objects types to a callback.
|
||||||
|
* Types are in the key, with a callable caster for value.
|
||||||
|
* Objects class are to be prefixed with a `o:`,
|
||||||
|
* resources type are to be prefixed with a `r:`,
|
||||||
|
* see e.g. static::$defaultCasters.
|
||||||
|
*
|
||||||
|
* @param callable[] $casters A map of casters.
|
||||||
|
*/
|
||||||
|
public function addCasters(array $casters)
|
||||||
|
{
|
||||||
|
foreach ($casters as $type => $callback) {
|
||||||
|
$this->casters[strtolower($type)][] = $callback;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the maximum number of items to clone past the first level in nested structures.
|
* Sets the maximum number of items to clone past the first level in nested structures.
|
||||||
@ -81,20 +128,48 @@ abstract class AbstractCloner implements ClonerInterface
|
|||||||
/**
|
/**
|
||||||
* Casts an object to an array representation.
|
* Casts an object to an array representation.
|
||||||
*
|
*
|
||||||
* @param string $class The class of the object.
|
* @param string $class The class of the object.
|
||||||
* @param object $obj The object itself.
|
* @param object $obj The object itself.
|
||||||
|
* @param bool $isNested True if the object is nested in the dumped structure.
|
||||||
|
* @param int &$cut After the cast, number of items removed from $obj.
|
||||||
*
|
*
|
||||||
* @return array The object casted as array.
|
* @return array The object casted as array.
|
||||||
*/
|
*/
|
||||||
protected function castObject($class, $obj)
|
protected function castObject($class, $obj, $isNested, &$cut)
|
||||||
{
|
{
|
||||||
if (method_exists($obj, '__debugInfo')) {
|
if (isset($this->classInfo[$class])) {
|
||||||
if (!$a = $this->callCaster(array($this, '__debugInfo'), $obj, array())) {
|
$classInfo = $this->classInfo[$class];
|
||||||
$a = (array) $obj;
|
} else {
|
||||||
}
|
$classInfo = array(
|
||||||
|
method_exists($class, '__debugInfo'),
|
||||||
|
new \ReflectionClass($class),
|
||||||
|
array_reverse(array($class => $class) + class_parents($class) + class_implements($class) + array('*' => '*')),
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->classInfo[$class] = $classInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($classInfo[0]) {
|
||||||
|
$a = $this->callCaster(array($obj, '__debugInfo'), $obj, array(), $isNested);
|
||||||
} else {
|
} else {
|
||||||
$a = (array) $obj;
|
$a = (array) $obj;
|
||||||
}
|
}
|
||||||
|
$cut = 0;
|
||||||
|
|
||||||
|
foreach ($a as $k => $p) {
|
||||||
|
if (!isset($k[0]) || ("\0" !== $k[0] && !$classInfo[1]->hasProperty($k))) {
|
||||||
|
unset($a[$k]);
|
||||||
|
$a["\0+\0".$k] = $p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($classInfo[2] as $p) {
|
||||||
|
if (!empty($this->casters[$p = 'o:'.strtolower($p)])) {
|
||||||
|
foreach ($this->casters[$p] as $p) {
|
||||||
|
$a = $this->callCaster($p, $obj, $a, $isNested, $cut);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $a;
|
return $a;
|
||||||
}
|
}
|
||||||
@ -102,15 +177,22 @@ abstract class AbstractCloner implements ClonerInterface
|
|||||||
/**
|
/**
|
||||||
* Casts a resource to an array representation.
|
* Casts a resource to an array representation.
|
||||||
*
|
*
|
||||||
* @param string $type The type of the resource.
|
* @param string $type The type of the resource.
|
||||||
* @param resource $res The resource.
|
* @param resource $res The resource.
|
||||||
|
* @param bool $isNested True if the object is nested in the dumped structure.
|
||||||
*
|
*
|
||||||
* @return array The resource casted as array.
|
* @return array The resource casted as array.
|
||||||
*/
|
*/
|
||||||
protected function castResource($type, $res)
|
protected function castResource($type, $res, $isNested)
|
||||||
{
|
{
|
||||||
$a = array();
|
$a = array();
|
||||||
|
|
||||||
|
if (!empty($this->casters['r:'.$type])) {
|
||||||
|
foreach ($this->casters['r:'.$type] as $c) {
|
||||||
|
$a = $this->callCaster($c, $res, $a, $isNested);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $a;
|
return $a;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,14 +202,15 @@ abstract class AbstractCloner implements ClonerInterface
|
|||||||
* @param callable $callback The caster.
|
* @param callable $callback The caster.
|
||||||
* @param object|resource $obj The object/resource being casted.
|
* @param object|resource $obj The object/resource being casted.
|
||||||
* @param array $a The result of the previous cast for chained casters.
|
* @param array $a The result of the previous cast for chained casters.
|
||||||
|
* @param bool $isNested True if $obj is nested in the dumped structure.
|
||||||
|
* @param int &$cut After the cast, number of items removed from $obj.
|
||||||
*
|
*
|
||||||
* @return array The casted object/resource.
|
* @return array The casted object/resource.
|
||||||
*/
|
*/
|
||||||
private function callCaster($callback, $obj, $a)
|
private function callCaster($callback, $obj, $a, $isNested, &$cut = 0)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
// Ignore invalid $callback
|
$cast = call_user_func_array($callback, array($obj, $a, $isNested, &$cut));
|
||||||
$cast = @call_user_func($callback, $obj, $a);
|
|
||||||
|
|
||||||
if (is_array($cast)) {
|
if (is_array($cast)) {
|
||||||
$a = $cast;
|
$a = $cast;
|
||||||
|
Reference in New Issue
Block a user