PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/psy/psysh/src/Psy/Command/ListCommand.php

https://gitlab.com/ealexis.t/trends
PHP | 278 lines | 183 code | 36 blank | 59 comment | 17 complexity | 7f66fb9ec2935585d2a623565b0fabf7 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2015 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;
  11. use Psy\Command\ListCommand\ClassConstantEnumerator;
  12. use Psy\Command\ListCommand\ClassEnumerator;
  13. use Psy\Command\ListCommand\ConstantEnumerator;
  14. use Psy\Command\ListCommand\FunctionEnumerator;
  15. use Psy\Command\ListCommand\GlobalVariableEnumerator;
  16. use Psy\Command\ListCommand\InterfaceEnumerator;
  17. use Psy\Command\ListCommand\MethodEnumerator;
  18. use Psy\Command\ListCommand\PropertyEnumerator;
  19. use Psy\Command\ListCommand\TraitEnumerator;
  20. use Psy\Command\ListCommand\VariableEnumerator;
  21. use Psy\Exception\RuntimeException;
  22. use Psy\VarDumper\Presenter;
  23. use Psy\VarDumper\PresenterAware;
  24. use Symfony\Component\Console\Formatter\OutputFormatter;
  25. use Symfony\Component\Console\Helper\TableHelper;
  26. use Symfony\Component\Console\Input\InputArgument;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Input\InputOption;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. /**
  31. * List available local variables, object properties, etc.
  32. */
  33. class ListCommand extends ReflectingCommand implements PresenterAware
  34. {
  35. protected $presenter;
  36. protected $enumerators;
  37. /**
  38. * PresenterAware interface.
  39. *
  40. * @param Presenter $manager
  41. */
  42. public function setPresenter(Presenter $presenter)
  43. {
  44. $this->presenter = $presenter;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function configure()
  50. {
  51. $this
  52. ->setName('ls')
  53. ->setAliases(array('list', 'dir'))
  54. ->setDefinition(array(
  55. new InputArgument('target', InputArgument::OPTIONAL, 'A target class or object to list.', null),
  56. new InputOption('vars', '', InputOption::VALUE_NONE, 'Display variables.'),
  57. new InputOption('constants', 'c', InputOption::VALUE_NONE, 'Display defined constants.'),
  58. new InputOption('functions', 'f', InputOption::VALUE_NONE, 'Display defined functions.'),
  59. new InputOption('classes', 'k', InputOption::VALUE_NONE, 'Display declared classes.'),
  60. new InputOption('interfaces', 'I', InputOption::VALUE_NONE, 'Display declared interfaces.'),
  61. new InputOption('traits', 't', InputOption::VALUE_NONE, 'Display declared traits.'),
  62. new InputOption('properties', 'p', InputOption::VALUE_NONE, 'Display class or object properties (public properties by default).'),
  63. new InputOption('methods', 'm', InputOption::VALUE_NONE, 'Display class or object methods (public methods by default).'),
  64. new InputOption('grep', 'G', InputOption::VALUE_REQUIRED, 'Limit to items matching the given pattern (string or regex).'),
  65. new InputOption('insensitive', 'i', InputOption::VALUE_NONE, 'Case-insensitive search (requires --grep).'),
  66. new InputOption('invert', 'v', InputOption::VALUE_NONE, 'Inverted search (requires --grep).'),
  67. new InputOption('globals', 'g', InputOption::VALUE_NONE, 'Include global variables.'),
  68. new InputOption('internal', 'n', InputOption::VALUE_NONE, 'Limit to internal functions and classes.'),
  69. new InputOption('user', 'u', InputOption::VALUE_NONE, 'Limit to user-defined constants, functions and classes.'),
  70. new InputOption('category', 'C', InputOption::VALUE_REQUIRED, 'Limit to constants in a specific category (e.g. "date").'),
  71. new InputOption('all', 'a', InputOption::VALUE_NONE, 'Include private and protected methods and properties.'),
  72. new InputOption('long', 'l', InputOption::VALUE_NONE, 'List in long format: includes class names and method signatures.'),
  73. ))
  74. ->setDescription('List local, instance or class variables, methods and constants.')
  75. ->setHelp(
  76. <<<'HELP'
  77. List variables, constants, classes, interfaces, traits, functions, methods,
  78. and properties.
  79. Called without options, this will return a list of variables currently in scope.
  80. If a target object is provided, list properties, constants and methods of that
  81. target. If a class, interface or trait name is passed instead, list constants
  82. and methods on that class.
  83. e.g.
  84. <return>>>> ls</return>
  85. <return>>>> ls $foo</return>
  86. <return>>>> ls -k --grep mongo -i</return>
  87. <return>>>> ls -al ReflectionClass</return>
  88. <return>>>> ls --constants --category date</return>
  89. <return>>>> ls -l --functions --grep /^array_.*/</return>
  90. HELP
  91. );
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function execute(InputInterface $input, OutputInterface $output)
  97. {
  98. $this->validateInput($input);
  99. $this->initEnumerators();
  100. $method = $input->getOption('long') ? 'writeLong' : 'write';
  101. if ($target = $input->getArgument('target')) {
  102. list($target, $reflector) = $this->getTargetAndReflector($target, true);
  103. } else {
  104. $reflector = null;
  105. }
  106. // TODO: something cleaner than this :-/
  107. if ($input->getOption('long')) {
  108. $output->startPaging();
  109. }
  110. foreach ($this->enumerators as $enumerator) {
  111. $this->$method($output, $enumerator->enumerate($input, $reflector, $target));
  112. }
  113. if ($input->getOption('long')) {
  114. $output->stopPaging();
  115. }
  116. }
  117. /**
  118. * Initialize Enumerators.
  119. */
  120. protected function initEnumerators()
  121. {
  122. if (!isset($this->enumerators)) {
  123. $mgr = $this->presenter;
  124. $this->enumerators = array(
  125. new ClassConstantEnumerator($mgr),
  126. new ClassEnumerator($mgr),
  127. new ConstantEnumerator($mgr),
  128. new FunctionEnumerator($mgr),
  129. new GlobalVariableEnumerator($mgr),
  130. new InterfaceEnumerator($mgr),
  131. new PropertyEnumerator($mgr),
  132. new MethodEnumerator($mgr),
  133. new TraitEnumerator($mgr),
  134. new VariableEnumerator($mgr, $this->context),
  135. );
  136. }
  137. }
  138. /**
  139. * Write the list items to $output.
  140. *
  141. * @param OutputInterface $output
  142. * @param null|array $result List of enumerated items.
  143. */
  144. protected function write(OutputInterface $output, array $result = null)
  145. {
  146. if ($result === null) {
  147. return;
  148. }
  149. foreach ($result as $label => $items) {
  150. $names = array_map(array($this, 'formatItemName'), $items);
  151. $output->writeln(sprintf('<strong>%s</strong>: %s', $label, implode(', ', $names)));
  152. }
  153. }
  154. /**
  155. * Write the list items to $output.
  156. *
  157. * Items are listed one per line, and include the item signature.
  158. *
  159. * @param OutputInterface $output
  160. * @param null|array $result List of enumerated items.
  161. */
  162. protected function writeLong(OutputInterface $output, array $result = null)
  163. {
  164. if ($result === null) {
  165. return;
  166. }
  167. $table = $this->getTable($output);
  168. foreach ($result as $label => $items) {
  169. $output->writeln('');
  170. $output->writeln(sprintf('<strong>%s:</strong>', $label));
  171. $table->setRows(array());
  172. foreach ($items as $item) {
  173. $table->addRow(array($this->formatItemName($item), $item['value']));
  174. }
  175. if ($table instanceof TableHelper) {
  176. $table->render($output);
  177. } else {
  178. $table->render();
  179. }
  180. }
  181. }
  182. /**
  183. * Format an item name given its visibility.
  184. *
  185. * @param array $item
  186. *
  187. * @return string
  188. */
  189. private function formatItemName($item)
  190. {
  191. return sprintf('<%s>%s</%s>', $item['style'], OutputFormatter::escape($item['name']), $item['style']);
  192. }
  193. /**
  194. * Validate that input options make sense, provide defaults when called without options.
  195. *
  196. * @throws RuntimeException if options are inconsistent.
  197. *
  198. * @param InputInterface $input
  199. */
  200. private function validateInput(InputInterface $input)
  201. {
  202. // grep, invert and insensitive
  203. if (!$input->getOption('grep')) {
  204. foreach (array('invert', 'insensitive') as $option) {
  205. if ($input->getOption($option)) {
  206. throw new RuntimeException('--' . $option . ' does not make sense without --grep');
  207. }
  208. }
  209. }
  210. if (!$input->getArgument('target')) {
  211. // if no target is passed, there can be no properties or methods
  212. foreach (array('properties', 'methods') as $option) {
  213. if ($input->getOption($option)) {
  214. throw new RuntimeException('--' . $option . ' does not make sense without a specified target.');
  215. }
  216. }
  217. foreach (array('globals', 'vars', 'constants', 'functions', 'classes', 'interfaces', 'traits') as $option) {
  218. if ($input->getOption($option)) {
  219. return;
  220. }
  221. }
  222. // default to --vars if no other options are passed
  223. $input->setOption('vars', true);
  224. } else {
  225. // if a target is passed, classes, functions, etc don't make sense
  226. foreach (array('vars', 'globals', 'functions', 'classes', 'interfaces', 'traits') as $option) {
  227. if ($input->getOption($option)) {
  228. throw new RuntimeException('--' . $option . ' does not make sense with a specified target.');
  229. }
  230. }
  231. foreach (array('constants', 'properties', 'methods') as $option) {
  232. if ($input->getOption($option)) {
  233. return;
  234. }
  235. }
  236. // default to --constants --properties --methods if no other options are passed
  237. $input->setOption('constants', true);
  238. $input->setOption('properties', true);
  239. $input->setOption('methods', true);
  240. }
  241. }
  242. }