4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Cloner;
14 use Symfony\Component\VarDumper\Caster\Caster;
15 use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
18 * AbstractCloner implements a generic caster mechanism for objects and resources.
20 * @author Nicolas Grekas <p@tchwork.com>
22 abstract class AbstractCloner implements ClonerInterface
24 public static $defaultCasters = array(
25 '__PHP_Incomplete_Class' => array('Symfony\Component\VarDumper\Caster\Caster', 'castPhpIncompleteClass'),
27 'Symfony\Component\VarDumper\Caster\CutStub' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'castStub'),
28 'Symfony\Component\VarDumper\Caster\CutArrayStub' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'castCutArray'),
29 'Symfony\Component\VarDumper\Caster\ConstStub' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'castStub'),
30 'Symfony\Component\VarDumper\Caster\EnumStub' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'castEnum'),
32 'Closure' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClosure'),
33 'Generator' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castGenerator'),
34 'ReflectionType' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castType'),
35 'ReflectionGenerator' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castReflectionGenerator'),
36 'ReflectionClass' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClass'),
37 'ReflectionFunctionAbstract' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castFunctionAbstract'),
38 'ReflectionMethod' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castMethod'),
39 'ReflectionParameter' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castParameter'),
40 'ReflectionProperty' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castProperty'),
41 'ReflectionExtension' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castExtension'),
42 'ReflectionZendExtension' => array('Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castZendExtension'),
44 'Doctrine\Common\Persistence\ObjectManager' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'),
45 'Doctrine\Common\Proxy\Proxy' => array('Symfony\Component\VarDumper\Caster\DoctrineCaster', 'castCommonProxy'),
46 'Doctrine\ORM\Proxy\Proxy' => array('Symfony\Component\VarDumper\Caster\DoctrineCaster', 'castOrmProxy'),
47 'Doctrine\ORM\PersistentCollection' => array('Symfony\Component\VarDumper\Caster\DoctrineCaster', 'castPersistentCollection'),
49 'DOMException' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castException'),
50 'DOMStringList' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castLength'),
51 'DOMNameList' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castLength'),
52 'DOMImplementation' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castImplementation'),
53 'DOMImplementationList' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castLength'),
54 'DOMNode' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castNode'),
55 'DOMNameSpaceNode' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castNameSpaceNode'),
56 'DOMDocument' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castDocument'),
57 'DOMNodeList' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castLength'),
58 'DOMNamedNodeMap' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castLength'),
59 'DOMCharacterData' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castCharacterData'),
60 'DOMAttr' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castAttr'),
61 'DOMElement' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castElement'),
62 'DOMText' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castText'),
63 'DOMTypeinfo' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castTypeinfo'),
64 'DOMDomError' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castDomError'),
65 'DOMLocator' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castLocator'),
66 'DOMDocumentType' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castDocumentType'),
67 'DOMNotation' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castNotation'),
68 'DOMEntity' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castEntity'),
69 'DOMProcessingInstruction' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castProcessingInstruction'),
70 'DOMXPath' => array('Symfony\Component\VarDumper\Caster\DOMCaster', 'castXPath'),
72 'XmlReader' => array('Symfony\Component\VarDumper\Caster\XmlReaderCaster', 'castXmlReader'),
74 'ErrorException' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castErrorException'),
75 'Exception' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castException'),
76 'Error' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castError'),
77 'Symfony\Component\DependencyInjection\ContainerInterface' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'),
78 'Symfony\Component\HttpFoundation\Request' => array('Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castRequest'),
79 'Symfony\Component\VarDumper\Exception\ThrowingCasterException' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castThrowingCasterException'),
80 'Symfony\Component\VarDumper\Caster\TraceStub' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castTraceStub'),
81 'Symfony\Component\VarDumper\Caster\FrameStub' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castFrameStub'),
82 'Symfony\Component\Debug\Exception\SilencedErrorContext' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castSilencedErrorContext'),
84 'PHPUnit_Framework_MockObject_MockObject' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'),
85 'Prophecy\Prophecy\ProphecySubjectInterface' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'),
86 'Mockery\MockInterface' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'),
88 'PDO' => array('Symfony\Component\VarDumper\Caster\PdoCaster', 'castPdo'),
89 'PDOStatement' => array('Symfony\Component\VarDumper\Caster\PdoCaster', 'castPdoStatement'),
91 'AMQPConnection' => array('Symfony\Component\VarDumper\Caster\AmqpCaster', 'castConnection'),
92 'AMQPChannel' => array('Symfony\Component\VarDumper\Caster\AmqpCaster', 'castChannel'),
93 'AMQPQueue' => array('Symfony\Component\VarDumper\Caster\AmqpCaster', 'castQueue'),
94 'AMQPExchange' => array('Symfony\Component\VarDumper\Caster\AmqpCaster', 'castExchange'),
95 'AMQPEnvelope' => array('Symfony\Component\VarDumper\Caster\AmqpCaster', 'castEnvelope'),
97 'ArrayObject' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castArrayObject'),
98 'ArrayIterator' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castArrayIterator'),
99 'SplDoublyLinkedList' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castDoublyLinkedList'),
100 'SplFileInfo' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castFileInfo'),
101 'SplFileObject' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castFileObject'),
102 'SplFixedArray' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castFixedArray'),
103 'SplHeap' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'),
104 'SplObjectStorage' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castObjectStorage'),
105 'SplPriorityQueue' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'),
106 'OuterIterator' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castOuterIterator'),
108 'MongoCursorInterface' => array('Symfony\Component\VarDumper\Caster\MongoCaster', 'castCursor'),
110 'Redis' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedis'),
111 'RedisArray' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'),
113 'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'),
114 'DateInterval' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'),
115 'DateTimeZone' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castTimeZone'),
116 'DatePeriod' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castPeriod'),
118 ':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
119 ':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
120 ':dba persistent' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
121 ':gd' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castGd'),
122 ':mysql link' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castMysqlLink'),
123 ':pgsql large object' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLargeObject'),
124 ':pgsql link' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'),
125 ':pgsql link persistent' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'),
126 ':pgsql result' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'),
127 ':process' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castProcess'),
128 ':stream' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'),
129 ':persistent stream' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'),
130 ':stream-context' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'),
131 ':xml' => array('Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'),
134 protected $maxItems = 2500;
135 protected $maxString = -1;
136 protected $minDepth = 1;
139 private $casters = array();
140 private $prevErrorHandler;
141 private $classInfo = array();
145 * @param callable[]|null $casters A map of casters
149 public function __construct(array $casters = null)
151 if (null === $casters) {
152 $casters = static::$defaultCasters;
154 $this->addCasters($casters);
155 $this->useExt = \extension_loaded('symfony_debug');
159 * Adds casters for resources and objects.
161 * Maps resources or objects types to a callback.
162 * Types are in the key, with a callable caster for value.
163 * Resource types are to be prefixed with a `:`,
164 * see e.g. static::$defaultCasters.
166 * @param callable[] $casters A map of casters
168 public function addCasters(array $casters)
170 foreach ($casters as $type => $callback) {
171 $this->casters[strtolower($type)][] = \is_string($callback) && false !== strpos($callback, '::') ? explode('::', $callback, 2) : $callback;
176 * Sets the maximum number of items to clone past the minimum depth in nested structures.
178 * @param int $maxItems
180 public function setMaxItems($maxItems)
182 $this->maxItems = (int) $maxItems;
186 * Sets the maximum cloned length for strings.
188 * @param int $maxString
190 public function setMaxString($maxString)
192 $this->maxString = (int) $maxString;
196 * Sets the minimum tree depth where we are guaranteed to clone all the items. After this
197 * depth is reached, only setMaxItems items will be cloned.
199 * @param int $minDepth
201 public function setMinDepth($minDepth)
203 $this->minDepth = (int) $minDepth;
207 * Clones a PHP variable.
209 * @param mixed $var Any PHP variable
210 * @param int $filter A bit field of Caster::EXCLUDE_* constants
212 * @return Data The cloned variable represented by a Data object
214 public function cloneVar($var, $filter = 0)
216 $this->prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) {
217 if (E_RECOVERABLE_ERROR === $type || E_USER_ERROR === $type) {
219 throw new \ErrorException($msg, 0, $type, $file, $line);
222 if ($this->prevErrorHandler) {
223 return \call_user_func($this->prevErrorHandler, $type, $msg, $file, $line, $context);
228 $this->filter = $filter;
230 if ($gc = gc_enabled()) {
234 return new Data($this->doClone($var));
239 restore_error_handler();
240 $this->prevErrorHandler = null;
245 * Effectively clones the PHP variable.
247 * @param mixed $var Any PHP variable
249 * @return array The cloned variable represented in an array
251 abstract protected function doClone($var);
254 * Casts an object to an array representation.
256 * @param Stub $stub The Stub for the casted object
257 * @param bool $isNested True if the object is nested in the dumped structure
259 * @return array The object casted as array
261 protected function castObject(Stub $stub, $isNested)
264 $class = $stub->class;
266 if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) {
267 $stub->class = get_parent_class($class).'@anonymous';
269 if (isset($this->classInfo[$class])) {
270 list($i, $parents, $hasDebugInfo) = $this->classInfo[$class];
273 $parents = array(strtolower($class));
274 $hasDebugInfo = method_exists($class, '__debugInfo');
276 foreach (class_parents($class) as $p) {
277 $parents[] = strtolower($p);
280 foreach (class_implements($class) as $p) {
281 $parents[] = strtolower($p);
286 $this->classInfo[$class] = array($i, $parents, $hasDebugInfo);
289 $a = Caster::castObject($obj, $class, $hasDebugInfo);
293 if (!empty($this->casters[$p = $parents[$i]])) {
294 foreach ($this->casters[$p] as $callback) {
295 $a = $callback($obj, $a, $stub, $isNested, $this->filter);
299 } catch (\Exception $e) {
300 $a = array((Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)) + $a;
307 * Casts a resource to an array representation.
309 * @param Stub $stub The Stub for the casted resource
310 * @param bool $isNested True if the object is nested in the dumped structure
312 * @return array The resource casted as array
314 protected function castResource(Stub $stub, $isNested)
318 $type = $stub->class;
321 if (!empty($this->casters[':'.$type])) {
322 foreach ($this->casters[':'.$type] as $callback) {
323 $a = $callback($res, $a, $stub, $isNested, $this->filter);
326 } catch (\Exception $e) {
327 $a = array((Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)) + $a;