ReflectionCaster.php 11 KB

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