4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command;
14 use Psy\Command\ListCommand\ClassConstantEnumerator;
15 use Psy\Command\ListCommand\ClassEnumerator;
16 use Psy\Command\ListCommand\ConstantEnumerator;
17 use Psy\Command\ListCommand\FunctionEnumerator;
18 use Psy\Command\ListCommand\GlobalVariableEnumerator;
19 use Psy\Command\ListCommand\MethodEnumerator;
20 use Psy\Command\ListCommand\PropertyEnumerator;
21 use Psy\Command\ListCommand\VariableEnumerator;
22 use Psy\Exception\RuntimeException;
23 use Psy\Input\CodeArgument;
24 use Psy\Input\FilterOptions;
25 use Psy\VarDumper\Presenter;
26 use Psy\VarDumper\PresenterAware;
27 use Symfony\Component\Console\Formatter\OutputFormatter;
28 use Symfony\Component\Console\Helper\TableHelper;
29 use Symfony\Component\Console\Input\InputInterface;
30 use Symfony\Component\Console\Input\InputOption;
31 use Symfony\Component\Console\Output\OutputInterface;
34 * List available local variables, object properties, etc.
36 class ListCommand extends ReflectingCommand implements PresenterAware
39 protected $enumerators;
42 * PresenterAware interface.
44 * @param Presenter $presenter
46 public function setPresenter(Presenter $presenter)
48 $this->presenter = $presenter;
54 protected function configure()
56 list($grep, $insensitive, $invert) = FilterOptions::getOptions();
60 ->setAliases(['list', 'dir'])
62 new CodeArgument('target', CodeArgument::OPTIONAL, 'A target class or object to list.'),
64 new InputOption('vars', '', InputOption::VALUE_NONE, 'Display variables.'),
65 new InputOption('constants', 'c', InputOption::VALUE_NONE, 'Display defined constants.'),
66 new InputOption('functions', 'f', InputOption::VALUE_NONE, 'Display defined functions.'),
67 new InputOption('classes', 'k', InputOption::VALUE_NONE, 'Display declared classes.'),
68 new InputOption('interfaces', 'I', InputOption::VALUE_NONE, 'Display declared interfaces.'),
69 new InputOption('traits', 't', InputOption::VALUE_NONE, 'Display declared traits.'),
71 new InputOption('no-inherit', '', InputOption::VALUE_NONE, 'Exclude inherited methods, properties and constants.'),
73 new InputOption('properties', 'p', InputOption::VALUE_NONE, 'Display class or object properties (public properties by default).'),
74 new InputOption('methods', 'm', InputOption::VALUE_NONE, 'Display class or object methods (public methods by default).'),
80 new InputOption('globals', 'g', InputOption::VALUE_NONE, 'Include global variables.'),
81 new InputOption('internal', 'n', InputOption::VALUE_NONE, 'Limit to internal functions and classes.'),
82 new InputOption('user', 'u', InputOption::VALUE_NONE, 'Limit to user-defined constants, functions and classes.'),
83 new InputOption('category', 'C', InputOption::VALUE_REQUIRED, 'Limit to constants in a specific category (e.g. "date").'),
85 new InputOption('all', 'a', InputOption::VALUE_NONE, 'Include private and protected methods and properties.'),
86 new InputOption('long', 'l', InputOption::VALUE_NONE, 'List in long format: includes class names and method signatures.'),
88 ->setDescription('List local, instance or class variables, methods and constants.')
91 List variables, constants, classes, interfaces, traits, functions, methods,
94 Called without options, this will return a list of variables currently in scope.
96 If a target object is provided, list properties, constants and methods of that
97 target. If a class, interface or trait name is passed instead, list constants
98 and methods on that class.
101 <return>>>> ls</return>
102 <return>>>> ls $foo</return>
103 <return>>>> ls -k --grep mongo -i</return>
104 <return>>>> ls -al ReflectionClass</return>
105 <return>>>> ls --constants --category date</return>
106 <return>>>> ls -l --functions --grep /^array_.*/</return>
107 <return>>>> ls -l --properties new DateTime()</return>
115 protected function execute(InputInterface $input, OutputInterface $output)
117 $this->validateInput($input);
118 $this->initEnumerators();
120 $method = $input->getOption('long') ? 'writeLong' : 'write';
122 if ($target = $input->getArgument('target')) {
123 list($target, $reflector) = $this->getTargetAndReflector($target);
128 // @todo something cleaner than this :-/
129 if ($input->getOption('long')) {
130 $output->startPaging();
133 foreach ($this->enumerators as $enumerator) {
134 $this->$method($output, $enumerator->enumerate($input, $reflector, $target));
137 if ($input->getOption('long')) {
138 $output->stopPaging();
141 // Set some magic local variables
142 if ($reflector !== null) {
143 $this->setCommandScopeVariables($reflector);
148 * Initialize Enumerators.
150 protected function initEnumerators()
152 if (!isset($this->enumerators)) {
153 $mgr = $this->presenter;
155 $this->enumerators = [
156 new ClassConstantEnumerator($mgr),
157 new ClassEnumerator($mgr),
158 new ConstantEnumerator($mgr),
159 new FunctionEnumerator($mgr),
160 new GlobalVariableEnumerator($mgr),
161 new PropertyEnumerator($mgr),
162 new MethodEnumerator($mgr),
163 new VariableEnumerator($mgr, $this->context),
169 * Write the list items to $output.
171 * @param OutputInterface $output
172 * @param null|array $result List of enumerated items
174 protected function write(OutputInterface $output, array $result = null)
176 if ($result === null) {
180 foreach ($result as $label => $items) {
181 $names = array_map([$this, 'formatItemName'], $items);
182 $output->writeln(sprintf('<strong>%s</strong>: %s', $label, implode(', ', $names)));
187 * Write the list items to $output.
189 * Items are listed one per line, and include the item signature.
191 * @param OutputInterface $output
192 * @param null|array $result List of enumerated items
194 protected function writeLong(OutputInterface $output, array $result = null)
196 if ($result === null) {
200 $table = $this->getTable($output);
202 foreach ($result as $label => $items) {
203 $output->writeln('');
204 $output->writeln(sprintf('<strong>%s:</strong>', $label));
207 foreach ($items as $item) {
208 $table->addRow([$this->formatItemName($item), $item['value']]);
211 if ($table instanceof TableHelper) {
212 $table->render($output);
220 * Format an item name given its visibility.
226 private function formatItemName($item)
228 return sprintf('<%s>%s</%s>', $item['style'], OutputFormatter::escape($item['name']), $item['style']);
232 * Validate that input options make sense, provide defaults when called without options.
234 * @throws RuntimeException if options are inconsistent
236 * @param InputInterface $input
238 private function validateInput(InputInterface $input)
240 if (!$input->getArgument('target')) {
241 // if no target is passed, there can be no properties or methods
242 foreach (['properties', 'methods', 'no-inherit'] as $option) {
243 if ($input->getOption($option)) {
244 throw new RuntimeException('--' . $option . ' does not make sense without a specified target');
248 foreach (['globals', 'vars', 'constants', 'functions', 'classes', 'interfaces', 'traits'] as $option) {
249 if ($input->getOption($option)) {
254 // default to --vars if no other options are passed
255 $input->setOption('vars', true);
257 // if a target is passed, classes, functions, etc don't make sense
258 foreach (['vars', 'globals', 'functions', 'classes', 'interfaces', 'traits'] as $option) {
259 if ($input->getOption($option)) {
260 throw new RuntimeException('--' . $option . ' does not make sense with a specified target');
264 foreach (['constants', 'properties', 'methods'] as $option) {
265 if ($input->getOption($option)) {
270 // default to --constants --properties --methods if no other options are passed
271 $input->setOption('constants', true);
272 $input->setOption('properties', true);
273 $input->setOption('methods', true);