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