Drupal investigation

AbstractCloner.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 = array(
  21. 'Symfony\Component\VarDumper\Caster\CutStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
  22. 'Symfony\Component\VarDumper\Caster\CutArrayStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castCutArray',
  23. 'Symfony\Component\VarDumper\Caster\ConstStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
  24. 'Symfony\Component\VarDumper\Caster\EnumStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castEnum',
  25. 'Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure',
  26. 'Generator' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castGenerator',
  27. 'ReflectionType' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castType',
  28. 'ReflectionGenerator' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castReflectionGenerator',
  29. 'ReflectionClass' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClass',
  30. 'ReflectionFunctionAbstract' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castFunctionAbstract',
  31. 'ReflectionMethod' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castMethod',
  32. 'ReflectionParameter' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castParameter',
  33. 'ReflectionProperty' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castProperty',
  34. 'ReflectionExtension' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castExtension',
  35. 'ReflectionZendExtension' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castZendExtension',
  36. 'Doctrine\Common\Persistence\ObjectManager' => 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals',
  37. 'Doctrine\Common\Proxy\Proxy' => 'Symfony\Component\VarDumper\Caster\DoctrineCaster::castCommonProxy',
  38. 'Doctrine\ORM\Proxy\Proxy' => 'Symfony\Component\VarDumper\Caster\DoctrineCaster::castOrmProxy',
  39. 'Doctrine\ORM\PersistentCollection' => 'Symfony\Component\VarDumper\Caster\DoctrineCaster::castPersistentCollection',
  40. 'DOMException' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castException',
  41. 'DOMStringList' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castLength',
  42. 'DOMNameList' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castLength',
  43. 'DOMImplementation' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castImplementation',
  44. 'DOMImplementationList' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castLength',
  45. 'DOMNode' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castNode',
  46. 'DOMNameSpaceNode' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castNameSpaceNode',
  47. 'DOMDocument' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castDocument',
  48. 'DOMNodeList' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castLength',
  49. 'DOMNamedNodeMap' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castLength',
  50. 'DOMCharacterData' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castCharacterData',
  51. 'DOMAttr' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castAttr',
  52. 'DOMElement' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castElement',
  53. 'DOMText' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castText',
  54. 'DOMTypeinfo' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castTypeinfo',
  55. 'DOMDomError' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castDomError',
  56. 'DOMLocator' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castLocator',
  57. 'DOMDocumentType' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castDocumentType',
  58. 'DOMNotation' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castNotation',
  59. 'DOMEntity' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castEntity',
  60. 'DOMProcessingInstruction' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castProcessingInstruction',
  61. 'DOMXPath' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castXPath',
  62. 'ErrorException' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castErrorException',
  63. 'Exception' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castException',
  64. 'Error' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castError',
  65. 'Symfony\Component\DependencyInjection\ContainerInterface' => 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals',
  66. 'Symfony\Component\VarDumper\Exception\ThrowingCasterException' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castThrowingCasterException',
  67. 'Symfony\Component\VarDumper\Caster\TraceStub' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castTraceStub',
  68. 'Symfony\Component\VarDumper\Caster\FrameStub' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castFrameStub',
  69. 'PHPUnit_Framework_MockObject_MockObject' => 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals',
  70. 'Prophecy\Prophecy\ProphecySubjectInterface' => 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals',
  71. 'Mockery\MockInterface' => 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals',
  72. 'PDO' => 'Symfony\Component\VarDumper\Caster\PdoCaster::castPdo',
  73. 'PDOStatement' => 'Symfony\Component\VarDumper\Caster\PdoCaster::castPdoStatement',
  74. 'AMQPConnection' => 'Symfony\Component\VarDumper\Caster\AmqpCaster::castConnection',
  75. 'AMQPChannel' => 'Symfony\Component\VarDumper\Caster\AmqpCaster::castChannel',
  76. 'AMQPQueue' => 'Symfony\Component\VarDumper\Caster\AmqpCaster::castQueue',
  77. 'AMQPExchange' => 'Symfony\Component\VarDumper\Caster\AmqpCaster::castExchange',
  78. 'AMQPEnvelope' => 'Symfony\Component\VarDumper\Caster\AmqpCaster::castEnvelope',
  79. 'ArrayObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castArrayObject',
  80. 'SplDoublyLinkedList' => 'Symfony\Component\VarDumper\Caster\SplCaster::castDoublyLinkedList',
  81. 'SplFileInfo' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileInfo',
  82. 'SplFileObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileObject',
  83. 'SplFixedArray' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFixedArray',
  84. 'SplHeap' => 'Symfony\Component\VarDumper\Caster\SplCaster::castHeap',
  85. 'SplObjectStorage' => 'Symfony\Component\VarDumper\Caster\SplCaster::castObjectStorage',
  86. 'SplPriorityQueue' => 'Symfony\Component\VarDumper\Caster\SplCaster::castHeap',
  87. 'OuterIterator' => 'Symfony\Component\VarDumper\Caster\SplCaster::castOuterIterator',
  88. 'MongoCursorInterface' => 'Symfony\Component\VarDumper\Caster\MongoCaster::castCursor',
  89. ':curl' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castCurl',
  90. ':dba' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
  91. ':dba persistent' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
  92. ':gd' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castGd',
  93. ':mysql link' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castMysqlLink',
  94. ':pgsql large object' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castLargeObject',
  95. ':pgsql link' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castLink',
  96. ':pgsql link persistent' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castLink',
  97. ':pgsql result' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castResult',
  98. ':process' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castProcess',
  99. ':stream' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStream',
  100. ':persistent stream' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStream',
  101. ':stream-context' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStreamContext',
  102. ':xml' => 'Symfony\Component\VarDumper\Caster\XmlResourceCaster::castXml',
  103. );
  104. protected $maxItems = 2500;
  105. protected $maxString = -1;
  106. protected $useExt;
  107. private $casters = array();
  108. private $prevErrorHandler;
  109. private $classInfo = array();
  110. private $filter = 0;
  111. /**
  112. * @param callable[]|null $casters A map of casters
  113. *
  114. * @see addCasters
  115. */
  116. public function __construct(array $casters = null)
  117. {
  118. if (null === $casters) {
  119. $casters = static::$defaultCasters;
  120. }
  121. $this->addCasters($casters);
  122. $this->useExt = extension_loaded('symfony_debug');
  123. }
  124. /**
  125. * Adds casters for resources and objects.
  126. *
  127. * Maps resources or objects types to a callback.
  128. * Types are in the key, with a callable caster for value.
  129. * Resource types are to be prefixed with a `:`,
  130. * see e.g. static::$defaultCasters.
  131. *
  132. * @param callable[] $casters A map of casters
  133. */
  134. public function addCasters(array $casters)
  135. {
  136. foreach ($casters as $type => $callback) {
  137. $this->casters[strtolower($type)][] = $callback;
  138. }
  139. }
  140. /**
  141. * Sets the maximum number of items to clone past the first level in nested structures.
  142. *
  143. * @param int $maxItems
  144. */
  145. public function setMaxItems($maxItems)
  146. {
  147. $this->maxItems = (int) $maxItems;
  148. }
  149. /**
  150. * Sets the maximum cloned length for strings.
  151. *
  152. * @param int $maxString
  153. */
  154. public function setMaxString($maxString)
  155. {
  156. $this->maxString = (int) $maxString;
  157. }
  158. /**
  159. * Clones a PHP variable.
  160. *
  161. * @param mixed $var Any PHP variable
  162. * @param int $filter A bit field of Caster::EXCLUDE_* constants
  163. *
  164. * @return Data The cloned variable represented by a Data object
  165. */
  166. public function cloneVar($var, $filter = 0)
  167. {
  168. $this->filter = $filter;
  169. $this->prevErrorHandler = set_error_handler(array($this, 'handleError'));
  170. try {
  171. $data = $this->doClone($var);
  172. } catch (\Exception $e) {
  173. }
  174. restore_error_handler();
  175. $this->prevErrorHandler = null;
  176. if (isset($e)) {
  177. throw $e;
  178. }
  179. return new Data($data);
  180. }
  181. /**
  182. * Effectively clones the PHP variable.
  183. *
  184. * @param mixed $var Any PHP variable
  185. *
  186. * @return array The cloned variable represented in an array
  187. */
  188. abstract protected function doClone($var);
  189. /**
  190. * Casts an object to an array representation.
  191. *
  192. * @param Stub $stub The Stub for the casted object
  193. * @param bool $isNested True if the object is nested in the dumped structure
  194. *
  195. * @return array The object casted as array
  196. */
  197. protected function castObject(Stub $stub, $isNested)
  198. {
  199. $obj = $stub->value;
  200. $class = $stub->class;
  201. if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) {
  202. $stub->class = get_parent_class($class).'@anonymous';
  203. }
  204. if (isset($this->classInfo[$class])) {
  205. $classInfo = $this->classInfo[$class];
  206. } else {
  207. $classInfo = array(
  208. new \ReflectionClass($class),
  209. array_reverse(array($class => $class) + class_parents($class) + class_implements($class) + array('*' => '*')),
  210. );
  211. $this->classInfo[$class] = $classInfo;
  212. }
  213. $a = $this->callCaster('Symfony\Component\VarDumper\Caster\Caster::castObject', $obj, $classInfo[0], null, $isNested);
  214. foreach ($classInfo[1] as $p) {
  215. if (!empty($this->casters[$p = strtolower($p)])) {
  216. foreach ($this->casters[$p] as $p) {
  217. $a = $this->callCaster($p, $obj, $a, $stub, $isNested);
  218. }
  219. }
  220. }
  221. return $a;
  222. }
  223. /**
  224. * Casts a resource to an array representation.
  225. *
  226. * @param Stub $stub The Stub for the casted resource
  227. * @param bool $isNested True if the object is nested in the dumped structure
  228. *
  229. * @return array The resource casted as array
  230. */
  231. protected function castResource(Stub $stub, $isNested)
  232. {
  233. $a = array();
  234. $res = $stub->value;
  235. $type = $stub->class;
  236. if (!empty($this->casters[':'.$type])) {
  237. foreach ($this->casters[':'.$type] as $c) {
  238. $a = $this->callCaster($c, $res, $a, $stub, $isNested);
  239. }
  240. }
  241. return $a;
  242. }
  243. /**
  244. * Calls a custom caster.
  245. *
  246. * @param callable $callback The caster
  247. * @param object|resource $obj The object/resource being casted
  248. * @param array $a The result of the previous cast for chained casters
  249. * @param Stub $stub The Stub for the casted object/resource
  250. * @param bool $isNested True if $obj is nested in the dumped structure
  251. *
  252. * @return array The casted object/resource
  253. */
  254. private function callCaster($callback, $obj, $a, $stub, $isNested)
  255. {
  256. try {
  257. $cast = call_user_func($callback, $obj, $a, $stub, $isNested, $this->filter);
  258. if (is_array($cast)) {
  259. $a = $cast;
  260. }
  261. } catch (\Exception $e) {
  262. $a[(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠'] = new ThrowingCasterException($e);
  263. }
  264. return $a;
  265. }
  266. /**
  267. * Special handling for errors: cloning must be fail-safe.
  268. *
  269. * @internal
  270. */
  271. public function handleError($type, $msg, $file, $line, $context)
  272. {
  273. if (E_RECOVERABLE_ERROR === $type || E_USER_ERROR === $type) {
  274. // Cloner never dies
  275. throw new \ErrorException($msg, 0, $type, $file, $line);
  276. }
  277. if ($this->prevErrorHandler) {
  278. return call_user_func($this->prevErrorHandler, $type, $msg, $file, $line, $context);
  279. }
  280. return false;
  281. }
  282. }