ReflectionCaster.php 11 KB

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. public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested, $filter = 0)
  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, $filter);
  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 (!($filter & Caster::EXCLUDE_VERBOSE) && $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. if (is_object($v)) {
  170. $a[$prefix.'use']['$'.$k] = new CutStub($v);
  171. } else {
  172. $a[$prefix.'use']['$'.$k] = &$v;
  173. }
  174. }
  175. unset($v);
  176. $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
  177. }
  178. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  179. self::addExtra($a, $c);
  180. }
  181. // Added by HHVM
  182. unset($a[Caster::PREFIX_DYNAMIC.'static']);
  183. return $a;
  184. }
  185. public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
  186. {
  187. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  188. return $a;
  189. }
  190. public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
  191. {
  192. $prefix = Caster::PREFIX_VIRTUAL;
  193. // Added by HHVM
  194. unset($a['info']);
  195. self::addMap($a, $c, array(
  196. 'position' => 'getPosition',
  197. 'isVariadic' => 'isVariadic',
  198. 'byReference' => 'isPassedByReference',
  199. 'allowsNull' => 'allowsNull',
  200. ));
  201. if (method_exists($c, 'getType')) {
  202. if ($v = $c->getType()) {
  203. $a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
  204. }
  205. } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $c, $v)) {
  206. $a[$prefix.'typeHint'] = $v[1];
  207. }
  208. if (isset($a[$prefix.'typeHint'])) {
  209. $v = $a[$prefix.'typeHint'];
  210. $a[$prefix.'typeHint'] = new ClassStub($v, array(class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', ''));
  211. } else {
  212. unset($a[$prefix.'allowsNull']);
  213. }
  214. try {
  215. $a[$prefix.'default'] = $v = $c->getDefaultValue();
  216. if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) {
  217. $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
  218. }
  219. if (null === $v) {
  220. unset($a[$prefix.'allowsNull']);
  221. }
  222. } catch (\ReflectionException $e) {
  223. if (isset($a[$prefix.'typeHint']) && $c->allowsNull() && !class_exists('ReflectionNamedType', false)) {
  224. $a[$prefix.'default'] = null;
  225. unset($a[$prefix.'allowsNull']);
  226. }
  227. }
  228. return $a;
  229. }
  230. public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
  231. {
  232. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  233. self::addExtra($a, $c);
  234. return $a;
  235. }
  236. public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
  237. {
  238. self::addMap($a, $c, array(
  239. 'version' => 'getVersion',
  240. 'dependencies' => 'getDependencies',
  241. 'iniEntries' => 'getIniEntries',
  242. 'isPersistent' => 'isPersistent',
  243. 'isTemporary' => 'isTemporary',
  244. 'constants' => 'getConstants',
  245. 'functions' => 'getFunctions',
  246. 'classes' => 'getClasses',
  247. ));
  248. return $a;
  249. }
  250. public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
  251. {
  252. self::addMap($a, $c, array(
  253. 'version' => 'getVersion',
  254. 'author' => 'getAuthor',
  255. 'copyright' => 'getCopyright',
  256. 'url' => 'getURL',
  257. ));
  258. return $a;
  259. }
  260. private static function addExtra(&$a, \Reflector $c)
  261. {
  262. $x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : array();
  263. if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
  264. $x['file'] = new LinkStub($m, $c->getStartLine());
  265. $x['line'] = $c->getStartLine().' to '.$c->getEndLine();
  266. }
  267. self::addMap($x, $c, self::$extraMap, '');
  268. if ($x) {
  269. $a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x);
  270. }
  271. }
  272. private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
  273. {
  274. foreach ($map as $k => $m) {
  275. if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
  276. $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;
  277. }
  278. }
  279. }
  280. }