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