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