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@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@0: protected function describeInputArgument(InputArgument $argument, array $options = array()) Chris@0: { Chris@0: $this->write( Chris@0: '**'.$argument->getName().':**'."\n\n" Chris@0: .'* Name: '.($argument->getName() ?: '')."\n" Chris@0: .'* Is required: '.($argument->isRequired() ? 'yes' : 'no')."\n" Chris@0: .'* Is array: '.($argument->isArray() ? 'yes' : 'no')."\n" Chris@0: .'* Description: '.preg_replace('/\s*[\r\n]\s*/', "\n ", $argument->getDescription() ?: '')."\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@0: protected function describeInputOption(InputOption $option, array $options = array()) Chris@0: { Chris@0: $this->write( Chris@0: '**'.$option->getName().':**'."\n\n" Chris@0: .'* Name: `--'.$option->getName().'`'."\n" Chris@0: .'* Shortcut: '.($option->getShortcut() ? '`-'.implode('|-', explode('|', $option->getShortcut())).'`' : '')."\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: .'* Description: '.preg_replace('/\s*[\r\n]\s*/', "\n ", $option->getDescription() ?: '')."\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@0: protected function describeInputDefinition(InputDefinition $definition, array $options = array()) Chris@0: { Chris@0: if ($showArguments = count($definition->getArguments()) > 0) { Chris@0: $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@0: if (count($definition->getOptions()) > 0) { Chris@0: if ($showArguments) { Chris@0: $this->write("\n\n"); Chris@0: } Chris@0: Chris@0: $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@0: protected function describeCommand(Command $command, array $options = array()) Chris@0: { Chris@0: $command->getSynopsis(); Chris@0: $command->mergeApplicationDefinition(false); Chris@0: Chris@0: $this->write( Chris@0: $command->getName()."\n" Chris@0: .str_repeat('-', Helper::strlen($command->getName()))."\n\n" Chris@0: .'* Description: '.($command->getDescription() ?: '')."\n" Chris@0: .'* Usage:'."\n\n" Chris@0: .array_reduce(array_merge(array($command->getSynopsis()), $command->getAliases(), $command->getUsages()), function ($carry, $usage) { Chris@0: 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@0: protected function describeApplication(Application $application, array $options = array()) Chris@0: { Chris@0: $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null; Chris@0: $description = new ApplicationDescription($application, $describedNamespace); Chris@0: Chris@0: $this->write($application->getName()."\n".str_repeat('=', Helper::strlen($application->getName()))); 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@0: $this->write(implode("\n", array_map(function ($commandName) { Chris@0: return '* '.$commandName; 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@0: }