Drupal investigation

ReflectionCaster.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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\Caster;
  11. use Symfony\Component\VarDumper\Cloner\Stub;
  12. /**
  13. * Casts Reflector related classes to array representation.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class ReflectionCaster
  18. {
  19. private static $extraMap = array(
  20. 'docComment' => 'getDocComment',
  21. 'extension' => 'getExtensionName',
  22. 'isDisabled' => 'isDisabled',
  23. 'isDeprecated' => 'isDeprecated',
  24. 'isInternal' => 'isInternal',
  25. 'isUserDefined' => 'isUserDefined',
  26. 'isGenerator' => 'isGenerator',
  27. 'isVariadic' => 'isVariadic',
  28. );
  29. /**
  30. * @deprecated since Symfony 2.7, to be removed in 3.0.
  31. */
  32. public static function castReflector(\Reflector $c, array $a, Stub $stub, $isNested)
  33. {
  34. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
  35. $a[Caster::PREFIX_VIRTUAL.'reflection'] = $c->__toString();
  36. return $a;
  37. }
  38. public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
  39. {
  40. $prefix = Caster::PREFIX_VIRTUAL;
  41. $c = new \ReflectionFunction($c);
  42. $stub->class = 'Closure'; // HHVM generates unique class names for closures
  43. $a = static::castFunctionAbstract($c, $a, $stub, $isNested);
  44. if (isset($a[$prefix.'parameters'])) {
  45. foreach ($a[$prefix.'parameters']->value as &$v) {
  46. $param = $v;
  47. $v = new EnumStub(array());
  48. foreach (static::castParameter($param, array(), $stub, true) as $k => $param) {
  49. if ("\0" === $k[0]) {
  50. $v->value[substr($k, 3)] = $param;
  51. }
  52. }
  53. unset($v->value['position'], $v->value['isVariadic'], $v->value['byReference'], $v);
  54. }
  55. }
  56. if ($f = $c->getFileName()) {
  57. $a[$prefix.'file'] = $f;
  58. $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
  59. }
  60. $prefix = Caster::PREFIX_DYNAMIC;
  61. unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
  62. return $a;
  63. }
  64. public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested)
  65. {
  66. if (!class_exists('ReflectionGenerator', false)) {
  67. return $a;
  68. }
  69. // Cannot create ReflectionGenerator based on a terminated Generator
  70. try {
  71. $reflectionGenerator = new \ReflectionGenerator($c);
  72. } catch (\Exception $e) {
  73. $a[Caster::PREFIX_VIRTUAL.'closed'] = true;
  74. return $a;
  75. }
  76. return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested);
  77. }
  78. public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
  79. {
  80. $prefix = Caster::PREFIX_VIRTUAL;
  81. $a += array(
  82. $prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : $c->__toString(),
  83. $prefix.'allowsNull' => $c->allowsNull(),
  84. $prefix.'isBuiltin' => $c->isBuiltin(),
  85. );
  86. return $a;
  87. }
  88. public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
  89. {
  90. $prefix = Caster::PREFIX_VIRTUAL;
  91. if ($c->getThis()) {
  92. $a[$prefix.'this'] = new CutStub($c->getThis());
  93. }
  94. $function = $c->getFunction();
  95. $frame = array(
  96. 'class' => isset($function->class) ? $function->class : null,
  97. 'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null,
  98. 'function' => $function->name,
  99. 'file' => $c->getExecutingFile(),
  100. 'line' => $c->getExecutingLine(),
  101. );
  102. if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
  103. $function = new \ReflectionGenerator($c->getExecutingGenerator());
  104. array_unshift($trace, array(
  105. 'function' => 'yield',
  106. 'file' => $function->getExecutingFile(),
  107. 'line' => $function->getExecutingLine() - 1,
  108. ));
  109. $trace[] = $frame;
  110. $a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
  111. } else {
  112. $function = new FrameStub($frame, false, true);
  113. $function = ExceptionCaster::castFrameStub($function, array(), $function, true);
  114. $a[$prefix.'executing'] = new EnumStub(array(
  115. $frame['class'].$frame['type'].$frame['function'].'()' => $function[$prefix.'src'],
  116. ));
  117. }
  118. $a[Caster::PREFIX_VIRTUAL.'closed'] = false;
  119. return $a;
  120. }
  121. public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
  122. {
  123. $prefix = Caster::PREFIX_VIRTUAL;
  124. if ($n = \Reflection::getModifierNames($c->getModifiers())) {
  125. $a[$prefix.'modifiers'] = implode(' ', $n);
  126. }
  127. self::addMap($a, $c, array(
  128. 'extends' => 'getParentClass',
  129. 'implements' => 'getInterfaceNames',
  130. 'constants' => 'getConstants',
  131. ));
  132. foreach ($c->getProperties() as $n) {
  133. $a[$prefix.'properties'][$n->name] = $n;
  134. }
  135. foreach ($c->getMethods() as $n) {
  136. $a[$prefix.'methods'][$n->name] = $n;
  137. }
  138. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  139. self::addExtra($a, $c);
  140. }
  141. return $a;
  142. }
  143. public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0)
  144. {
  145. $prefix = Caster::PREFIX_VIRTUAL;
  146. self::addMap($a, $c, array(
  147. 'returnsReference' => 'returnsReference',
  148. 'returnType' => 'getReturnType',
  149. 'class' => 'getClosureScopeClass',
  150. 'this' => 'getClosureThis',
  151. ));
  152. if (isset($a[$prefix.'returnType'])) {
  153. $v = $a[$prefix.'returnType'];
  154. $v = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
  155. $a[$prefix.'returnType'] = $a[$prefix.'returnType']->allowsNull() ? '?'.$v : $v;
  156. }
  157. if (isset($a[$prefix.'this'])) {
  158. $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
  159. }
  160. foreach ($c->getParameters() as $v) {
  161. $k = '$'.$v->name;
  162. if (method_exists($v, 'isVariadic') && $v->isVariadic()) {
  163. $k = '...'.$k;
  164. }
  165. if ($v->isPassedByReference()) {
  166. $k = '&'.$k;
  167. }
  168. $a[$prefix.'parameters'][$k] = $v;
  169. }
  170. if (isset($a[$prefix.'parameters'])) {
  171. $a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
  172. }
  173. if ($v = $c->getStaticVariables()) {
  174. foreach ($v as $k => &$v) {
  175. $a[$prefix.'use']['$'.$k] = &$v;
  176. }
  177. unset($v);
  178. $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
  179. }
  180. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  181. self::addExtra($a, $c);
  182. }
  183. // Added by HHVM
  184. unset($a[Caster::PREFIX_DYNAMIC.'static']);
  185. return $a;
  186. }
  187. public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
  188. {
  189. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  190. return $a;
  191. }
  192. public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
  193. {
  194. $prefix = Caster::PREFIX_VIRTUAL;
  195. // Added by HHVM
  196. unset($a['info']);
  197. self::addMap($a, $c, array(
  198. 'position' => 'getPosition',
  199. 'isVariadic' => 'isVariadic',
  200. 'byReference' => 'isPassedByReference',
  201. 'allowsNull' => 'allowsNull',
  202. ));
  203. if (method_exists($c, 'getType')) {
  204. if ($v = $c->getType()) {
  205. $a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
  206. }
  207. } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $c, $v)) {
  208. $a[$prefix.'typeHint'] = $v[1];
  209. }
  210. if (!isset($a[$prefix.'typeHint'])) {
  211. unset($a[$prefix.'allowsNull']);
  212. }
  213. try {
  214. $a[$prefix.'default'] = $v = $c->getDefaultValue();
  215. if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) {
  216. $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
  217. }
  218. if (null === $v) {
  219. unset($a[$prefix.'allowsNull']);
  220. }
  221. } catch (\ReflectionException $e) {
  222. if (isset($a[$prefix.'typeHint']) && $c->allowsNull() && !class_exists('ReflectionNamedType', false)) {
  223. $a[$prefix.'default'] = null;
  224. unset($a[$prefix.'allowsNull']);
  225. }
  226. }
  227. return $a;
  228. }
  229. public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
  230. {
  231. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  232. self::addExtra($a, $c);
  233. return $a;
  234. }
  235. public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
  236. {
  237. self::addMap($a, $c, array(
  238. 'version' => 'getVersion',
  239. 'dependencies' => 'getDependencies',
  240. 'iniEntries' => 'getIniEntries',
  241. 'isPersistent' => 'isPersistent',
  242. 'isTemporary' => 'isTemporary',
  243. 'constants' => 'getConstants',
  244. 'functions' => 'getFunctions',
  245. 'classes' => 'getClasses',
  246. ));
  247. return $a;
  248. }
  249. public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
  250. {
  251. self::addMap($a, $c, array(
  252. 'version' => 'getVersion',
  253. 'author' => 'getAuthor',
  254. 'copyright' => 'getCopyright',
  255. 'url' => 'getURL',
  256. ));
  257. return $a;
  258. }
  259. private static function addExtra(&$a, \Reflector $c)
  260. {
  261. $x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : array();
  262. if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
  263. $x['file'] = $m;
  264. $x['line'] = $c->getStartLine().' to '.$c->getEndLine();
  265. }
  266. self::addMap($x, $c, self::$extraMap, '');
  267. if ($x) {
  268. $a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x);
  269. }
  270. }
  271. private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
  272. {
  273. foreach ($map as $k => $m) {
  274. if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
  275. $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;
  276. }
  277. }
  278. }
  279. }