annotate vendor/psy/psysh/src/Psy/Command/ListCommand.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
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-2017 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\FilterOptions;
Chris@0 24 use Psy\VarDumper\Presenter;
Chris@0 25 use Psy\VarDumper\PresenterAware;
Chris@0 26 use Symfony\Component\Console\Formatter\OutputFormatter;
Chris@0 27 use Symfony\Component\Console\Helper\TableHelper;
Chris@0 28 use Symfony\Component\Console\Input\InputArgument;
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 $manager
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(array('list', 'dir'))
Chris@0 61 ->setDefinition(array(
Chris@0 62 new InputArgument('target', InputArgument::OPTIONAL, 'A target class or object to list.', null),
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 HELP
Chris@0 108 );
Chris@0 109 }
Chris@0 110
Chris@0 111 /**
Chris@0 112 * {@inheritdoc}
Chris@0 113 */
Chris@0 114 protected function execute(InputInterface $input, OutputInterface $output)
Chris@0 115 {
Chris@0 116 $this->validateInput($input);
Chris@0 117 $this->initEnumerators();
Chris@0 118
Chris@0 119 $method = $input->getOption('long') ? 'writeLong' : 'write';
Chris@0 120
Chris@0 121 if ($target = $input->getArgument('target')) {
Chris@0 122 list($target, $reflector) = $this->getTargetAndReflector($target, true);
Chris@0 123 } else {
Chris@0 124 $reflector = null;
Chris@0 125 }
Chris@0 126
Chris@0 127 // @todo something cleaner than this :-/
Chris@0 128 if ($input->getOption('long')) {
Chris@0 129 $output->startPaging();
Chris@0 130 }
Chris@0 131
Chris@0 132 foreach ($this->enumerators as $enumerator) {
Chris@0 133 $this->$method($output, $enumerator->enumerate($input, $reflector, $target));
Chris@0 134 }
Chris@0 135
Chris@0 136 if ($input->getOption('long')) {
Chris@0 137 $output->stopPaging();
Chris@0 138 }
Chris@0 139
Chris@0 140 // Set some magic local variables
Chris@0 141 if ($reflector !== null) {
Chris@0 142 $this->setCommandScopeVariables($reflector);
Chris@0 143 }
Chris@0 144 }
Chris@0 145
Chris@0 146 /**
Chris@0 147 * Initialize Enumerators.
Chris@0 148 */
Chris@0 149 protected function initEnumerators()
Chris@0 150 {
Chris@0 151 if (!isset($this->enumerators)) {
Chris@0 152 $mgr = $this->presenter;
Chris@0 153
Chris@0 154 $this->enumerators = array(
Chris@0 155 new ClassConstantEnumerator($mgr),
Chris@0 156 new ClassEnumerator($mgr),
Chris@0 157 new ConstantEnumerator($mgr),
Chris@0 158 new FunctionEnumerator($mgr),
Chris@0 159 new GlobalVariableEnumerator($mgr),
Chris@0 160 new PropertyEnumerator($mgr),
Chris@0 161 new MethodEnumerator($mgr),
Chris@0 162 new VariableEnumerator($mgr, $this->context),
Chris@0 163 );
Chris@0 164 }
Chris@0 165 }
Chris@0 166
Chris@0 167 /**
Chris@0 168 * Write the list items to $output.
Chris@0 169 *
Chris@0 170 * @param OutputInterface $output
Chris@0 171 * @param null|array $result List of enumerated items
Chris@0 172 */
Chris@0 173 protected function write(OutputInterface $output, array $result = null)
Chris@0 174 {
Chris@0 175 if ($result === null) {
Chris@0 176 return;
Chris@0 177 }
Chris@0 178
Chris@0 179 foreach ($result as $label => $items) {
Chris@0 180 $names = array_map(array($this, 'formatItemName'), $items);
Chris@0 181 $output->writeln(sprintf('<strong>%s</strong>: %s', $label, implode(', ', $names)));
Chris@0 182 }
Chris@0 183 }
Chris@0 184
Chris@0 185 /**
Chris@0 186 * Write the list items to $output.
Chris@0 187 *
Chris@0 188 * Items are listed one per line, and include the item signature.
Chris@0 189 *
Chris@0 190 * @param OutputInterface $output
Chris@0 191 * @param null|array $result List of enumerated items
Chris@0 192 */
Chris@0 193 protected function writeLong(OutputInterface $output, array $result = null)
Chris@0 194 {
Chris@0 195 if ($result === null) {
Chris@0 196 return;
Chris@0 197 }
Chris@0 198
Chris@0 199 $table = $this->getTable($output);
Chris@0 200
Chris@0 201 foreach ($result as $label => $items) {
Chris@0 202 $output->writeln('');
Chris@0 203 $output->writeln(sprintf('<strong>%s:</strong>', $label));
Chris@0 204
Chris@0 205 $table->setRows(array());
Chris@0 206 foreach ($items as $item) {
Chris@0 207 $table->addRow(array($this->formatItemName($item), $item['value']));
Chris@0 208 }
Chris@0 209
Chris@0 210 if ($table instanceof TableHelper) {
Chris@0 211 $table->render($output);
Chris@0 212 } else {
Chris@0 213 $table->render();
Chris@0 214 }
Chris@0 215 }
Chris@0 216 }
Chris@0 217
Chris@0 218 /**
Chris@0 219 * Format an item name given its visibility.
Chris@0 220 *
Chris@0 221 * @param array $item
Chris@0 222 *
Chris@0 223 * @return string
Chris@0 224 */
Chris@0 225 private function formatItemName($item)
Chris@0 226 {
Chris@0 227 return sprintf('<%s>%s</%s>', $item['style'], OutputFormatter::escape($item['name']), $item['style']);
Chris@0 228 }
Chris@0 229
Chris@0 230 /**
Chris@0 231 * Validate that input options make sense, provide defaults when called without options.
Chris@0 232 *
Chris@0 233 * @throws RuntimeException if options are inconsistent
Chris@0 234 *
Chris@0 235 * @param InputInterface $input
Chris@0 236 */
Chris@0 237 private function validateInput(InputInterface $input)
Chris@0 238 {
Chris@0 239 if (!$input->getArgument('target')) {
Chris@0 240 // if no target is passed, there can be no properties or methods
Chris@0 241 foreach (array('properties', 'methods', 'no-inherit') as $option) {
Chris@0 242 if ($input->getOption($option)) {
Chris@0 243 throw new RuntimeException('--' . $option . ' does not make sense without a specified target.');
Chris@0 244 }
Chris@0 245 }
Chris@0 246
Chris@0 247 foreach (array('globals', 'vars', 'constants', 'functions', 'classes', 'interfaces', 'traits') as $option) {
Chris@0 248 if ($input->getOption($option)) {
Chris@0 249 return;
Chris@0 250 }
Chris@0 251 }
Chris@0 252
Chris@0 253 // default to --vars if no other options are passed
Chris@0 254 $input->setOption('vars', true);
Chris@0 255 } else {
Chris@0 256 // if a target is passed, classes, functions, etc don't make sense
Chris@0 257 foreach (array('vars', 'globals', 'functions', 'classes', 'interfaces', 'traits') as $option) {
Chris@0 258 if ($input->getOption($option)) {
Chris@0 259 throw new RuntimeException('--' . $option . ' does not make sense with a specified target.');
Chris@0 260 }
Chris@0 261 }
Chris@0 262
Chris@0 263 foreach (array('constants', 'properties', 'methods') as $option) {
Chris@0 264 if ($input->getOption($option)) {
Chris@0 265 return;
Chris@0 266 }
Chris@0 267 }
Chris@0 268
Chris@0 269 // default to --constants --properties --methods if no other options are passed
Chris@0 270 $input->setOption('constants', true);
Chris@0 271 $input->setOption('properties', true);
Chris@0 272 $input->setOption('methods', true);
Chris@0 273 }
Chris@0 274 }
Chris@0 275 }