ReflectionCaster.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
  30. {
  31. $prefix = Caster::PREFIX_VIRTUAL;
  32. $c = new \ReflectionFunction($c);
  33. $stub->class = 'Closure'; // HHVM generates unique class names for closures
  34. $a = static::castFunctionAbstract($c, $a, $stub, $isNested);
  35. if (isset($a[$prefix.'parameters'])) {
  36. foreach ($a[$prefix.'parameters']->value as &$v) {
  37. $param = $v;
  38. $v = new EnumStub(array());
  39. foreach (static::castParameter($param, array(), $stub, true) as $k => $param) {
  40. if ("\0" === $k[0]) {
  41. $v->value[substr($k, 3)] = $param;
  42. }
  43. }
  44. unset($v->value['position'], $v->value['isVariadic'], $v->value['byReference'], $v);
  45. }
  46. }
  47. if ($f = $c->getFileName()) {
  48. $a[$prefix.'file'] = $f;
  49. $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
  50. }
  51. $prefix = Caster::PREFIX_DYNAMIC;
  52. unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
  53. return $a;
  54. }
  55. public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested)
  56. {
  57. return class_exists('ReflectionGenerator', false) ? self::castReflectionGenerator(new \ReflectionGenerator($c), $a, $stub, $isNested) : $a;
  58. }
  59. public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
  60. {
  61. $prefix = Caster::PREFIX_VIRTUAL;
  62. $a += array(
  63. $prefix.'type' => $c->__toString(),
  64. $prefix.'allowsNull' => $c->allowsNull(),
  65. $prefix.'isBuiltin' => $c->isBuiltin(),
  66. );
  67. return $a;
  68. }
  69. public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
  70. {
  71. $prefix = Caster::PREFIX_VIRTUAL;
  72. if ($c->getThis()) {
  73. $a[$prefix.'this'] = new CutStub($c->getThis());
  74. }
  75. $x = $c->getFunction();
  76. $frame = array(
  77. 'class' => isset($x->class) ? $x->class : null,
  78. 'type' => isset($x->class) ? ($x->isStatic() ? '::' : '->') : null,
  79. 'function' => $x->name,
  80. 'file' => $c->getExecutingFile(),
  81. 'line' => $c->getExecutingLine(),
  82. );
  83. if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
  84. $x = new \ReflectionGenerator($c->getExecutingGenerator());
  85. array_unshift($trace, array(
  86. 'function' => 'yield',
  87. 'file' => $x->getExecutingFile(),
  88. 'line' => $x->getExecutingLine() - 1,
  89. ));
  90. $trace[] = $frame;
  91. $a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
  92. } else {
  93. $x = new FrameStub($frame, false, true);
  94. $x = ExceptionCaster::castFrameStub($x, array(), $x, true);
  95. $a[$prefix.'executing'] = new EnumStub(array(
  96. $frame['class'].$frame['type'].$frame['function'].'()' => $x[$prefix.'src'],
  97. ));
  98. }
  99. return $a;
  100. }
  101. public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
  102. {
  103. $prefix = Caster::PREFIX_VIRTUAL;
  104. if ($n = \Reflection::getModifierNames($c->getModifiers())) {
  105. $a[$prefix.'modifiers'] = implode(' ', $n);
  106. }
  107. self::addMap($a, $c, array(
  108. 'extends' => 'getParentClass',
  109. 'implements' => 'getInterfaceNames',
  110. 'constants' => 'getConstants',
  111. ));
  112. foreach ($c->getProperties() as $n) {
  113. $a[$prefix.'properties'][$n->name] = $n;
  114. }
  115. foreach ($c->getMethods() as $n) {
  116. $a[$prefix.'methods'][$n->name] = $n;
  117. }
  118. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  119. self::addExtra($a, $c);
  120. }
  121. return $a;
  122. }
  123. public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0)
  124. {
  125. $prefix = Caster::PREFIX_VIRTUAL;
  126. self::addMap($a, $c, array(
  127. 'returnsReference' => 'returnsReference',
  128. 'returnType' => 'getReturnType',
  129. 'class' => 'getClosureScopeClass',
  130. 'this' => 'getClosureThis',
  131. ));
  132. if (isset($a[$prefix.'returnType'])) {
  133. $a[$prefix.'returnType'] = (string) $a[$prefix.'returnType'];
  134. }
  135. if (isset($a[$prefix.'this'])) {
  136. $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
  137. }
  138. foreach ($c->getParameters() as $v) {
  139. $k = '$'.$v->name;
  140. if ($v->isPassedByReference()) {
  141. $k = '&'.$k;
  142. }
  143. if (method_exists($v, 'isVariadic') && $v->isVariadic()) {
  144. $k = '...'.$k;
  145. }
  146. $a[$prefix.'parameters'][$k] = $v;
  147. }
  148. if (isset($a[$prefix.'parameters'])) {
  149. $a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
  150. }
  151. if ($v = $c->getStaticVariables()) {
  152. foreach ($v as $k => &$v) {
  153. $a[$prefix.'use']['$'.$k] = &$v;
  154. }
  155. unset($v);
  156. $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
  157. }
  158. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  159. self::addExtra($a, $c);
  160. }
  161. // Added by HHVM
  162. unset($a[Caster::PREFIX_DYNAMIC.'static']);
  163. return $a;
  164. }
  165. public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
  166. {
  167. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  168. return $a;
  169. }
  170. public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
  171. {
  172. $prefix = Caster::PREFIX_VIRTUAL;
  173. // Added by HHVM
  174. unset($a['info']);
  175. self::addMap($a, $c, array(
  176. 'position' => 'getPosition',
  177. 'isVariadic' => 'isVariadic',
  178. 'byReference' => 'isPassedByReference',
  179. ));
  180. try {
  181. if (method_exists($c, 'hasType')) {
  182. if ($c->hasType()) {
  183. $a[$prefix.'typeHint'] = $c->getType()->__toString();
  184. }
  185. } else {
  186. $v = explode(' ', $c->__toString(), 6);
  187. if (isset($v[5]) && 0 === strspn($v[4], '.&$')) {
  188. $a[$prefix.'typeHint'] = $v[4];
  189. }
  190. }
  191. } catch (\ReflectionException $e) {
  192. if (preg_match('/^Class ([^ ]++) does not exist$/', $e->getMessage(), $m)) {
  193. $a[$prefix.'typeHint'] = $m[1];
  194. }
  195. }
  196. try {
  197. $a[$prefix.'default'] = $v = $c->getDefaultValue();
  198. if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) {
  199. $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
  200. }
  201. } catch (\ReflectionException $e) {
  202. if (isset($a[$prefix.'typeHint']) && $c->allowsNull()) {
  203. $a[$prefix.'default'] = null;
  204. }
  205. }
  206. return $a;
  207. }
  208. public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
  209. {
  210. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  211. self::addExtra($a, $c);
  212. return $a;
  213. }
  214. public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
  215. {
  216. self::addMap($a, $c, array(
  217. 'version' => 'getVersion',
  218. 'dependencies' => 'getDependencies',
  219. 'iniEntries' => 'getIniEntries',
  220. 'isPersistent' => 'isPersistent',
  221. 'isTemporary' => 'isTemporary',
  222. 'constants' => 'getConstants',
  223. 'functions' => 'getFunctions',
  224. 'classes' => 'getClasses',
  225. ));
  226. return $a;
  227. }
  228. public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
  229. {
  230. self::addMap($a, $c, array(
  231. 'version' => 'getVersion',
  232. 'author' => 'getAuthor',
  233. 'copyright' => 'getCopyright',
  234. 'url' => 'getURL',
  235. ));
  236. return $a;
  237. }
  238. private static function addExtra(&$a, \Reflector $c)
  239. {
  240. $x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : array();
  241. if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
  242. $x['file'] = $m;
  243. $x['line'] = $c->getStartLine().' to '.$c->getEndLine();
  244. }
  245. self::addMap($x, $c, self::$extraMap, '');
  246. if ($x) {
  247. $a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x);
  248. }
  249. }
  250. private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
  251. {
  252. foreach ($map as $k => $m) {
  253. if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
  254. $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;
  255. }
  256. }
  257. }
  258. }