comparison vendor/symfony/console/Descriptor/TextDescriptor.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
141 $command->mergeApplicationDefinition(false); 141 $command->mergeApplicationDefinition(false);
142 142
143 $this->writeText('<comment>Usage:</comment>', $options); 143 $this->writeText('<comment>Usage:</comment>', $options);
144 foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) { 144 foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) {
145 $this->writeText("\n"); 145 $this->writeText("\n");
146 $this->writeText(' '.$usage, $options); 146 $this->writeText(' '.OutputFormatter::escape($usage), $options);
147 } 147 }
148 $this->writeText("\n"); 148 $this->writeText("\n");
149 149
150 $definition = $command->getNativeDefinition(); 150 $definition = $command->getNativeDefinition();
151 if ($definition->getOptions() || $definition->getArguments()) { 151 if ($definition->getOptions() || $definition->getArguments()) {
189 $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options); 189 $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options);
190 190
191 $this->writeText("\n"); 191 $this->writeText("\n");
192 $this->writeText("\n"); 192 $this->writeText("\n");
193 193
194 $width = $this->getColumnWidth($description->getCommands()); 194 $commands = $description->getCommands();
195 $namespaces = $description->getNamespaces();
196 if ($describedNamespace && $namespaces) {
197 // make sure all alias commands are included when describing a specific namespace
198 $describedNamespaceInfo = reset($namespaces);
199 foreach ($describedNamespaceInfo['commands'] as $name) {
200 $commands[$name] = $description->getCommand($name);
201 }
202 }
203
204 // calculate max. width based on available commands per namespace
205 $width = $this->getColumnWidth(call_user_func_array('array_merge', array_map(function ($namespace) use ($commands) {
206 return array_intersect($namespace['commands'], array_keys($commands));
207 }, $namespaces)));
195 208
196 if ($describedNamespace) { 209 if ($describedNamespace) {
197 $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options); 210 $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
198 } else { 211 } else {
199 $this->writeText('<comment>Available commands:</comment>', $options); 212 $this->writeText('<comment>Available commands:</comment>', $options);
200 } 213 }
201 214
202 // add commands by namespace 215 foreach ($namespaces as $namespace) {
203 $commands = $description->getCommands(); 216 $namespace['commands'] = array_filter($namespace['commands'], function ($name) use ($commands) {
204 217 return isset($commands[$name]);
205 foreach ($description->getNamespaces() as $namespace) { 218 });
219
220 if (!$namespace['commands']) {
221 continue;
222 }
223
206 if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { 224 if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
207 $this->writeText("\n"); 225 $this->writeText("\n");
208 $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options); 226 $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options);
209 } 227 }
210 228
211 foreach ($namespace['commands'] as $name) { 229 foreach ($namespace['commands'] as $name) {
212 if (isset($commands[$name])) { 230 $this->writeText("\n");
213 $this->writeText("\n"); 231 $spacingWidth = $width - Helper::strlen($name);
214 $spacingWidth = $width - Helper::strlen($name); 232 $command = $commands[$name];
215 $command = $commands[$name]; 233 $commandAliases = $name === $command->getName() ? $this->getCommandAliasesText($command) : '';
216 $commandAliases = $this->getCommandAliasesText($command); 234 $this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options);
217 $this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options);
218 }
219 } 235 }
220 } 236 }
221 237
222 $this->writeText("\n"); 238 $this->writeText("\n");
223 } 239 }
235 } 251 }
236 252
237 /** 253 /**
238 * Formats command aliases to show them in the command description. 254 * Formats command aliases to show them in the command description.
239 * 255 *
240 * @param Command $command
241 *
242 * @return string 256 * @return string
243 */ 257 */
244 private function getCommandAliasesText($command) 258 private function getCommandAliasesText(Command $command)
245 { 259 {
246 $text = ''; 260 $text = '';
247 $aliases = $command->getAliases(); 261 $aliases = $command->getAliases();
248 262
249 if ($aliases) { 263 if ($aliases) {
278 292
279 return str_replace('\\\\', '\\', json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); 293 return str_replace('\\\\', '\\', json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
280 } 294 }
281 295
282 /** 296 /**
283 * @param Command[] $commands 297 * @param (Command|string)[] $commands
284 * 298 *
285 * @return int 299 * @return int
286 */ 300 */
287 private function getColumnWidth(array $commands) 301 private function getColumnWidth(array $commands)
288 { 302 {
289 $widths = array(); 303 $widths = array();
290 304
291 foreach ($commands as $command) { 305 foreach ($commands as $command) {
292 $widths[] = Helper::strlen($command->getName()); 306 if ($command instanceof Command) {
293 foreach ($command->getAliases() as $alias) { 307 $widths[] = Helper::strlen($command->getName());
294 $widths[] = Helper::strlen($alias); 308 foreach ($command->getAliases() as $alias) {
295 } 309 $widths[] = Helper::strlen($alias);
296 } 310 }
297 311 } else {
298 return max($widths) + 2; 312 $widths[] = Helper::strlen($command);
313 }
314 }
315
316 return $widths ? max($widths) + 2 : 0;
299 } 317 }
300 318
301 /** 319 /**
302 * @param InputOption[] $options 320 * @param InputOption[] $options
303 * 321 *
304 * @return int 322 * @return int
305 */ 323 */
306 private function calculateTotalWidthForOptions($options) 324 private function calculateTotalWidthForOptions(array $options)
307 { 325 {
308 $totalWidth = 0; 326 $totalWidth = 0;
309 foreach ($options as $option) { 327 foreach ($options as $option) {
310 // "-" + shortcut + ", --" + name 328 // "-" + shortcut + ", --" + name
311 $nameLength = 1 + max(Helper::strlen($option->getShortcut()), 1) + 4 + Helper::strlen($option->getName()); 329 $nameLength = 1 + max(Helper::strlen($option->getShortcut()), 1) + 4 + Helper::strlen($option->getName());