|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPCR\Shell\Console\Descriptor; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Descriptor\Descriptor; |
| 6 | +use Symfony\Component\Console\Input\InputArgument; |
| 7 | +use Symfony\Component\Console\Input\InputOption; |
| 8 | +use Symfony\Component\Console\Input\InputDefinition; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Application; |
| 11 | +use Symfony\Component\Console\Descriptor\ApplicationDescription; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class which dumps the command reference in RST format |
| 15 | + * for use by the official documentation. |
| 16 | + * |
| 17 | + * @author Daniel Leech <[email protected]> |
| 18 | + */ |
| 19 | +class RstDescriptor extends Descriptor |
| 20 | +{ |
| 21 | + protected $ignoreOptions = array( |
| 22 | + 'verbose', |
| 23 | + 'version', |
| 24 | + 'quiet', |
| 25 | + 'ansi', |
| 26 | + 'no-ansi', |
| 27 | + 'no-interaction', |
| 28 | + ); |
| 29 | + |
| 30 | + protected function getCommandRefName($name) |
| 31 | + { |
| 32 | + return 'phpcr_shell_command_' . str_replace(':', '', $name); |
| 33 | + } |
| 34 | + |
| 35 | + protected function underline($string, $char) |
| 36 | + { |
| 37 | + return str_repeat($char, strlen($string)); |
| 38 | + } |
| 39 | + |
| 40 | + protected function formatText($text) |
| 41 | + { |
| 42 | + $lines = explode("\n", $text); |
| 43 | + $newLines = array(); |
| 44 | + $blockLines = array(); |
| 45 | + |
| 46 | + foreach ($lines as $line) { |
| 47 | + // if line is indented by 2 or 4 spaces, assume |
| 48 | + // that it is a code block |
| 49 | + if (preg_match('{^[ | ]}', $line)) { |
| 50 | + $inBlock = true; |
| 51 | + $blockLines = array(); |
| 52 | + } else { |
| 53 | + $inBlock = false; |
| 54 | + } |
| 55 | + |
| 56 | + if (true === $inBlock) { |
| 57 | + $blockLines[] = $line; |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + if (false === $inBlock && $blockLines) { |
| 62 | + $newLines[] = ''; |
| 63 | + $newLines[] = '.. code-block:: bash'; |
| 64 | + $newLines[] = ''; |
| 65 | + foreach ($blockLines as $blockLine) { |
| 66 | + $blockLine = preg_replace('{( +)<(.*?)>(.*)</(.*)>}', '\3', $blockLine); |
| 67 | + $newLines[] = ' ' . $blockLine; |
| 68 | + } |
| 69 | + $newLines[] = ''; |
| 70 | + $blockLines = array(); |
| 71 | + } else { |
| 72 | + // replace inline tags with literals |
| 73 | + $line = preg_replace('{<(.*?)>(.*)</(.*)>}', '``\2``', $line); |
| 74 | + $newLines[] = $line; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + return implode("\n", $newLines); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@inheritdoc} |
| 84 | + */ |
| 85 | + protected function describeInputArgument(InputArgument $argument, array $options = array()) |
| 86 | + { |
| 87 | + return implode("\n", array( |
| 88 | + $argument->getName(), |
| 89 | + $this->underline($argument->getName(), '"'), |
| 90 | + '', |
| 91 | + '* **Name:** ``'. ($argument->getName() ?: '*<none>*').'``', |
| 92 | + '* **Is required:** '.($argument->isRequired() ? 'yes' : 'no'), |
| 93 | + '* **Is array:** '.($argument->isArray() ? 'yes' : 'no'), |
| 94 | + '* **Description:** '.($argument->getDescription() ?: '*<none>*'), |
| 95 | + '* **Default:** ``'.str_replace("\n", '', var_export($argument->getDefault(), true)).'``', |
| 96 | + '', |
| 97 | + )); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * {@inheritdoc} |
| 102 | + */ |
| 103 | + protected function describeInputOption(InputOption $option, array $options = array()) |
| 104 | + { |
| 105 | + if (in_array($option->getName(), $this->ignoreOptions)) { |
| 106 | + return ''; |
| 107 | + } |
| 108 | + |
| 109 | + return implode("\n", array( |
| 110 | + $option->getName(), |
| 111 | + $this->underline($option->getName(), '"'), |
| 112 | + '', |
| 113 | + '* **Name:** ``--'.$option->getName().'``', |
| 114 | + '* **Accept value:** '.($option->acceptValue() ? 'yes' : 'no'), |
| 115 | + '* **Is value required:** '.($option->isValueRequired() ? 'yes' : 'no'), |
| 116 | + '* **Is multiple:** '.($option->isArray() ? 'yes' : 'no'), |
| 117 | + '* **Description:** '.($option->getDescription() ?: '*<none>*'), |
| 118 | + '* **Default:** ``'.str_replace("\n", '', var_export($option->getDefault(), true)).'``', |
| 119 | + '', |
| 120 | + )); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * {@inheritdoc} |
| 125 | + */ |
| 126 | + protected function describeInputDefinition(InputDefinition $definition, array $options = array()) |
| 127 | + { |
| 128 | + $blocks = array(); |
| 129 | + |
| 130 | + if (count($definition->getArguments()) > 0) { |
| 131 | + $blocks[] = 'Arguments:'; |
| 132 | + $blocks[] = '~~~~~~~~~~'; |
| 133 | + $blocks[] = ''; |
| 134 | + foreach ($definition->getArguments() as $argument) { |
| 135 | + $blocks[] = $this->describeInputArgument($argument); |
| 136 | + } |
| 137 | + $blocks[] = ''; |
| 138 | + } |
| 139 | + |
| 140 | + if (count($definition->getOptions()) > 0) { |
| 141 | + $blocks[] = 'Options:'; |
| 142 | + $blocks[] = '~~~~~~~~'; |
| 143 | + $blocks[] = ''; |
| 144 | + foreach ($definition->getOptions() as $option) { |
| 145 | + $blocks[] = $this->describeInputOption($option); |
| 146 | + } |
| 147 | + $blocks[] = ''; |
| 148 | + } |
| 149 | + |
| 150 | + return implode("\n", $blocks); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * {@inheritdoc} |
| 155 | + */ |
| 156 | + protected function describeCommand(Command $command, array $options = array()) |
| 157 | + { |
| 158 | + $command->getSynopsis(); |
| 159 | + $command->mergeApplicationDefinition(false); |
| 160 | + |
| 161 | + $rst = array( |
| 162 | + '', |
| 163 | + '.. _' . $this->getCommandRefName($command->getName()) . ':', |
| 164 | + '', |
| 165 | + $command->getName(), |
| 166 | + $this->underline($command->getName(), '-'), |
| 167 | + '', |
| 168 | + '* **Description:** '.($command->getDescription() ? $this->formatText($command->getDescription()) : '*<none>*'), |
| 169 | + '* **Usage:** ``'.$command->getSynopsis().'``', |
| 170 | + ); |
| 171 | + |
| 172 | + $rst[] = ''; |
| 173 | + |
| 174 | + if ($help = $command->getProcessedHelp()) { |
| 175 | + $rst[] = $this->formatText($help); |
| 176 | + $rst[] = ''; |
| 177 | + } |
| 178 | + |
| 179 | + if ($definitionRst = $this->describeInputDefinition($command->getNativeDefinition())) { |
| 180 | + $rst[] = $this->formatText($definitionRst); |
| 181 | + $rst[] = ''; |
| 182 | + } |
| 183 | + |
| 184 | + return implode("\n", $rst); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * {@inheritdoc} |
| 189 | + */ |
| 190 | + protected function describeApplication(Application $application, array $options = array()) |
| 191 | + { |
| 192 | + $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null; |
| 193 | + $description = new ApplicationDescription($application, $describedNamespace); |
| 194 | + $blocks[] = 'Reference'; |
| 195 | + $blocks[] = '========='; |
| 196 | + $blocks[] = ''; |
| 197 | + |
| 198 | + foreach ($description->getNamespaces() as $namespace) { |
| 199 | + if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { |
| 200 | + $blocks[] = $namespace['id']; |
| 201 | + $blocks[] = str_repeat('-', strlen($namespace['id'])); |
| 202 | + $blocks[] = ''; |
| 203 | + } |
| 204 | + |
| 205 | + $blocks[] = implode("\n", array_map(function ($commandName) { |
| 206 | + return '* :ref:`' . $this->getCommandRefName($commandName) . '`'; |
| 207 | + } , $namespace['commands'])); |
| 208 | + $blocks[] = ''; |
| 209 | + } |
| 210 | + |
| 211 | + foreach ($description->getCommands() as $command) { |
| 212 | + $blocks[] = $this->describeCommand($command); |
| 213 | + } |
| 214 | + |
| 215 | + return implode("\n", $blocks); |
| 216 | + } |
| 217 | +} |
0 commit comments