ReflectionCaster.php 9.4 KB

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