vendor/symfony/var-dumper/Cloner/AbstractCloner.php line 300

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\VarDumper\Cloner;
  11. use Symfony\Component\VarDumper\Caster\Caster;
  12. use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
  13. /**
  14.  * AbstractCloner implements a generic caster mechanism for objects and resources.
  15.  *
  16.  * @author Nicolas Grekas <p@tchwork.com>
  17.  */
  18. abstract class AbstractCloner implements ClonerInterface
  19. {
  20.     public static $defaultCasters = [
  21.         '__PHP_Incomplete_Class' => ['Symfony\Component\VarDumper\Caster\Caster''castPhpIncompleteClass'],
  22.         'Symfony\Component\VarDumper\Caster\CutStub' => ['Symfony\Component\VarDumper\Caster\StubCaster''castStub'],
  23.         'Symfony\Component\VarDumper\Caster\CutArrayStub' => ['Symfony\Component\VarDumper\Caster\StubCaster''castCutArray'],
  24.         'Symfony\Component\VarDumper\Caster\ConstStub' => ['Symfony\Component\VarDumper\Caster\StubCaster''castStub'],
  25.         'Symfony\Component\VarDumper\Caster\EnumStub' => ['Symfony\Component\VarDumper\Caster\StubCaster''castEnum'],
  26.         'Closure' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castClosure'],
  27.         'Generator' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castGenerator'],
  28.         'ReflectionType' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castType'],
  29.         'ReflectionGenerator' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castReflectionGenerator'],
  30.         'ReflectionClass' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castClass'],
  31.         'ReflectionFunctionAbstract' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castFunctionAbstract'],
  32.         'ReflectionMethod' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castMethod'],
  33.         'ReflectionParameter' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castParameter'],
  34.         'ReflectionProperty' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castProperty'],
  35.         'ReflectionReference' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castReference'],
  36.         'ReflectionExtension' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castExtension'],
  37.         'ReflectionZendExtension' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster''castZendExtension'],
  38.         'Doctrine\Common\Persistence\ObjectManager' => ['Symfony\Component\VarDumper\Caster\StubCaster''cutInternals'],
  39.         'Doctrine\Common\Proxy\Proxy' => ['Symfony\Component\VarDumper\Caster\DoctrineCaster''castCommonProxy'],
  40.         'Doctrine\ORM\Proxy\Proxy' => ['Symfony\Component\VarDumper\Caster\DoctrineCaster''castOrmProxy'],
  41.         'Doctrine\ORM\PersistentCollection' => ['Symfony\Component\VarDumper\Caster\DoctrineCaster''castPersistentCollection'],
  42.         'Doctrine\Persistence\ObjectManager' => ['Symfony\Component\VarDumper\Caster\StubCaster''cutInternals'],
  43.         'DOMException' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castException'],
  44.         'DOMStringList' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castLength'],
  45.         'DOMNameList' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castLength'],
  46.         'DOMImplementation' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castImplementation'],
  47.         'DOMImplementationList' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castLength'],
  48.         'DOMNode' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castNode'],
  49.         'DOMNameSpaceNode' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castNameSpaceNode'],
  50.         'DOMDocument' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castDocument'],
  51.         'DOMNodeList' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castLength'],
  52.         'DOMNamedNodeMap' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castLength'],
  53.         'DOMCharacterData' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castCharacterData'],
  54.         'DOMAttr' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castAttr'],
  55.         'DOMElement' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castElement'],
  56.         'DOMText' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castText'],
  57.         'DOMTypeinfo' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castTypeinfo'],
  58.         'DOMDomError' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castDomError'],
  59.         'DOMLocator' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castLocator'],
  60.         'DOMDocumentType' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castDocumentType'],
  61.         'DOMNotation' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castNotation'],
  62.         'DOMEntity' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castEntity'],
  63.         'DOMProcessingInstruction' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castProcessingInstruction'],
  64.         'DOMXPath' => ['Symfony\Component\VarDumper\Caster\DOMCaster''castXPath'],
  65.         'XMLReader' => ['Symfony\Component\VarDumper\Caster\XmlReaderCaster''castXmlReader'],
  66.         'ErrorException' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster''castErrorException'],
  67.         'Exception' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster''castException'],
  68.         'Error' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster''castError'],
  69.         'Symfony\Component\DependencyInjection\ContainerInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster''cutInternals'],
  70.         'Symfony\Component\EventDispatcher\EventDispatcherInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster''cutInternals'],
  71.         'Symfony\Component\HttpClient\CurlHttpClient' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster''castHttpClient'],
  72.         'Symfony\Component\HttpClient\NativeHttpClient' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster''castHttpClient'],
  73.         'Symfony\Component\HttpClient\Response\CurlResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster''castHttpClientResponse'],
  74.         'Symfony\Component\HttpClient\Response\NativeResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster''castHttpClientResponse'],
  75.         'Symfony\Component\HttpFoundation\Request' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster''castRequest'],
  76.         'Symfony\Component\VarDumper\Exception\ThrowingCasterException' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster''castThrowingCasterException'],
  77.         'Symfony\Component\VarDumper\Caster\TraceStub' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster''castTraceStub'],
  78.         'Symfony\Component\VarDumper\Caster\FrameStub' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster''castFrameStub'],
  79.         'Symfony\Component\VarDumper\Cloner\AbstractCloner' => ['Symfony\Component\VarDumper\Caster\StubCaster''cutInternals'],
  80.         'Symfony\Component\ErrorHandler\Exception\SilencedErrorContext' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster''castSilencedErrorContext'],
  81.         'Imagine\Image\ImageInterface' => ['Symfony\Component\VarDumper\Caster\ImagineCaster''castImage'],
  82.         'Ramsey\Uuid\UuidInterface' => ['Symfony\Component\VarDumper\Caster\UuidCaster''castRamseyUuid'],
  83.         'ProxyManager\Proxy\ProxyInterface' => ['Symfony\Component\VarDumper\Caster\ProxyManagerCaster''castProxy'],
  84.         'PHPUnit_Framework_MockObject_MockObject' => ['Symfony\Component\VarDumper\Caster\StubCaster''cutInternals'],
  85.         'PHPUnit\Framework\MockObject\MockObject' => ['Symfony\Component\VarDumper\Caster\StubCaster''cutInternals'],
  86.         'PHPUnit\Framework\MockObject\Stub' => ['Symfony\Component\VarDumper\Caster\StubCaster''cutInternals'],
  87.         'Prophecy\Prophecy\ProphecySubjectInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster''cutInternals'],
  88.         'Mockery\MockInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster''cutInternals'],
  89.         'PDO' => ['Symfony\Component\VarDumper\Caster\PdoCaster''castPdo'],
  90.         'PDOStatement' => ['Symfony\Component\VarDumper\Caster\PdoCaster''castPdoStatement'],
  91.         'AMQPConnection' => ['Symfony\Component\VarDumper\Caster\AmqpCaster''castConnection'],
  92.         'AMQPChannel' => ['Symfony\Component\VarDumper\Caster\AmqpCaster''castChannel'],
  93.         'AMQPQueue' => ['Symfony\Component\VarDumper\Caster\AmqpCaster''castQueue'],
  94.         'AMQPExchange' => ['Symfony\Component\VarDumper\Caster\AmqpCaster''castExchange'],
  95.         'AMQPEnvelope' => ['Symfony\Component\VarDumper\Caster\AmqpCaster''castEnvelope'],
  96.         'ArrayObject' => ['Symfony\Component\VarDumper\Caster\SplCaster''castArrayObject'],
  97.         'ArrayIterator' => ['Symfony\Component\VarDumper\Caster\SplCaster''castArrayIterator'],
  98.         'SplDoublyLinkedList' => ['Symfony\Component\VarDumper\Caster\SplCaster''castDoublyLinkedList'],
  99.         'SplFileInfo' => ['Symfony\Component\VarDumper\Caster\SplCaster''castFileInfo'],
  100.         'SplFileObject' => ['Symfony\Component\VarDumper\Caster\SplCaster''castFileObject'],
  101.         'SplHeap' => ['Symfony\Component\VarDumper\Caster\SplCaster''castHeap'],
  102.         'SplObjectStorage' => ['Symfony\Component\VarDumper\Caster\SplCaster''castObjectStorage'],
  103.         'SplPriorityQueue' => ['Symfony\Component\VarDumper\Caster\SplCaster''castHeap'],
  104.         'OuterIterator' => ['Symfony\Component\VarDumper\Caster\SplCaster''castOuterIterator'],
  105.         'WeakReference' => ['Symfony\Component\VarDumper\Caster\SplCaster''castWeakReference'],
  106.         'Redis' => ['Symfony\Component\VarDumper\Caster\RedisCaster''castRedis'],
  107.         'RedisArray' => ['Symfony\Component\VarDumper\Caster\RedisCaster''castRedisArray'],
  108.         'RedisCluster' => ['Symfony\Component\VarDumper\Caster\RedisCaster''castRedisCluster'],
  109.         'DateTimeInterface' => ['Symfony\Component\VarDumper\Caster\DateCaster''castDateTime'],
  110.         'DateInterval' => ['Symfony\Component\VarDumper\Caster\DateCaster''castInterval'],
  111.         'DateTimeZone' => ['Symfony\Component\VarDumper\Caster\DateCaster''castTimeZone'],
  112.         'DatePeriod' => ['Symfony\Component\VarDumper\Caster\DateCaster''castPeriod'],
  113.         'GMP' => ['Symfony\Component\VarDumper\Caster\GmpCaster''castGmp'],
  114.         'MessageFormatter' => ['Symfony\Component\VarDumper\Caster\IntlCaster''castMessageFormatter'],
  115.         'NumberFormatter' => ['Symfony\Component\VarDumper\Caster\IntlCaster''castNumberFormatter'],
  116.         'IntlTimeZone' => ['Symfony\Component\VarDumper\Caster\IntlCaster''castIntlTimeZone'],
  117.         'IntlCalendar' => ['Symfony\Component\VarDumper\Caster\IntlCaster''castIntlCalendar'],
  118.         'IntlDateFormatter' => ['Symfony\Component\VarDumper\Caster\IntlCaster''castIntlDateFormatter'],
  119.         'Memcached' => ['Symfony\Component\VarDumper\Caster\MemcachedCaster''castMemcached'],
  120.         'Ds\Collection' => ['Symfony\Component\VarDumper\Caster\DsCaster''castCollection'],
  121.         'Ds\Map' => ['Symfony\Component\VarDumper\Caster\DsCaster''castMap'],
  122.         'Ds\Pair' => ['Symfony\Component\VarDumper\Caster\DsCaster''castPair'],
  123.         'Symfony\Component\VarDumper\Caster\DsPairStub' => ['Symfony\Component\VarDumper\Caster\DsCaster''castPairStub'],
  124.         'mysqli_driver' => ['Symfony\Component\VarDumper\Caster\MysqliCaster''castMysqliDriver'],
  125.         'CurlHandle' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castCurl'],
  126.         ':curl' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castCurl'],
  127.         ':dba' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castDba'],
  128.         ':dba persistent' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castDba'],
  129.         'GdImage' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castGd'],
  130.         ':gd' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castGd'],
  131.         ':mysql link' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castMysqlLink'],
  132.         ':pgsql large object' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster''castLargeObject'],
  133.         ':pgsql link' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster''castLink'],
  134.         ':pgsql link persistent' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster''castLink'],
  135.         ':pgsql result' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster''castResult'],
  136.         ':process' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castProcess'],
  137.         ':stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castStream'],
  138.         'OpenSSLCertificate' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castOpensslX509'],
  139.         ':OpenSSL X.509' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castOpensslX509'],
  140.         ':persistent stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castStream'],
  141.         ':stream-context' => ['Symfony\Component\VarDumper\Caster\ResourceCaster''castStreamContext'],
  142.         'XmlParser' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster''castXml'],
  143.         ':xml' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster''castXml'],
  144.     ];
  145.     protected $maxItems 2500;
  146.     protected $maxString = -1;
  147.     protected $minDepth 1;
  148.     private $casters = [];
  149.     private $prevErrorHandler;
  150.     private $classInfo = [];
  151.     private $filter 0;
  152.     /**
  153.      * @param callable[]|null $casters A map of casters
  154.      *
  155.      * @see addCasters
  156.      */
  157.     public function __construct(array $casters null)
  158.     {
  159.         if (null === $casters) {
  160.             $casters = static::$defaultCasters;
  161.         }
  162.         $this->addCasters($casters);
  163.     }
  164.     /**
  165.      * Adds casters for resources and objects.
  166.      *
  167.      * Maps resources or objects types to a callback.
  168.      * Types are in the key, with a callable caster for value.
  169.      * Resource types are to be prefixed with a `:`,
  170.      * see e.g. static::$defaultCasters.
  171.      *
  172.      * @param callable[] $casters A map of casters
  173.      */
  174.     public function addCasters(array $casters)
  175.     {
  176.         foreach ($casters as $type => $callback) {
  177.             $this->casters[$type][] = $callback;
  178.         }
  179.     }
  180.     /**
  181.      * Sets the maximum number of items to clone past the minimum depth in nested structures.
  182.      *
  183.      * @param int $maxItems
  184.      */
  185.     public function setMaxItems($maxItems)
  186.     {
  187.         $this->maxItems = (int) $maxItems;
  188.     }
  189.     /**
  190.      * Sets the maximum cloned length for strings.
  191.      *
  192.      * @param int $maxString
  193.      */
  194.     public function setMaxString($maxString)
  195.     {
  196.         $this->maxString = (int) $maxString;
  197.     }
  198.     /**
  199.      * Sets the minimum tree depth where we are guaranteed to clone all the items.  After this
  200.      * depth is reached, only setMaxItems items will be cloned.
  201.      *
  202.      * @param int $minDepth
  203.      */
  204.     public function setMinDepth($minDepth)
  205.     {
  206.         $this->minDepth = (int) $minDepth;
  207.     }
  208.     /**
  209.      * Clones a PHP variable.
  210.      *
  211.      * @param mixed $var    Any PHP variable
  212.      * @param int   $filter A bit field of Caster::EXCLUDE_* constants
  213.      *
  214.      * @return Data The cloned variable represented by a Data object
  215.      */
  216.     public function cloneVar($var$filter 0)
  217.     {
  218.         $this->prevErrorHandler set_error_handler(function ($type$msg$file$line$context = []) {
  219.             if (\E_RECOVERABLE_ERROR === $type || \E_USER_ERROR === $type) {
  220.                 // Cloner never dies
  221.                 throw new \ErrorException($msg0$type$file$line);
  222.             }
  223.             if ($this->prevErrorHandler) {
  224.                 return ($this->prevErrorHandler)($type$msg$file$line$context);
  225.             }
  226.             return false;
  227.         });
  228.         $this->filter $filter;
  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.      * Effectively clones the PHP variable.
  244.      *
  245.      * @param mixed $var Any PHP variable
  246.      *
  247.      * @return array The cloned variable represented in an array
  248.      */
  249.     abstract protected function doClone($var);
  250.     /**
  251.      * Casts an object to an array representation.
  252.      *
  253.      * @param bool $isNested True if the object is nested in the dumped structure
  254.      *
  255.      * @return array The object casted as array
  256.      */
  257.     protected function castObject(Stub $stub$isNested)
  258.     {
  259.         $obj $stub->value;
  260.         $class $stub->class;
  261.         if (\PHP_VERSION_ID 80000 "\0" === ($class[15] ?? null) : str_contains($class"@anonymous\0")) {
  262.             $stub->class get_debug_type($obj);
  263.         }
  264.         if (isset($this->classInfo[$class])) {
  265.             [$i$parents$hasDebugInfo$fileInfo] = $this->classInfo[$class];
  266.         } else {
  267.             $i 2;
  268.             $parents = [$class];
  269.             $hasDebugInfo method_exists($class'__debugInfo');
  270.             foreach (class_parents($class) as $p) {
  271.                 $parents[] = $p;
  272.                 ++$i;
  273.             }
  274.             foreach (class_implements($class) as $p) {
  275.                 $parents[] = $p;
  276.                 ++$i;
  277.             }
  278.             $parents[] = '*';
  279.             $r = new \ReflectionClass($class);
  280.             $fileInfo $r->isInternal() || $r->isSubclassOf(Stub::class) ? [] : [
  281.                 'file' => $r->getFileName(),
  282.                 'line' => $r->getStartLine(),
  283.             ];
  284.             $this->classInfo[$class] = [$i$parents$hasDebugInfo$fileInfo];
  285.         }
  286.         $stub->attr += $fileInfo;
  287.         $a Caster::castObject($obj$class$hasDebugInfo$stub->class);
  288.         try {
  289.             while ($i--) {
  290.                 if (!empty($this->casters[$p $parents[$i]])) {
  291.                     foreach ($this->casters[$p] as $callback) {
  292.                         $a $callback($obj$a$stub$isNested$this->filter);
  293.                     }
  294.                 }
  295.             }
  296.         } catch (\Exception $e) {
  297.             $a = [(Stub::TYPE_OBJECT === $stub->type Caster::PREFIX_VIRTUAL '').'⚠' => new ThrowingCasterException($e)] + $a;
  298.         }
  299.         return $a;
  300.     }
  301.     /**
  302.      * Casts a resource to an array representation.
  303.      *
  304.      * @param bool $isNested True if the object is nested in the dumped structure
  305.      *
  306.      * @return array The resource casted as array
  307.      */
  308.     protected function castResource(Stub $stub$isNested)
  309.     {
  310.         $a = [];
  311.         $res $stub->value;
  312.         $type $stub->class;
  313.         try {
  314.             if (!empty($this->casters[':'.$type])) {
  315.                 foreach ($this->casters[':'.$type] as $callback) {
  316.                     $a $callback($res$a$stub$isNested$this->filter);
  317.                 }
  318.             }
  319.         } catch (\Exception $e) {
  320.             $a = [(Stub::TYPE_OBJECT === $stub->type Caster::PREFIX_VIRTUAL '').'⚠' => new ThrowingCasterException($e)] + $a;
  321.         }
  322.         return $a;
  323.     }
  324. }