Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of Psy Shell.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) 2012-2017 Justin Hileman
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Psy\Command;
|
Chris@0
|
13
|
Chris@0
|
14 use Psy\Shell;
|
Chris@0
|
15 use Symfony\Component\Console\Application;
|
Chris@0
|
16 use Symfony\Component\Console\Command\Command as BaseCommand;
|
Chris@0
|
17 use Symfony\Component\Console\Helper\Table;
|
Chris@0
|
18 use Symfony\Component\Console\Helper\TableHelper;
|
Chris@0
|
19 use Symfony\Component\Console\Helper\TableStyle;
|
Chris@0
|
20 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The Psy Shell base command.
|
Chris@0
|
24 */
|
Chris@0
|
25 abstract class Command extends BaseCommand
|
Chris@0
|
26 {
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Sets the application instance for this command.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @param Application $application An Application instance
|
Chris@0
|
31 *
|
Chris@0
|
32 * @api
|
Chris@0
|
33 */
|
Chris@0
|
34 public function setApplication(Application $application = null)
|
Chris@0
|
35 {
|
Chris@0
|
36 if ($application !== null && !$application instanceof Shell) {
|
Chris@0
|
37 throw new \InvalidArgumentException('PsySH Commands require an instance of Psy\Shell.');
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 return parent::setApplication($application);
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * {@inheritdoc}
|
Chris@0
|
45 */
|
Chris@0
|
46 public function asText()
|
Chris@0
|
47 {
|
Chris@0
|
48 $messages = array(
|
Chris@0
|
49 '<comment>Usage:</comment>',
|
Chris@0
|
50 ' ' . $this->getSynopsis(),
|
Chris@0
|
51 '',
|
Chris@0
|
52 );
|
Chris@0
|
53
|
Chris@0
|
54 if ($this->getAliases()) {
|
Chris@0
|
55 $messages[] = $this->aliasesAsText();
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 if ($this->getArguments()) {
|
Chris@0
|
59 $messages[] = $this->argumentsAsText();
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 if ($this->getOptions()) {
|
Chris@0
|
63 $messages[] = $this->optionsAsText();
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 if ($help = $this->getProcessedHelp()) {
|
Chris@0
|
67 $messages[] = '<comment>Help:</comment>';
|
Chris@0
|
68 $messages[] = ' ' . str_replace("\n", "\n ", $help) . "\n";
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 return implode("\n", $messages);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * {@inheritdoc}
|
Chris@0
|
76 */
|
Chris@0
|
77 private function getArguments()
|
Chris@0
|
78 {
|
Chris@0
|
79 $hidden = $this->getHiddenArguments();
|
Chris@0
|
80
|
Chris@0
|
81 return array_filter($this->getNativeDefinition()->getArguments(), function ($argument) use ($hidden) {
|
Chris@0
|
82 return !in_array($argument->getName(), $hidden);
|
Chris@0
|
83 });
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * These arguments will be excluded from help output.
|
Chris@0
|
88 *
|
Chris@0
|
89 * @return array
|
Chris@0
|
90 */
|
Chris@0
|
91 protected function getHiddenArguments()
|
Chris@0
|
92 {
|
Chris@0
|
93 return array('command');
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * {@inheritdoc}
|
Chris@0
|
98 */
|
Chris@0
|
99 private function getOptions()
|
Chris@0
|
100 {
|
Chris@0
|
101 $hidden = $this->getHiddenOptions();
|
Chris@0
|
102
|
Chris@0
|
103 return array_filter($this->getNativeDefinition()->getOptions(), function ($option) use ($hidden) {
|
Chris@0
|
104 return !in_array($option->getName(), $hidden);
|
Chris@0
|
105 });
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * These options will be excluded from help output.
|
Chris@0
|
110 *
|
Chris@0
|
111 * @return array
|
Chris@0
|
112 */
|
Chris@0
|
113 protected function getHiddenOptions()
|
Chris@0
|
114 {
|
Chris@0
|
115 return array('verbose');
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * Format command aliases as text..
|
Chris@0
|
120 *
|
Chris@0
|
121 * @return string
|
Chris@0
|
122 */
|
Chris@0
|
123 private function aliasesAsText()
|
Chris@0
|
124 {
|
Chris@0
|
125 return '<comment>Aliases:</comment> <info>' . implode(', ', $this->getAliases()) . '</info>' . PHP_EOL;
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * Format command arguments as text.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @return string
|
Chris@0
|
132 */
|
Chris@0
|
133 private function argumentsAsText()
|
Chris@0
|
134 {
|
Chris@0
|
135 $max = $this->getMaxWidth();
|
Chris@0
|
136 $messages = array();
|
Chris@0
|
137
|
Chris@0
|
138 $arguments = $this->getArguments();
|
Chris@0
|
139 if (!empty($arguments)) {
|
Chris@0
|
140 $messages[] = '<comment>Arguments:</comment>';
|
Chris@0
|
141 foreach ($arguments as $argument) {
|
Chris@0
|
142 if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
|
Chris@0
|
143 $default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($argument->getDefault()));
|
Chris@0
|
144 } else {
|
Chris@0
|
145 $default = '';
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 $description = str_replace("\n", "\n" . str_pad('', $max + 2, ' '), $argument->getDescription());
|
Chris@0
|
149
|
Chris@0
|
150 $messages[] = sprintf(" <info>%-${max}s</info> %s%s", $argument->getName(), $description, $default);
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 $messages[] = '';
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 return implode(PHP_EOL, $messages);
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * Format options as text.
|
Chris@0
|
161 *
|
Chris@0
|
162 * @return string
|
Chris@0
|
163 */
|
Chris@0
|
164 private function optionsAsText()
|
Chris@0
|
165 {
|
Chris@0
|
166 $max = $this->getMaxWidth();
|
Chris@0
|
167 $messages = array();
|
Chris@0
|
168
|
Chris@0
|
169 $options = $this->getOptions();
|
Chris@0
|
170 if ($options) {
|
Chris@0
|
171 $messages[] = '<comment>Options:</comment>';
|
Chris@0
|
172
|
Chris@0
|
173 foreach ($options as $option) {
|
Chris@0
|
174 if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
|
Chris@0
|
175 $default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($option->getDefault()));
|
Chris@0
|
176 } else {
|
Chris@0
|
177 $default = '';
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 $multiple = $option->isArray() ? '<comment> (multiple values allowed)</comment>' : '';
|
Chris@0
|
181 $description = str_replace("\n", "\n" . str_pad('', $max + 2, ' '), $option->getDescription());
|
Chris@0
|
182
|
Chris@0
|
183 $optionMax = $max - strlen($option->getName()) - 2;
|
Chris@0
|
184 $messages[] = sprintf(
|
Chris@0
|
185 " <info>%s</info> %-${optionMax}s%s%s%s",
|
Chris@0
|
186 '--' . $option->getName(),
|
Chris@0
|
187 $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '',
|
Chris@0
|
188 $description,
|
Chris@0
|
189 $default,
|
Chris@0
|
190 $multiple
|
Chris@0
|
191 );
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 $messages[] = '';
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 return implode(PHP_EOL, $messages);
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * Calculate the maximum padding width for a set of lines.
|
Chris@0
|
202 *
|
Chris@0
|
203 * @return int
|
Chris@0
|
204 */
|
Chris@0
|
205 private function getMaxWidth()
|
Chris@0
|
206 {
|
Chris@0
|
207 $max = 0;
|
Chris@0
|
208
|
Chris@0
|
209 foreach ($this->getOptions() as $option) {
|
Chris@0
|
210 $nameLength = strlen($option->getName()) + 2;
|
Chris@0
|
211 if ($option->getShortcut()) {
|
Chris@0
|
212 $nameLength += strlen($option->getShortcut()) + 3;
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 $max = max($max, $nameLength);
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 foreach ($this->getArguments() as $argument) {
|
Chris@0
|
219 $max = max($max, strlen($argument->getName()));
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 return ++$max;
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 /**
|
Chris@0
|
226 * Format an option default as text.
|
Chris@0
|
227 *
|
Chris@0
|
228 * @param mixed $default
|
Chris@0
|
229 *
|
Chris@0
|
230 * @return string
|
Chris@0
|
231 */
|
Chris@0
|
232 private function formatDefaultValue($default)
|
Chris@0
|
233 {
|
Chris@0
|
234 if (is_array($default) && $default === array_values($default)) {
|
Chris@0
|
235 return sprintf("array('%s')", implode("', '", $default));
|
Chris@0
|
236 }
|
Chris@0
|
237
|
Chris@0
|
238 return str_replace("\n", '', var_export($default, true));
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 /**
|
Chris@0
|
242 * Get a Table instance.
|
Chris@0
|
243 *
|
Chris@0
|
244 * Falls back to legacy TableHelper.
|
Chris@0
|
245 *
|
Chris@0
|
246 * @return Table|TableHelper
|
Chris@0
|
247 */
|
Chris@0
|
248 protected function getTable(OutputInterface $output)
|
Chris@0
|
249 {
|
Chris@0
|
250 if (!class_exists('Symfony\Component\Console\Helper\Table')) {
|
Chris@0
|
251 return $this->getTableHelper();
|
Chris@0
|
252 }
|
Chris@0
|
253
|
Chris@0
|
254 $style = new TableStyle();
|
Chris@0
|
255 $style
|
Chris@0
|
256 ->setVerticalBorderChar(' ')
|
Chris@0
|
257 ->setHorizontalBorderChar('')
|
Chris@0
|
258 ->setCrossingChar('');
|
Chris@0
|
259
|
Chris@0
|
260 $table = new Table($output);
|
Chris@0
|
261
|
Chris@0
|
262 return $table
|
Chris@0
|
263 ->setRows(array())
|
Chris@0
|
264 ->setStyle($style);
|
Chris@0
|
265 }
|
Chris@0
|
266
|
Chris@0
|
267 /**
|
Chris@0
|
268 * Legacy fallback for getTable.
|
Chris@0
|
269 *
|
Chris@0
|
270 * @return TableHelper
|
Chris@0
|
271 */
|
Chris@0
|
272 protected function getTableHelper()
|
Chris@0
|
273 {
|
Chris@0
|
274 $table = $this->getApplication()->getHelperSet()->get('table');
|
Chris@0
|
275
|
Chris@0
|
276 return $table
|
Chris@0
|
277 ->setRows(array())
|
Chris@0
|
278 ->setLayout(TableHelper::LAYOUT_BORDERLESS)
|
Chris@0
|
279 ->setHorizontalBorderChar('')
|
Chris@0
|
280 ->setCrossingChar('');
|
Chris@0
|
281 }
|
Chris@0
|
282 }
|