| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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 Symfony\Component\Console\Descriptor; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Application; |
| 15 |
use Symfony\Component\Console\Command\Command; |
| 16 |
use Symfony\Component\Console\Helper\Helper; |
| 17 |
use Symfony\Component\Console\Input\InputArgument; |
| 18 |
use Symfony\Component\Console\Input\InputDefinition; |
| 19 |
use Symfony\Component\Console\Input\InputOption; |
| 20 |
use Symfony\Component\Console\Output\OutputInterface; |
| 21 |
|
| 22 |
/** |
| 23 |
* Markdown descriptor. |
| 24 |
* |
| 25 |
* @author Jean-François Simon <[email protected]> |
| 26 |
* |
| 27 |
* @internal |
| 28 |
*/ |
| 29 |
class MarkdownDescriptor extends Descriptor |
| 30 |
{ |
| 31 |
public function describe(OutputInterface $output, object $object, array $options = []): void |
| 32 |
{ |
| 33 |
$decorated = $output->isDecorated(); |
| 34 |
$output->setDecorated(false); |
| 35 |
|
| 36 |
parent::describe($output, $object, $options); |
| 37 |
|
| 38 |
$output->setDecorated($decorated); |
| 39 |
} |
| 40 |
|
| 41 |
protected function write(string $content, bool $decorated = true): void |
| 42 |
{ |
| 43 |
parent::write($content, $decorated); |
| 44 |
} |
| 45 |
|
| 46 |
protected function describeInputArgument(InputArgument $argument, array $options = []): void |
| 47 |
{ |
| 48 |
$this->write( |
| 49 |
'#### `'.($argument->getName() ?: '<none>')."`\n\n" |
| 50 |
.($argument->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $argument->getDescription())."\n\n" : '') |
| 51 |
.'* Is required: '.($argument->isRequired() ? 'yes' : 'no')."\n" |
| 52 |
.'* Is array: '.($argument->isArray() ? 'yes' : 'no')."\n" |
| 53 |
.'* Default: `'.str_replace("\n", '', var_export($argument->getDefault(), true)).'`' |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
protected function describeInputOption(InputOption $option, array $options = []): void |
| 58 |
{ |
| 59 |
$name = '--'.$option->getName(); |
| 60 |
if ($option->isNegatable()) { |
| 61 |
$name .= '|--no-'.$option->getName(); |
| 62 |
} |
| 63 |
if ($option->getShortcut()) { |
| 64 |
$name .= '|-'.str_replace('|', '|-', $option->getShortcut()).''; |
| 65 |
} |
| 66 |
|
| 67 |
$this->write( |
| 68 |
'#### `'.$name.'`'."\n\n" |
| 69 |
.($option->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $option->getDescription())."\n\n" : '') |
| 70 |
.'* Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n" |
| 71 |
.'* Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n" |
| 72 |
.'* Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n" |
| 73 |
.'* Is negatable: '.($option->isNegatable() ? 'yes' : 'no')."\n" |
| 74 |
.'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`' |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
protected function describeInputDefinition(InputDefinition $definition, array $options = []): void |
| 79 |
{ |
| 80 |
if ($showArguments = \count($definition->getArguments()) > 0) { |
| 81 |
$this->write('### Arguments'); |
| 82 |
foreach ($definition->getArguments() as $argument) { |
| 83 |
$this->write("\n\n"); |
| 84 |
$this->describeInputArgument($argument); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (\count($definition->getOptions()) > 0) { |
| 89 |
if ($showArguments) { |
| 90 |
$this->write("\n\n"); |
| 91 |
} |
| 92 |
|
| 93 |
$this->write('### Options'); |
| 94 |
foreach ($definition->getOptions() as $option) { |
| 95 |
$this->write("\n\n"); |
| 96 |
$this->describeInputOption($option); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
protected function describeCommand(Command $command, array $options = []): void |
| 102 |
{ |
| 103 |
if ($options['short'] ?? false) { |
| 104 |
$this->write( |
| 105 |
'`'.$command->getName()."`\n" |
| 106 |
.str_repeat('-', Helper::width($command->getName()) + 2)."\n\n" |
| 107 |
.($command->getDescription() ? $command->getDescription()."\n\n" : '') |
| 108 |
.'### Usage'."\n\n" |
| 109 |
.array_reduce($command->getAliases(), static fn ($carry, $usage) => $carry.'* `'.$usage.'`'."\n") |
| 110 |
); |
| 111 |
|
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$command->mergeApplicationDefinition(false); |
| 116 |
|
| 117 |
$this->write( |
| 118 |
'`'.$command->getName()."`\n" |
| 119 |
.str_repeat('-', Helper::width($command->getName()) + 2)."\n\n" |
| 120 |
.($command->getDescription() ? $command->getDescription()."\n\n" : '') |
| 121 |
.'### Usage'."\n\n" |
| 122 |
.array_reduce(array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()), static fn ($carry, $usage) => $carry.'* `'.$usage.'`'."\n") |
| 123 |
); |
| 124 |
|
| 125 |
if ($help = $command->getProcessedHelp()) { |
| 126 |
$this->write("\n"); |
| 127 |
$this->write($help); |
| 128 |
} |
| 129 |
|
| 130 |
$definition = $command->getDefinition(); |
| 131 |
if ($definition->getOptions() || $definition->getArguments()) { |
| 132 |
$this->write("\n\n"); |
| 133 |
$this->describeInputDefinition($definition); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
protected function describeApplication(Application $application, array $options = []): void |
| 138 |
{ |
| 139 |
$describedNamespace = $options['namespace'] ?? null; |
| 140 |
$description = new ApplicationDescription($application, $describedNamespace); |
| 141 |
$title = $this->getApplicationTitle($application); |
| 142 |
|
| 143 |
$this->write($title."\n".str_repeat('=', Helper::width($title))); |
| 144 |
|
| 145 |
foreach ($description->getNamespaces() as $namespace) { |
| 146 |
if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { |
| 147 |
$this->write("\n\n"); |
| 148 |
$this->write('**'.$namespace['id'].':**'); |
| 149 |
} |
| 150 |
|
| 151 |
$this->write("\n\n"); |
| 152 |
$this->write(implode("\n", array_map(static fn ($commandName) => \sprintf('* [`%s`](#%s)', $commandName, str_replace(':', '', $description->getCommand($commandName)->getName())), $namespace['commands']))); |
| 153 |
} |
| 154 |
|
| 155 |
foreach ($description->getCommands() as $command) { |
| 156 |
$this->write("\n\n"); |
| 157 |
$this->describeCommand($command, $options); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
private function getApplicationTitle(Application $application): string |
| 162 |
{ |
| 163 |
if ('UNKNOWN' !== $application->getName()) { |
| 164 |
if ('UNKNOWN' !== $application->getVersion()) { |
| 165 |
return \sprintf('%s %s', $application->getName(), $application->getVersion()); |
| 166 |
} |
| 167 |
|
| 168 |
return $application->getName(); |
| 169 |
} |
| 170 |
|
| 171 |
return 'Console Tool'; |
| 172 |
} |
| 173 |
} |