Security update to Drupal 8.4.6
[yaffs-website] / vendor / symfony / var-dumper / Cloner / AbstractCloner.php
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\Cloner;
13
14 use Symfony\Component\VarDumper\Caster\Caster;
15 use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
16
17 /**
18  * AbstractCloner implements a generic caster mechanism for objects and resources.
19  *
20  * @author Nicolas Grekas <p@tchwork.com>
21  */
22 abstract class AbstractCloner implements ClonerInterface
23 {
24     public static $defaultCasters = array(
25         '__PHP_Incomplete_Class' => array('Symfony\Component\VarDumper\Caster\Caster', 'castPhpIncompleteClass'),
26
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'),
31
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'),
43
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'),
48
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'),
71
72         'XmlReader' => array('Symfony\Component\VarDumper\Caster\XmlReaderCaster', 'castXmlReader'),
73
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'),
83
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'),
87
88         'PDO' => array('Symfony\Component\VarDumper\Caster\PdoCaster', 'castPdo'),
89         'PDOStatement' => array('Symfony\Component\VarDumper\Caster\PdoCaster', 'castPdoStatement'),
90
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'),
96
97         'ArrayObject' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castArrayObject'),
98         'SplDoublyLinkedList' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castDoublyLinkedList'),
99         'SplFileInfo' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castFileInfo'),
100         'SplFileObject' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castFileObject'),
101         'SplFixedArray' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castFixedArray'),
102         'SplHeap' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'),
103         'SplObjectStorage' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castObjectStorage'),
104         'SplPriorityQueue' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'),
105         'OuterIterator' => array('Symfony\Component\VarDumper\Caster\SplCaster', 'castOuterIterator'),
106
107         'MongoCursorInterface' => array('Symfony\Component\VarDumper\Caster\MongoCaster', 'castCursor'),
108
109         'Redis' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedis'),
110         'RedisArray' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'),
111
112         'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'),
113         'DateInterval' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'),
114         'DateTimeZone' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castTimeZone'),
115         'DatePeriod' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castPeriod'),
116
117         ':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
118         ':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
119         ':dba persistent' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
120         ':gd' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castGd'),
121         ':mysql link' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castMysqlLink'),
122         ':pgsql large object' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLargeObject'),
123         ':pgsql link' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'),
124         ':pgsql link persistent' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'),
125         ':pgsql result' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'),
126         ':process' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castProcess'),
127         ':stream' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'),
128         ':persistent stream' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'),
129         ':stream-context' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'),
130         ':xml' => array('Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'),
131     );
132
133     protected $maxItems = 2500;
134     protected $maxString = -1;
135     protected $minDepth = 1;
136     protected $useExt;
137
138     private $casters = array();
139     private $prevErrorHandler;
140     private $classInfo = array();
141     private $filter = 0;
142
143     /**
144      * @param callable[]|null $casters A map of casters
145      *
146      * @see addCasters
147      */
148     public function __construct(array $casters = null)
149     {
150         if (null === $casters) {
151             $casters = static::$defaultCasters;
152         }
153         $this->addCasters($casters);
154         $this->useExt = extension_loaded('symfony_debug');
155     }
156
157     /**
158      * Adds casters for resources and objects.
159      *
160      * Maps resources or objects types to a callback.
161      * Types are in the key, with a callable caster for value.
162      * Resource types are to be prefixed with a `:`,
163      * see e.g. static::$defaultCasters.
164      *
165      * @param callable[] $casters A map of casters
166      */
167     public function addCasters(array $casters)
168     {
169         foreach ($casters as $type => $callback) {
170             $this->casters[strtolower($type)][] = is_string($callback) && false !== strpos($callback, '::') ? explode('::', $callback, 2) : $callback;
171         }
172     }
173
174     /**
175      * Sets the maximum number of items to clone past the minimum depth in nested structures.
176      *
177      * @param int $maxItems
178      */
179     public function setMaxItems($maxItems)
180     {
181         $this->maxItems = (int) $maxItems;
182     }
183
184     /**
185      * Sets the maximum cloned length for strings.
186      *
187      * @param int $maxString
188      */
189     public function setMaxString($maxString)
190     {
191         $this->maxString = (int) $maxString;
192     }
193
194     /**
195      * Sets the minimum tree depth where we are guaranteed to clone all the items.  After this
196      * depth is reached, only setMaxItems items will be cloned.
197      *
198      * @param int $minDepth
199      */
200     public function setMinDepth($minDepth)
201     {
202         $this->minDepth = (int) $minDepth;
203     }
204
205     /**
206      * Clones a PHP variable.
207      *
208      * @param mixed $var    Any PHP variable
209      * @param int   $filter A bit field of Caster::EXCLUDE_* constants
210      *
211      * @return Data The cloned variable represented by a Data object
212      */
213     public function cloneVar($var, $filter = 0)
214     {
215         $this->prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) {
216             if (E_RECOVERABLE_ERROR === $type || E_USER_ERROR === $type) {
217                 // Cloner never dies
218                 throw new \ErrorException($msg, 0, $type, $file, $line);
219             }
220
221             if ($this->prevErrorHandler) {
222                 return call_user_func($this->prevErrorHandler, $type, $msg, $file, $line, $context);
223             }
224
225             return false;
226         });
227         $this->filter = $filter;
228
229         if ($gc = gc_enabled()) {
230             gc_disable();
231         }
232         try {
233             return new Data($this->doClone($var));
234         } finally {
235             if ($gc) {
236                 gc_enable();
237             }
238             restore_error_handler();
239             $this->prevErrorHandler = null;
240         }
241     }
242
243     /**
244      * Effectively clones the PHP variable.
245      *
246      * @param mixed $var Any PHP variable
247      *
248      * @return array The cloned variable represented in an array
249      */
250     abstract protected function doClone($var);
251
252     /**
253      * Casts an object to an array representation.
254      *
255      * @param Stub $stub     The Stub for the casted object
256      * @param bool $isNested True if the object is nested in the dumped structure
257      *
258      * @return array The object casted as array
259      */
260     protected function castObject(Stub $stub, $isNested)
261     {
262         $obj = $stub->value;
263         $class = $stub->class;
264
265         if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) {
266             $stub->class = get_parent_class($class).'@anonymous';
267         }
268         if (isset($this->classInfo[$class])) {
269             list($i, $parents, $hasDebugInfo) = $this->classInfo[$class];
270         } else {
271             $i = 2;
272             $parents = array(strtolower($class));
273             $hasDebugInfo = method_exists($class, '__debugInfo');
274
275             foreach (class_parents($class) as $p) {
276                 $parents[] = strtolower($p);
277                 ++$i;
278             }
279             foreach (class_implements($class) as $p) {
280                 $parents[] = strtolower($p);
281                 ++$i;
282             }
283             $parents[] = '*';
284
285             $this->classInfo[$class] = array($i, $parents, $hasDebugInfo);
286         }
287
288         $a = Caster::castObject($obj, $class, $hasDebugInfo);
289
290         try {
291             while ($i--) {
292                 if (!empty($this->casters[$p = $parents[$i]])) {
293                     foreach ($this->casters[$p] as $callback) {
294                         $a = $callback($obj, $a, $stub, $isNested, $this->filter);
295                     }
296                 }
297             }
298         } catch (\Exception $e) {
299             $a = array((Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)) + $a;
300         }
301
302         return $a;
303     }
304
305     /**
306      * Casts a resource to an array representation.
307      *
308      * @param Stub $stub     The Stub for the casted resource
309      * @param bool $isNested True if the object is nested in the dumped structure
310      *
311      * @return array The resource casted as array
312      */
313     protected function castResource(Stub $stub, $isNested)
314     {
315         $a = array();
316         $res = $stub->value;
317         $type = $stub->class;
318
319         try {
320             if (!empty($this->casters[':'.$type])) {
321                 foreach ($this->casters[':'.$type] as $callback) {
322                     $a = $callback($res, $a, $stub, $isNested, $this->filter);
323                 }
324             }
325         } catch (\Exception $e) {
326             $a = array((Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)) + $a;
327         }
328
329         return $a;
330     }
331 }