PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/var-dumper/Caster/ReflectionCaster.php

https://gitlab.com/judielsm/Handora
PHP | 240 lines | 181 code | 43 blank | 16 comment | 22 complexity | a1253078f033a5d524d44c11c2fb9e24 MD5 | raw file
  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. 'class' => 'getClosureScopeClass',
  92. 'this' => 'getClosureThis',
  93. ));
  94. if (isset($a[$prefix.'this'])) {
  95. $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
  96. }
  97. foreach ($c->getParameters() as $v) {
  98. $k = '$'.$v->name;
  99. if ($v->isPassedByReference()) {
  100. $k = '&'.$k;
  101. }
  102. if (method_exists($v, 'isVariadic') && $v->isVariadic()) {
  103. $k = '...'.$k;
  104. }
  105. $a[$prefix.'parameters'][$k] = $v;
  106. }
  107. if ($v = $c->getStaticVariables()) {
  108. foreach ($v as $k => &$v) {
  109. $a[$prefix.'use']['$'.$k] =& $v;
  110. }
  111. unset($v);
  112. }
  113. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  114. self::addExtra($a, $c);
  115. }
  116. return $a;
  117. }
  118. public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
  119. {
  120. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  121. return $a;
  122. }
  123. public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
  124. {
  125. $prefix = Caster::PREFIX_VIRTUAL;
  126. self::addMap($a, $c, array(
  127. 'position' => 'getPosition',
  128. 'isVariadic' => 'isVariadic',
  129. 'byReference' => 'isPassedByReference',
  130. ));
  131. try {
  132. if ($c->isArray()) {
  133. $a[$prefix.'typeHint'] = 'array';
  134. } elseif (method_exists($c, 'isCallable') && $c->isCallable()) {
  135. $a[$prefix.'typeHint'] = 'callable';
  136. } elseif ($v = $c->getClass()) {
  137. $a[$prefix.'typeHint'] = $v->name;
  138. }
  139. } catch (\ReflectionException $e) {
  140. }
  141. try {
  142. $a[$prefix.'default'] = $v = $c->getDefaultValue();
  143. if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) {
  144. $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
  145. }
  146. } catch (\ReflectionException $e) {
  147. }
  148. return $a;
  149. }
  150. public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
  151. {
  152. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  153. self::addExtra($a, $c);
  154. return $a;
  155. }
  156. public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
  157. {
  158. self::addMap($a, $c, array(
  159. 'version' => 'getVersion',
  160. 'dependencies' => 'getDependencies',
  161. 'iniEntries' => 'getIniEntries',
  162. 'isPersistent' => 'isPersistent',
  163. 'isTemporary' => 'isTemporary',
  164. 'constants' => 'getConstants',
  165. 'functions' => 'getFunctions',
  166. 'classes' => 'getClasses',
  167. ));
  168. return $a;
  169. }
  170. public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
  171. {
  172. self::addMap($a, $c, array(
  173. 'version' => 'getVersion',
  174. 'author' => 'getAuthor',
  175. 'copyright' => 'getCopyright',
  176. 'url' => 'getURL',
  177. ));
  178. return $a;
  179. }
  180. private static function addExtra(&$a, \Reflector $c)
  181. {
  182. $a =& $a[Caster::PREFIX_VIRTUAL.'extra'];
  183. if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
  184. $a['file'] = $m;
  185. $a['line'] = $c->getStartLine().' to '.$c->getEndLine();
  186. }
  187. self::addMap($a, $c, self::$extraMap, '');
  188. }
  189. private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
  190. {
  191. foreach ($map as $k => $m) {
  192. if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
  193. $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;
  194. }
  195. }
  196. }
  197. }