/laravel_tintuc/vendor/psy/psysh/src/Command/ListCommand/PropertyEnumerator.php

https://gitlab.com/nmhieucoder/laravel_tintuc · PHP · 178 lines · 93 code · 26 blank · 59 comment · 16 complexity · ebf310324faae698a893e9a2df0dfddb MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2020 Justin Hileman
  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 Psy\Command\ListCommand;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. /**
  13. * Property Enumerator class.
  14. */
  15. class PropertyEnumerator extends Enumerator
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
  21. {
  22. // only list properties when a Reflector is present.
  23. if ($reflector === null) {
  24. return [];
  25. }
  26. // We can only list properties on actual class (or object) reflectors.
  27. if (!$reflector instanceof \ReflectionClass) {
  28. return [];
  29. }
  30. // only list properties if we are specifically asked
  31. if (!$input->getOption('properties')) {
  32. return [];
  33. }
  34. $showAll = $input->getOption('all');
  35. $noInherit = $input->getOption('no-inherit');
  36. $properties = $this->prepareProperties($this->getProperties($showAll, $reflector, $noInherit), $target);
  37. if (empty($properties)) {
  38. return [];
  39. }
  40. $ret = [];
  41. $ret[$this->getKindLabel($reflector)] = $properties;
  42. return $ret;
  43. }
  44. /**
  45. * Get defined properties for the given class or object Reflector.
  46. *
  47. * @param bool $showAll Include private and protected properties
  48. * @param \Reflector $reflector
  49. * @param bool $noInherit Exclude inherited properties
  50. *
  51. * @return array
  52. */
  53. protected function getProperties($showAll, \Reflector $reflector, $noInherit = false)
  54. {
  55. $className = $reflector->getName();
  56. $properties = [];
  57. foreach ($reflector->getProperties() as $property) {
  58. if ($noInherit && $property->getDeclaringClass()->getName() !== $className) {
  59. continue;
  60. }
  61. if ($showAll || $property->isPublic()) {
  62. $properties[$property->getName()] = $property;
  63. }
  64. }
  65. \ksort($properties, \SORT_NATURAL | \SORT_FLAG_CASE);
  66. return $properties;
  67. }
  68. /**
  69. * Prepare formatted property array.
  70. *
  71. * @param array $properties
  72. *
  73. * @return array
  74. */
  75. protected function prepareProperties(array $properties, $target = null)
  76. {
  77. // My kingdom for a generator.
  78. $ret = [];
  79. foreach ($properties as $name => $property) {
  80. if ($this->showItem($name)) {
  81. $fname = '$'.$name;
  82. $ret[$fname] = [
  83. 'name' => $fname,
  84. 'style' => $this->getVisibilityStyle($property),
  85. 'value' => $this->presentValue($property, $target),
  86. ];
  87. }
  88. }
  89. return $ret;
  90. }
  91. /**
  92. * Get a label for the particular kind of "class" represented.
  93. *
  94. * @param \ReflectionClass $reflector
  95. *
  96. * @return string
  97. */
  98. protected function getKindLabel(\ReflectionClass $reflector)
  99. {
  100. if (\method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
  101. return 'Trait Properties';
  102. } else {
  103. return 'Class Properties';
  104. }
  105. }
  106. /**
  107. * Get output style for the given property's visibility.
  108. *
  109. * @param \ReflectionProperty $property
  110. *
  111. * @return string
  112. */
  113. private function getVisibilityStyle(\ReflectionProperty $property)
  114. {
  115. if ($property->isPublic()) {
  116. return self::IS_PUBLIC;
  117. } elseif ($property->isProtected()) {
  118. return self::IS_PROTECTED;
  119. } else {
  120. return self::IS_PRIVATE;
  121. }
  122. }
  123. /**
  124. * Present the $target's current value for a reflection property.
  125. *
  126. * @param \ReflectionProperty $property
  127. * @param mixed $target
  128. *
  129. * @return string
  130. */
  131. protected function presentValue(\ReflectionProperty $property, $target)
  132. {
  133. // If $target is a class or trait (try to) get the default
  134. // value for the property.
  135. if (!\is_object($target)) {
  136. try {
  137. $refl = new \ReflectionClass($target);
  138. $props = $refl->getDefaultProperties();
  139. if (\array_key_exists($property->name, $props)) {
  140. $suffix = $property->isStatic() ? '' : ' <aside>(default)</aside>';
  141. return $this->presentRef($props[$property->name]).$suffix;
  142. }
  143. } catch (\Exception $e) {
  144. // Well, we gave it a shot.
  145. }
  146. return '';
  147. }
  148. $property->setAccessible(true);
  149. $value = $property->getValue($target);
  150. return $this->presentRef($value);
  151. }
  152. }