Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Descriptor; Chris@0: Chris@0: use Symfony\Component\Console\Application; Chris@0: use Symfony\Component\Console\Command\Command; Chris@0: use Symfony\Component\Console\Helper\Helper; Chris@0: use Symfony\Component\Console\Input\InputArgument; Chris@0: use Symfony\Component\Console\Input\InputDefinition; Chris@0: use Symfony\Component\Console\Input\InputOption; Chris@14: use Symfony\Component\Console\Output\OutputInterface; Chris@0: Chris@0: /** Chris@0: * Markdown descriptor. Chris@0: * Chris@0: * @author Jean-François Simon Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class MarkdownDescriptor extends Descriptor Chris@0: { Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function describe(OutputInterface $output, $object, array $options = []) Chris@14: { Chris@14: $decorated = $output->isDecorated(); Chris@14: $output->setDecorated(false); Chris@14: Chris@14: parent::describe($output, $object, $options); Chris@14: Chris@14: $output->setDecorated($decorated); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: protected function write($content, $decorated = true) Chris@14: { Chris@14: parent::write($content, $decorated); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@17: protected function describeInputArgument(InputArgument $argument, array $options = []) Chris@0: { Chris@0: $this->write( Chris@14: '#### `'.($argument->getName() ?: '')."`\n\n" Chris@14: .($argument->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $argument->getDescription())."\n\n" : '') Chris@0: .'* Is required: '.($argument->isRequired() ? 'yes' : 'no')."\n" Chris@0: .'* Is array: '.($argument->isArray() ? 'yes' : 'no')."\n" Chris@0: .'* Default: `'.str_replace("\n", '', var_export($argument->getDefault(), true)).'`' Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: protected function describeInputOption(InputOption $option, array $options = []) Chris@0: { Chris@14: $name = '--'.$option->getName(); Chris@14: if ($option->getShortcut()) { Chris@16: $name .= '|-'.str_replace('|', '|-', $option->getShortcut()).''; Chris@14: } Chris@14: Chris@0: $this->write( Chris@14: '#### `'.$name.'`'."\n\n" Chris@14: .($option->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $option->getDescription())."\n\n" : '') Chris@0: .'* Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n" Chris@0: .'* Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n" Chris@0: .'* Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n" Chris@0: .'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`' Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: protected function describeInputDefinition(InputDefinition $definition, array $options = []) Chris@0: { Chris@17: if ($showArguments = \count($definition->getArguments()) > 0) { Chris@14: $this->write('### Arguments'); Chris@0: foreach ($definition->getArguments() as $argument) { Chris@0: $this->write("\n\n"); Chris@0: $this->write($this->describeInputArgument($argument)); Chris@0: } Chris@0: } Chris@0: Chris@17: if (\count($definition->getOptions()) > 0) { Chris@0: if ($showArguments) { Chris@0: $this->write("\n\n"); Chris@0: } Chris@0: Chris@14: $this->write('### Options'); Chris@0: foreach ($definition->getOptions() as $option) { Chris@0: $this->write("\n\n"); Chris@0: $this->write($this->describeInputOption($option)); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: protected function describeCommand(Command $command, array $options = []) Chris@0: { Chris@0: $command->getSynopsis(); Chris@0: $command->mergeApplicationDefinition(false); Chris@0: Chris@0: $this->write( Chris@14: '`'.$command->getName()."`\n" Chris@14: .str_repeat('-', Helper::strlen($command->getName()) + 2)."\n\n" Chris@14: .($command->getDescription() ? $command->getDescription()."\n\n" : '') Chris@14: .'### Usage'."\n\n" Chris@17: .array_reduce(array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()), function ($carry, $usage) { Chris@14: return $carry.'* `'.$usage.'`'."\n"; Chris@0: }) Chris@0: ); Chris@0: Chris@0: if ($help = $command->getProcessedHelp()) { Chris@0: $this->write("\n"); Chris@0: $this->write($help); Chris@0: } Chris@0: Chris@0: if ($command->getNativeDefinition()) { Chris@0: $this->write("\n\n"); Chris@0: $this->describeInputDefinition($command->getNativeDefinition()); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: protected function describeApplication(Application $application, array $options = []) Chris@0: { Chris@0: $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null; Chris@0: $description = new ApplicationDescription($application, $describedNamespace); Chris@14: $title = $this->getApplicationTitle($application); Chris@0: Chris@14: $this->write($title."\n".str_repeat('=', Helper::strlen($title))); Chris@0: Chris@0: foreach ($description->getNamespaces() as $namespace) { Chris@0: if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { Chris@0: $this->write("\n\n"); Chris@0: $this->write('**'.$namespace['id'].':**'); Chris@0: } Chris@0: Chris@0: $this->write("\n\n"); Chris@14: $this->write(implode("\n", array_map(function ($commandName) use ($description) { Chris@14: return sprintf('* [`%s`](#%s)', $commandName, str_replace(':', '', $description->getCommand($commandName)->getName())); Chris@0: }, $namespace['commands']))); Chris@0: } Chris@0: Chris@0: foreach ($description->getCommands() as $command) { Chris@0: $this->write("\n\n"); Chris@0: $this->write($this->describeCommand($command)); Chris@0: } Chris@0: } Chris@14: Chris@14: private function getApplicationTitle(Application $application) Chris@14: { Chris@14: if ('UNKNOWN' !== $application->getName()) { Chris@14: if ('UNKNOWN' !== $application->getVersion()) { Chris@14: return sprintf('%s %s', $application->getName(), $application->getVersion()); Chris@14: } Chris@14: Chris@14: return $application->getName(); Chris@14: } Chris@14: Chris@14: return 'Console Tool'; Chris@14: } Chris@0: }