annotate vendor/psy/psysh/src/Command/ListCommand.php @ 4:a9cd425dd02b

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