Mercurial > hg > cmmr2012-drupal-site
comparison vendor/psy/psysh/src/Command/ListCommand.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of Psy Shell. | |
5 * | |
6 * (c) 2012-2018 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\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; | |
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 $presenter | |
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(['list', 'dir']) | |
61 ->setDefinition([ | |
62 new CodeArgument('target', CodeArgument::OPTIONAL, 'A target class or object to list.'), | |
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 <return>>>> ls -l --properties new DateTime()</return> | |
108 HELP | |
109 ); | |
110 } | |
111 | |
112 /** | |
113 * {@inheritdoc} | |
114 */ | |
115 protected function execute(InputInterface $input, OutputInterface $output) | |
116 { | |
117 $this->validateInput($input); | |
118 $this->initEnumerators(); | |
119 | |
120 $method = $input->getOption('long') ? 'writeLong' : 'write'; | |
121 | |
122 if ($target = $input->getArgument('target')) { | |
123 list($target, $reflector) = $this->getTargetAndReflector($target); | |
124 } else { | |
125 $reflector = null; | |
126 } | |
127 | |
128 // @todo something cleaner than this :-/ | |
129 if ($input->getOption('long')) { | |
130 $output->startPaging(); | |
131 } | |
132 | |
133 foreach ($this->enumerators as $enumerator) { | |
134 $this->$method($output, $enumerator->enumerate($input, $reflector, $target)); | |
135 } | |
136 | |
137 if ($input->getOption('long')) { | |
138 $output->stopPaging(); | |
139 } | |
140 | |
141 // Set some magic local variables | |
142 if ($reflector !== null) { | |
143 $this->setCommandScopeVariables($reflector); | |
144 } | |
145 } | |
146 | |
147 /** | |
148 * Initialize Enumerators. | |
149 */ | |
150 protected function initEnumerators() | |
151 { | |
152 if (!isset($this->enumerators)) { | |
153 $mgr = $this->presenter; | |
154 | |
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), | |
164 ]; | |
165 } | |
166 } | |
167 | |
168 /** | |
169 * Write the list items to $output. | |
170 * | |
171 * @param OutputInterface $output | |
172 * @param null|array $result List of enumerated items | |
173 */ | |
174 protected function write(OutputInterface $output, array $result = null) | |
175 { | |
176 if ($result === null) { | |
177 return; | |
178 } | |
179 | |
180 foreach ($result as $label => $items) { | |
181 $names = array_map([$this, 'formatItemName'], $items); | |
182 $output->writeln(sprintf('<strong>%s</strong>: %s', $label, implode(', ', $names))); | |
183 } | |
184 } | |
185 | |
186 /** | |
187 * Write the list items to $output. | |
188 * | |
189 * Items are listed one per line, and include the item signature. | |
190 * | |
191 * @param OutputInterface $output | |
192 * @param null|array $result List of enumerated items | |
193 */ | |
194 protected function writeLong(OutputInterface $output, array $result = null) | |
195 { | |
196 if ($result === null) { | |
197 return; | |
198 } | |
199 | |
200 $table = $this->getTable($output); | |
201 | |
202 foreach ($result as $label => $items) { | |
203 $output->writeln(''); | |
204 $output->writeln(sprintf('<strong>%s:</strong>', $label)); | |
205 | |
206 $table->setRows([]); | |
207 foreach ($items as $item) { | |
208 $table->addRow([$this->formatItemName($item), $item['value']]); | |
209 } | |
210 | |
211 if ($table instanceof TableHelper) { | |
212 $table->render($output); | |
213 } else { | |
214 $table->render(); | |
215 } | |
216 } | |
217 } | |
218 | |
219 /** | |
220 * Format an item name given its visibility. | |
221 * | |
222 * @param array $item | |
223 * | |
224 * @return string | |
225 */ | |
226 private function formatItemName($item) | |
227 { | |
228 return sprintf('<%s>%s</%s>', $item['style'], OutputFormatter::escape($item['name']), $item['style']); | |
229 } | |
230 | |
231 /** | |
232 * Validate that input options make sense, provide defaults when called without options. | |
233 * | |
234 * @throws RuntimeException if options are inconsistent | |
235 * | |
236 * @param InputInterface $input | |
237 */ | |
238 private function validateInput(InputInterface $input) | |
239 { | |
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'); | |
245 } | |
246 } | |
247 | |
248 foreach (['globals', 'vars', 'constants', 'functions', 'classes', 'interfaces', 'traits'] as $option) { | |
249 if ($input->getOption($option)) { | |
250 return; | |
251 } | |
252 } | |
253 | |
254 // default to --vars if no other options are passed | |
255 $input->setOption('vars', true); | |
256 } else { | |
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'); | |
261 } | |
262 } | |
263 | |
264 foreach (['constants', 'properties', 'methods'] as $option) { | |
265 if ($input->getOption($option)) { | |
266 return; | |
267 } | |
268 } | |
269 | |
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); | |
274 } | |
275 } | |
276 } |