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\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;
34 * List available local variables, object properties, etc.
36 class ListCommand extends ReflectingCommand implements PresenterAware
39 protected $enumerators;
42 * PresenterAware interface.
44 * @param Presenter $manager
46 public function setPresenter(Presenter $presenter)
48 $this->presenter = $presenter;
54 protected function configure()
56 list($grep, $insensitive, $invert) = FilterOptions::getOptions();
60 ->setAliases(array('list', 'dir'))
61 ->setDefinition(array(
62 new InputArgument('target', InputArgument::OPTIONAL, 'A target class or object to list.', null),
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>
114 protected function execute(InputInterface $input, OutputInterface $output)
116 $this->validateInput($input);
117 $this->initEnumerators();
119 $method = $input->getOption('long') ? 'writeLong' : 'write';
121 if ($target = $input->getArgument('target')) {
122 list($target, $reflector) = $this->getTargetAndReflector($target, true);
127 // @todo something cleaner than this :-/
128 if ($input->getOption('long')) {
129 $output->startPaging();
132 foreach ($this->enumerators as $enumerator) {
133 $this->$method($output, $enumerator->enumerate($input, $reflector, $target));
136 if ($input->getOption('long')) {
137 $output->stopPaging();
140 // Set some magic local variables
141 if ($reflector !== null) {
142 $this->setCommandScopeVariables($reflector);
147 * Initialize Enumerators.
149 protected function initEnumerators()
151 if (!isset($this->enumerators)) {
152 $mgr = $this->presenter;
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),
168 * Write the list items to $output.
170 * @param OutputInterface $output
171 * @param null|array $result List of enumerated items
173 protected function write(OutputInterface $output, array $result = null)
175 if ($result === null) {
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)));
186 * Write the list items to $output.
188 * Items are listed one per line, and include the item signature.
190 * @param OutputInterface $output
191 * @param null|array $result List of enumerated items
193 protected function writeLong(OutputInterface $output, array $result = null)
195 if ($result === null) {
199 $table = $this->getTable($output);
201 foreach ($result as $label => $items) {
202 $output->writeln('');
203 $output->writeln(sprintf('<strong>%s:</strong>', $label));
205 $table->setRows(array());
206 foreach ($items as $item) {
207 $table->addRow(array($this->formatItemName($item), $item['value']));
210 if ($table instanceof TableHelper) {
211 $table->render($output);
219 * Format an item name given its visibility.
225 private function formatItemName($item)
227 return sprintf('<%s>%s</%s>', $item['style'], OutputFormatter::escape($item['name']), $item['style']);
231 * Validate that input options make sense, provide defaults when called without options.
233 * @throws RuntimeException if options are inconsistent
235 * @param InputInterface $input
237 private function validateInput(InputInterface $input)
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.');
247 foreach (array('globals', 'vars', 'constants', 'functions', 'classes', 'interfaces', 'traits') as $option) {
248 if ($input->getOption($option)) {
253 // default to --vars if no other options are passed
254 $input->setOption('vars', true);
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.');
263 foreach (array('constants', 'properties', 'methods') as $option) {
264 if ($input->getOption($option)) {
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);