MarkdownDescriptor.php 4.9 KB

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