ReflectionCaster.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /**
  30. * @deprecated since Symfony 2.7, to be removed in 3.0.
  31. */
  32. public static function castReflector(\Reflector $c, array $a, Stub $stub, $isNested)
  33. {
  34. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
  35. $a[Caster::PREFIX_VIRTUAL.'reflection'] = $c->__toString();
  36. return $a;
  37. }
  38. public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
  39. {
  40. $prefix = Caster::PREFIX_VIRTUAL;
  41. $c = new \ReflectionFunction($c);
  42. $stub->class = 'Closure'; // HHVM generates unique class names for closures
  43. $a = static::castFunctionAbstract($c, $a, $stub, $isNested);
  44. if (isset($a[$prefix.'parameters'])) {
  45. foreach ($a[$prefix.'parameters'] as &$v) {
  46. $param = $v;
  47. $v = array();
  48. foreach (static::castParameter($param, array(), $stub, true) as $k => $param) {
  49. if ("\0" === $k[0]) {
  50. $v[substr($k, 3)] = $param;
  51. }
  52. }
  53. unset($v['position'], $v['isVariadic'], $v['byReference'], $v);
  54. }
  55. }
  56. if ($f = $c->getFileName()) {
  57. $a[$prefix.'file'] = $f;
  58. $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
  59. }
  60. $prefix = Caster::PREFIX_DYNAMIC;
  61. unset($a['name'], $a[$prefix.'0'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
  62. return $a;
  63. }
  64. public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
  65. {
  66. $prefix = Caster::PREFIX_VIRTUAL;
  67. if ($n = \Reflection::getModifierNames($c->getModifiers())) {
  68. $a[$prefix.'modifiers'] = implode(' ', $n);
  69. }
  70. self::addMap($a, $c, array(
  71. 'extends' => 'getParentClass',
  72. 'implements' => 'getInterfaceNames',
  73. 'constants' => 'getConstants',
  74. ));
  75. foreach ($c->getProperties() as $n) {
  76. $a[$prefix.'properties'][$n->name] = $n;
  77. }
  78. foreach ($c->getMethods() as $n) {
  79. $a[$prefix.'methods'][$n->name] = $n;
  80. }
  81. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  82. self::addExtra($a, $c);
  83. }
  84. return $a;
  85. }
  86. public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0)
  87. {
  88. $prefix = Caster::PREFIX_VIRTUAL;
  89. self::addMap($a, $c, array(
  90. 'returnsReference' => 'returnsReference',
  91. 'returnType' => 'getReturnType',
  92. 'class' => 'getClosureScopeClass',
  93. 'this' => 'getClosureThis',
  94. ));
  95. if (isset($a[$prefix.'this'])) {
  96. $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
  97. }
  98. foreach ($c->getParameters() as $v) {
  99. $k = '$'.$v->name;
  100. if ($v->isPassedByReference()) {
  101. $k = '&'.$k;
  102. }
  103. if (method_exists($v, 'isVariadic') && $v->isVariadic()) {
  104. $k = '...'.$k;
  105. }
  106. $a[$prefix.'parameters'][$k] = $v;
  107. }
  108. if ($v = $c->getStaticVariables()) {
  109. foreach ($v as $k => &$v) {
  110. $a[$prefix.'use']['$'.$k] = &$v;
  111. }
  112. unset($v);
  113. }
  114. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  115. self::addExtra($a, $c);
  116. }
  117. return $a;
  118. }
  119. public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
  120. {
  121. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  122. return $a;
  123. }
  124. public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
  125. {
  126. $prefix = Caster::PREFIX_VIRTUAL;
  127. self::addMap($a, $c, array(
  128. 'position' => 'getPosition',
  129. 'isVariadic' => 'isVariadic',
  130. 'byReference' => 'isPassedByReference',
  131. ));
  132. try {
  133. if ($c->isArray()) {
  134. $a[$prefix.'typeHint'] = 'array';
  135. } elseif (method_exists($c, 'isCallable') && $c->isCallable()) {
  136. $a[$prefix.'typeHint'] = 'callable';
  137. } elseif ($v = $c->getClass()) {
  138. $a[$prefix.'typeHint'] = $v->name;
  139. }
  140. } catch (\ReflectionException $e) {
  141. }
  142. try {
  143. $a[$prefix.'default'] = $v = $c->getDefaultValue();
  144. if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) {
  145. $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
  146. }
  147. } catch (\ReflectionException $e) {
  148. }
  149. return $a;
  150. }
  151. public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
  152. {
  153. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  154. self::addExtra($a, $c);
  155. return $a;
  156. }
  157. public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
  158. {
  159. self::addMap($a, $c, array(
  160. 'version' => 'getVersion',
  161. 'dependencies' => 'getDependencies',
  162. 'iniEntries' => 'getIniEntries',
  163. 'isPersistent' => 'isPersistent',
  164. 'isTemporary' => 'isTemporary',
  165. 'constants' => 'getConstants',
  166. 'functions' => 'getFunctions',
  167. 'classes' => 'getClasses',
  168. ));
  169. return $a;
  170. }
  171. public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
  172. {
  173. self::addMap($a, $c, array(
  174. 'version' => 'getVersion',
  175. 'author' => 'getAuthor',
  176. 'copyright' => 'getCopyright',
  177. 'url' => 'getURL',
  178. ));
  179. return $a;
  180. }
  181. private static function addExtra(&$a, \Reflector $c)
  182. {
  183. $a = &$a[Caster::PREFIX_VIRTUAL.'extra'];
  184. if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
  185. $a['file'] = $m;
  186. $a['line'] = $c->getStartLine().' to '.$c->getEndLine();
  187. }
  188. self::addMap($a, $c, self::$extraMap, '');
  189. }
  190. private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
  191. {
  192. foreach ($map as $k => $m) {
  193. if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
  194. $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;
  195. }
  196. }
  197. }
  198. }