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