annotate vendor/symfony/console/Descriptor/TextDescriptor.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Console\Descriptor;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Application;
Chris@0 15 use Symfony\Component\Console\Command\Command;
Chris@0 16 use Symfony\Component\Console\Formatter\OutputFormatter;
Chris@0 17 use Symfony\Component\Console\Helper\Helper;
Chris@0 18 use Symfony\Component\Console\Input\InputArgument;
Chris@0 19 use Symfony\Component\Console\Input\InputDefinition;
Chris@0 20 use Symfony\Component\Console\Input\InputOption;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Text descriptor.
Chris@0 24 *
Chris@0 25 * @author Jean-François Simon <contact@jfsimon.fr>
Chris@0 26 *
Chris@0 27 * @internal
Chris@0 28 */
Chris@0 29 class TextDescriptor extends Descriptor
Chris@0 30 {
Chris@0 31 /**
Chris@0 32 * {@inheritdoc}
Chris@0 33 */
Chris@0 34 protected function describeInputArgument(InputArgument $argument, array $options = array())
Chris@0 35 {
Chris@0 36 if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
Chris@0 37 $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($argument->getDefault()));
Chris@0 38 } else {
Chris@0 39 $default = '';
Chris@0 40 }
Chris@0 41
Chris@0 42 $totalWidth = isset($options['total_width']) ? $options['total_width'] : Helper::strlen($argument->getName());
Chris@0 43 $spacingWidth = $totalWidth - strlen($argument->getName());
Chris@0 44
Chris@0 45 $this->writeText(sprintf(' <info>%s</info> %s%s%s',
Chris@0 46 $argument->getName(),
Chris@0 47 str_repeat(' ', $spacingWidth),
Chris@0 48 // + 4 = 2 spaces before <info>, 2 spaces after </info>
Chris@0 49 preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $argument->getDescription()),
Chris@0 50 $default
Chris@0 51 ), $options);
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * {@inheritdoc}
Chris@0 56 */
Chris@0 57 protected function describeInputOption(InputOption $option, array $options = array())
Chris@0 58 {
Chris@0 59 if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
Chris@0 60 $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault()));
Chris@0 61 } else {
Chris@0 62 $default = '';
Chris@0 63 }
Chris@0 64
Chris@0 65 $value = '';
Chris@0 66 if ($option->acceptValue()) {
Chris@0 67 $value = '='.strtoupper($option->getName());
Chris@0 68
Chris@0 69 if ($option->isValueOptional()) {
Chris@0 70 $value = '['.$value.']';
Chris@0 71 }
Chris@0 72 }
Chris@0 73
Chris@0 74 $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions(array($option));
Chris@0 75 $synopsis = sprintf('%s%s',
Chris@0 76 $option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : ' ',
Chris@0 77 sprintf('--%s%s', $option->getName(), $value)
Chris@0 78 );
Chris@0 79
Chris@0 80 $spacingWidth = $totalWidth - Helper::strlen($synopsis);
Chris@0 81
Chris@0 82 $this->writeText(sprintf(' <info>%s</info> %s%s%s%s',
Chris@0 83 $synopsis,
Chris@0 84 str_repeat(' ', $spacingWidth),
Chris@0 85 // + 4 = 2 spaces before <info>, 2 spaces after </info>
Chris@0 86 preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $option->getDescription()),
Chris@0 87 $default,
Chris@0 88 $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''
Chris@0 89 ), $options);
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * {@inheritdoc}
Chris@0 94 */
Chris@0 95 protected function describeInputDefinition(InputDefinition $definition, array $options = array())
Chris@0 96 {
Chris@0 97 $totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
Chris@0 98 foreach ($definition->getArguments() as $argument) {
Chris@0 99 $totalWidth = max($totalWidth, Helper::strlen($argument->getName()));
Chris@0 100 }
Chris@0 101
Chris@0 102 if ($definition->getArguments()) {
Chris@0 103 $this->writeText('<comment>Arguments:</comment>', $options);
Chris@0 104 $this->writeText("\n");
Chris@0 105 foreach ($definition->getArguments() as $argument) {
Chris@0 106 $this->describeInputArgument($argument, array_merge($options, array('total_width' => $totalWidth)));
Chris@0 107 $this->writeText("\n");
Chris@0 108 }
Chris@0 109 }
Chris@0 110
Chris@0 111 if ($definition->getArguments() && $definition->getOptions()) {
Chris@0 112 $this->writeText("\n");
Chris@0 113 }
Chris@0 114
Chris@0 115 if ($definition->getOptions()) {
Chris@0 116 $laterOptions = array();
Chris@0 117
Chris@0 118 $this->writeText('<comment>Options:</comment>', $options);
Chris@0 119 foreach ($definition->getOptions() as $option) {
Chris@0 120 if (strlen($option->getShortcut()) > 1) {
Chris@0 121 $laterOptions[] = $option;
Chris@0 122 continue;
Chris@0 123 }
Chris@0 124 $this->writeText("\n");
Chris@0 125 $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
Chris@0 126 }
Chris@0 127 foreach ($laterOptions as $option) {
Chris@0 128 $this->writeText("\n");
Chris@0 129 $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
Chris@0 130 }
Chris@0 131 }
Chris@0 132 }
Chris@0 133
Chris@0 134 /**
Chris@0 135 * {@inheritdoc}
Chris@0 136 */
Chris@0 137 protected function describeCommand(Command $command, array $options = array())
Chris@0 138 {
Chris@0 139 $command->getSynopsis(true);
Chris@0 140 $command->getSynopsis(false);
Chris@0 141 $command->mergeApplicationDefinition(false);
Chris@0 142
Chris@0 143 $this->writeText('<comment>Usage:</comment>', $options);
Chris@0 144 foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) {
Chris@0 145 $this->writeText("\n");
Chris@0 146 $this->writeText(' '.$usage, $options);
Chris@0 147 }
Chris@0 148 $this->writeText("\n");
Chris@0 149
Chris@0 150 $definition = $command->getNativeDefinition();
Chris@0 151 if ($definition->getOptions() || $definition->getArguments()) {
Chris@0 152 $this->writeText("\n");
Chris@0 153 $this->describeInputDefinition($definition, $options);
Chris@0 154 $this->writeText("\n");
Chris@0 155 }
Chris@0 156
Chris@0 157 if ($help = $command->getProcessedHelp()) {
Chris@0 158 $this->writeText("\n");
Chris@0 159 $this->writeText('<comment>Help:</comment>', $options);
Chris@0 160 $this->writeText("\n");
Chris@0 161 $this->writeText(' '.str_replace("\n", "\n ", $help), $options);
Chris@0 162 $this->writeText("\n");
Chris@0 163 }
Chris@0 164 }
Chris@0 165
Chris@0 166 /**
Chris@0 167 * {@inheritdoc}
Chris@0 168 */
Chris@0 169 protected function describeApplication(Application $application, array $options = array())
Chris@0 170 {
Chris@0 171 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
Chris@0 172 $description = new ApplicationDescription($application, $describedNamespace);
Chris@0 173
Chris@0 174 if (isset($options['raw_text']) && $options['raw_text']) {
Chris@0 175 $width = $this->getColumnWidth($description->getCommands());
Chris@0 176
Chris@0 177 foreach ($description->getCommands() as $command) {
Chris@0 178 $this->writeText(sprintf("%-{$width}s %s", $command->getName(), $command->getDescription()), $options);
Chris@0 179 $this->writeText("\n");
Chris@0 180 }
Chris@0 181 } else {
Chris@0 182 if ('' != $help = $application->getHelp()) {
Chris@0 183 $this->writeText("$help\n\n", $options);
Chris@0 184 }
Chris@0 185
Chris@0 186 $this->writeText("<comment>Usage:</comment>\n", $options);
Chris@0 187 $this->writeText(" command [options] [arguments]\n\n", $options);
Chris@0 188
Chris@0 189 $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options);
Chris@0 190
Chris@0 191 $this->writeText("\n");
Chris@0 192 $this->writeText("\n");
Chris@0 193
Chris@0 194 $width = $this->getColumnWidth($description->getCommands());
Chris@0 195
Chris@0 196 if ($describedNamespace) {
Chris@0 197 $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
Chris@0 198 } else {
Chris@0 199 $this->writeText('<comment>Available commands:</comment>', $options);
Chris@0 200 }
Chris@0 201
Chris@0 202 // add commands by namespace
Chris@0 203 $commands = $description->getCommands();
Chris@0 204
Chris@0 205 foreach ($description->getNamespaces() as $namespace) {
Chris@0 206 if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
Chris@0 207 $this->writeText("\n");
Chris@0 208 $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options);
Chris@0 209 }
Chris@0 210
Chris@0 211 foreach ($namespace['commands'] as $name) {
Chris@0 212 if (isset($commands[$name])) {
Chris@0 213 $this->writeText("\n");
Chris@0 214 $spacingWidth = $width - Helper::strlen($name);
Chris@0 215 $command = $commands[$name];
Chris@0 216 $commandAliases = $this->getCommandAliasesText($command);
Chris@0 217 $this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options);
Chris@0 218 }
Chris@0 219 }
Chris@0 220 }
Chris@0 221
Chris@0 222 $this->writeText("\n");
Chris@0 223 }
Chris@0 224 }
Chris@0 225
Chris@0 226 /**
Chris@0 227 * {@inheritdoc}
Chris@0 228 */
Chris@0 229 private function writeText($content, array $options = array())
Chris@0 230 {
Chris@0 231 $this->write(
Chris@0 232 isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
Chris@0 233 isset($options['raw_output']) ? !$options['raw_output'] : true
Chris@0 234 );
Chris@0 235 }
Chris@0 236
Chris@0 237 /**
Chris@0 238 * Formats command aliases to show them in the command description.
Chris@0 239 *
Chris@0 240 * @param Command $command
Chris@0 241 *
Chris@0 242 * @return string
Chris@0 243 */
Chris@0 244 private function getCommandAliasesText($command)
Chris@0 245 {
Chris@0 246 $text = '';
Chris@0 247 $aliases = $command->getAliases();
Chris@0 248
Chris@0 249 if ($aliases) {
Chris@0 250 $text = '['.implode('|', $aliases).'] ';
Chris@0 251 }
Chris@0 252
Chris@0 253 return $text;
Chris@0 254 }
Chris@0 255
Chris@0 256 /**
Chris@0 257 * Formats input option/argument default value.
Chris@0 258 *
Chris@0 259 * @param mixed $default
Chris@0 260 *
Chris@0 261 * @return string
Chris@0 262 */
Chris@0 263 private function formatDefaultValue($default)
Chris@0 264 {
Chris@0 265 if (is_string($default)) {
Chris@0 266 $default = OutputFormatter::escape($default);
Chris@0 267 } elseif (is_array($default)) {
Chris@0 268 foreach ($default as $key => $value) {
Chris@0 269 if (is_string($value)) {
Chris@0 270 $default[$key] = OutputFormatter::escape($value);
Chris@0 271 }
Chris@0 272 }
Chris@0 273 }
Chris@0 274
Chris@0 275 return str_replace('\\\\', '\\', json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
Chris@0 276 }
Chris@0 277
Chris@0 278 /**
Chris@0 279 * @param Command[] $commands
Chris@0 280 *
Chris@0 281 * @return int
Chris@0 282 */
Chris@0 283 private function getColumnWidth(array $commands)
Chris@0 284 {
Chris@0 285 $widths = array();
Chris@0 286
Chris@0 287 foreach ($commands as $command) {
Chris@0 288 $widths[] = Helper::strlen($command->getName());
Chris@0 289 foreach ($command->getAliases() as $alias) {
Chris@0 290 $widths[] = Helper::strlen($alias);
Chris@0 291 }
Chris@0 292 }
Chris@0 293
Chris@0 294 return max($widths) + 2;
Chris@0 295 }
Chris@0 296
Chris@0 297 /**
Chris@0 298 * @param InputOption[] $options
Chris@0 299 *
Chris@0 300 * @return int
Chris@0 301 */
Chris@0 302 private function calculateTotalWidthForOptions($options)
Chris@0 303 {
Chris@0 304 $totalWidth = 0;
Chris@0 305 foreach ($options as $option) {
Chris@0 306 // "-" + shortcut + ", --" + name
Chris@0 307 $nameLength = 1 + max(Helper::strlen($option->getShortcut()), 1) + 4 + Helper::strlen($option->getName());
Chris@0 308
Chris@0 309 if ($option->acceptValue()) {
Chris@0 310 $valueLength = 1 + Helper::strlen($option->getName()); // = + value
Chris@0 311 $valueLength += $option->isValueOptional() ? 2 : 0; // [ + ]
Chris@0 312
Chris@0 313 $nameLength += $valueLength;
Chris@0 314 }
Chris@0 315 $totalWidth = max($totalWidth, $nameLength);
Chris@0 316 }
Chris@0 317
Chris@0 318 return $totalWidth;
Chris@0 319 }
Chris@0 320 }