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