4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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\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;
36 * List available local variables, object properties, etc.
38 class ListCommand extends ReflectingCommand implements PresenterAware
41 protected $enumerators;
44 * PresenterAware interface.
46 * @param Presenter $manager
48 public function setPresenter(Presenter $presenter)
50 $this->presenter = $presenter;
56 protected function configure()
58 list($grep, $insensitive, $invert) = FilterOptions::getOptions();
62 ->setAliases(array('list', 'dir'))
63 ->setDefinition(array(
64 new InputArgument('target', InputArgument::OPTIONAL, 'A target class or object to list.', null),
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.'),
73 new InputOption('no-inherit', '', InputOption::VALUE_NONE, 'Exclude inherited methods, properties and constants.'),
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).'),
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").'),
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.'),
90 ->setDescription('List local, instance or class variables, methods and constants.')
93 List variables, constants, classes, interfaces, traits, functions, methods,
96 Called without options, this will return a list of variables currently in scope.
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.
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>
116 protected function execute(InputInterface $input, OutputInterface $output)
118 $this->validateInput($input);
119 $this->initEnumerators();
121 $method = $input->getOption('long') ? 'writeLong' : 'write';
123 if ($target = $input->getArgument('target')) {
124 list($target, $reflector) = $this->getTargetAndReflector($target, true);
129 // @todo something cleaner than this :-/
130 if ($input->getOption('long')) {
131 $output->startPaging();
134 foreach ($this->enumerators as $enumerator) {
135 $this->$method($output, $enumerator->enumerate($input, $reflector, $target));
138 if ($input->getOption('long')) {
139 $output->stopPaging();
142 // Set some magic local variables
143 if ($reflector !== null) {
144 $this->setCommandScopeVariables($reflector);
149 * Initialize Enumerators.
151 protected function initEnumerators()
153 if (!isset($this->enumerators)) {
154 $mgr = $this->presenter;
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),
172 * Write the list items to $output.
174 * @param OutputInterface $output
175 * @param null|array $result List of enumerated items
177 protected function write(OutputInterface $output, array $result = null)
179 if ($result === null) {
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)));
190 * Write the list items to $output.
192 * Items are listed one per line, and include the item signature.
194 * @param OutputInterface $output
195 * @param null|array $result List of enumerated items
197 protected function writeLong(OutputInterface $output, array $result = null)
199 if ($result === null) {
203 $table = $this->getTable($output);
205 foreach ($result as $label => $items) {
206 $output->writeln('');
207 $output->writeln(sprintf('<strong>%s:</strong>', $label));
209 $table->setRows(array());
210 foreach ($items as $item) {
211 $table->addRow(array($this->formatItemName($item), $item['value']));
214 if ($table instanceof TableHelper) {
215 $table->render($output);
223 * Format an item name given its visibility.
229 private function formatItemName($item)
231 return sprintf('<%s>%s</%s>', $item['style'], OutputFormatter::escape($item['name']), $item['style']);
235 * Validate that input options make sense, provide defaults when called without options.
237 * @throws RuntimeException if options are inconsistent
239 * @param InputInterface $input
241 private function validateInput(InputInterface $input)
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.');
251 foreach (array('globals', 'vars', 'constants', 'functions', 'classes', 'interfaces', 'traits') as $option) {
252 if ($input->getOption($option)) {
257 // default to --vars if no other options are passed
258 $input->setOption('vars', true);
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.');
267 foreach (array('constants', 'properties', 'methods') as $option) {
268 if ($input->getOption($option)) {
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);