No Description

ReflectionCaster.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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'] = new LinkStub($f, $c->getStartLine());
  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.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : $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. $v = $a[$prefix.'returnType'];
  134. $v = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
  135. $a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType']->allowsNull() ? '?'.$v : $v, array(class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', ''));
  136. }
  137. if (isset($a[$prefix.'class'])) {
  138. $a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
  139. }
  140. if (isset($a[$prefix.'this'])) {
  141. $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
  142. }
  143. foreach ($c->getParameters() as $v) {
  144. $k = '$'.$v->name;
  145. if (method_exists($v, 'isVariadic') && $v->isVariadic()) {
  146. $k = '...'.$k;
  147. }
  148. if ($v->isPassedByReference()) {
  149. $k = '&'.$k;
  150. }
  151. $a[$prefix.'parameters'][$k] = $v;
  152. }
  153. if (isset($a[$prefix.'parameters'])) {
  154. $a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
  155. }
  156. if ($v = $c->getStaticVariables()) {
  157. foreach ($v as $k => &$v) {
  158. $a[$prefix.'use']['$'.$k] = &$v;
  159. }
  160. unset($v);
  161. $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
  162. }
  163. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  164. self::addExtra($a, $c);
  165. }
  166. // Added by HHVM
  167. unset($a[Caster::PREFIX_DYNAMIC.'static']);
  168. return $a;
  169. }
  170. public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
  171. {
  172. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  173. return $a;
  174. }
  175. public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
  176. {
  177. $prefix = Caster::PREFIX_VIRTUAL;
  178. // Added by HHVM
  179. unset($a['info']);
  180. self::addMap($a, $c, array(
  181. 'position' => 'getPosition',
  182. 'isVariadic' => 'isVariadic',
  183. 'byReference' => 'isPassedByReference',
  184. 'allowsNull' => 'allowsNull',
  185. ));
  186. if (method_exists($c, 'getType')) {
  187. if ($v = $c->getType()) {
  188. $a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
  189. }
  190. } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $c, $v)) {
  191. $a[$prefix.'typeHint'] = $v[1];
  192. }
  193. if (isset($a[$prefix.'typeHint'])) {
  194. $v = $a[$prefix.'typeHint'];
  195. $a[$prefix.'typeHint'] = new ClassStub($v, array(class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', ''));
  196. } else {
  197. unset($a[$prefix.'allowsNull']);
  198. }
  199. try {
  200. $a[$prefix.'default'] = $v = $c->getDefaultValue();
  201. if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) {
  202. $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
  203. }
  204. if (null === $v) {
  205. unset($a[$prefix.'allowsNull']);
  206. }
  207. } catch (\ReflectionException $e) {
  208. if (isset($a[$prefix.'typeHint']) && $c->allowsNull() && !class_exists('ReflectionNamedType', false)) {
  209. $a[$prefix.'default'] = null;
  210. unset($a[$prefix.'allowsNull']);
  211. }
  212. }
  213. return $a;
  214. }
  215. public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
  216. {
  217. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  218. self::addExtra($a, $c);
  219. return $a;
  220. }
  221. public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
  222. {
  223. self::addMap($a, $c, array(
  224. 'version' => 'getVersion',
  225. 'dependencies' => 'getDependencies',
  226. 'iniEntries' => 'getIniEntries',
  227. 'isPersistent' => 'isPersistent',
  228. 'isTemporary' => 'isTemporary',
  229. 'constants' => 'getConstants',
  230. 'functions' => 'getFunctions',
  231. 'classes' => 'getClasses',
  232. ));
  233. return $a;
  234. }
  235. public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
  236. {
  237. self::addMap($a, $c, array(
  238. 'version' => 'getVersion',
  239. 'author' => 'getAuthor',
  240. 'copyright' => 'getCopyright',
  241. 'url' => 'getURL',
  242. ));
  243. return $a;
  244. }
  245. private static function addExtra(&$a, \Reflector $c)
  246. {
  247. $x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : array();
  248. if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
  249. $x['file'] = new LinkStub($m, $c->getStartLine());
  250. $x['line'] = $c->getStartLine().' to '.$c->getEndLine();
  251. }
  252. self::addMap($x, $c, self::$extraMap, '');
  253. if ($x) {
  254. $a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x);
  255. }
  256. }
  257. private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
  258. {
  259. foreach ($map as $k => $m) {
  260. if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
  261. $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;
  262. }
  263. }
  264. }
  265. }