annotate vendor/psy/psysh/src/Command/ListCommand.php @ 19:fa3358dc1485 tip

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