MarkdownDescriptor.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Descriptor;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\Helper;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputDefinition;
  16. use Symfony\Component\Console\Input\InputOption;
  17. /**
  18. * Markdown descriptor.
  19. *
  20. * @author Jean-François Simon <contact@jfsimon.fr>
  21. *
  22. * @internal
  23. */
  24. class MarkdownDescriptor extends Descriptor
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function describeInputArgument(InputArgument $argument, array $options = array())
  30. {
  31. $this->write(
  32. '**'.$argument->getName().':**'."\n\n"
  33. .'* Name: '.($argument->getName() ?: '<none>')."\n"
  34. .'* Is required: '.($argument->isRequired() ? 'yes' : 'no')."\n"
  35. .'* Is array: '.($argument->isArray() ? 'yes' : 'no')."\n"
  36. .'* Description: '.preg_replace('/\s*[\r\n]\s*/', "\n ", $argument->getDescription() ?: '<none>')."\n"
  37. .'* Default: `'.str_replace("\n", '', var_export($argument->getDefault(), true)).'`'
  38. );
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. protected function describeInputOption(InputOption $option, array $options = array())
  44. {
  45. $this->write(
  46. '**'.$option->getName().':**'."\n\n"
  47. .'* Name: `--'.$option->getName().'`'."\n"
  48. .'* Shortcut: '.($option->getShortcut() ? '`-'.str_replace('|', '|-', $option->getShortcut()).'`' : '<none>')."\n"
  49. .'* Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n"
  50. .'* Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n"
  51. .'* Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n"
  52. .'* Description: '.preg_replace('/\s*[\r\n]\s*/', "\n ", $option->getDescription() ?: '<none>')."\n"
  53. .'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`'
  54. );
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function describeInputDefinition(InputDefinition $definition, array $options = array())
  60. {
  61. if ($showArguments = \count($definition->getArguments()) > 0) {
  62. $this->write('### Arguments:');
  63. foreach ($definition->getArguments() as $argument) {
  64. $this->write("\n\n");
  65. $this->write($this->describeInputArgument($argument));
  66. }
  67. }
  68. if (\count($definition->getOptions()) > 0) {
  69. if ($showArguments) {
  70. $this->write("\n\n");
  71. }
  72. $this->write('### Options:');
  73. foreach ($definition->getOptions() as $option) {
  74. $this->write("\n\n");
  75. $this->write($this->describeInputOption($option));
  76. }
  77. }
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. protected function describeCommand(Command $command, array $options = array())
  83. {
  84. $command->getSynopsis();
  85. $command->mergeApplicationDefinition(false);
  86. $this->write(
  87. $command->getName()."\n"
  88. .str_repeat('-', Helper::strlen($command->getName()))."\n\n"
  89. .'* Description: '.($command->getDescription() ?: '<none>')."\n"
  90. .'* Usage:'."\n\n"
  91. .array_reduce(array_merge(array($command->getSynopsis()), $command->getAliases(), $command->getUsages()), function ($carry, $usage) {
  92. return $carry.' * `'.$usage.'`'."\n";
  93. })
  94. );
  95. if ($help = $command->getProcessedHelp()) {
  96. $this->write("\n");
  97. $this->write($help);
  98. }
  99. if ($command->getNativeDefinition()) {
  100. $this->write("\n\n");
  101. $this->describeInputDefinition($command->getNativeDefinition());
  102. }
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. protected function describeApplication(Application $application, array $options = array())
  108. {
  109. $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
  110. $description = new ApplicationDescription($application, $describedNamespace);
  111. $this->write($application->getName()."\n".str_repeat('=', Helper::strlen($application->getName())));
  112. foreach ($description->getNamespaces() as $namespace) {
  113. if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
  114. $this->write("\n\n");
  115. $this->write('**'.$namespace['id'].':**');
  116. }
  117. $this->write("\n\n");
  118. $this->write(implode("\n", array_map(function ($commandName) {
  119. return '* '.$commandName;
  120. }, $namespace['commands'])));
  121. }
  122. foreach ($description->getCommands() as $command) {
  123. $this->write("\n\n");
  124. $this->write($this->describeCommand($command));
  125. }
  126. }
  127. }