| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Console\Descriptor;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Console\Application;
|
|
|
15 |
use Symfony\Component\Console\Command\Command;
|
|
|
16 |
use Symfony\Component\Console\Helper\Helper;
|
|
|
17 |
use Symfony\Component\Console\Input\InputArgument;
|
|
|
18 |
use Symfony\Component\Console\Input\InputDefinition;
|
|
|
19 |
use Symfony\Component\Console\Input\InputOption;
|
|
|
20 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Markdown descriptor.
|
|
|
24 |
*
|
|
|
25 |
* @author Jean-François Simon <contact@jfsimon.fr>
|
|
|
26 |
*
|
|
|
27 |
* @internal
|
|
|
28 |
*/
|
|
|
29 |
class MarkdownDescriptor extends Descriptor
|
|
|
30 |
{
|
|
|
31 |
public function describe(OutputInterface $output, object $object, array $options = [])
|
|
|
32 |
{
|
|
|
33 |
$decorated = $output->isDecorated();
|
|
|
34 |
$output->setDecorated(false);
|
|
|
35 |
|
|
|
36 |
parent::describe($output, $object, $options);
|
|
|
37 |
|
|
|
38 |
$output->setDecorated($decorated);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
protected function write(string $content, bool $decorated = true)
|
|
|
42 |
{
|
|
|
43 |
parent::write($content, $decorated);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
protected function describeInputArgument(InputArgument $argument, array $options = [])
|
|
|
47 |
{
|
|
|
48 |
$this->write(
|
|
|
49 |
'#### `'.($argument->getName() ?: '<none>')."`\n\n"
|
|
|
50 |
.($argument->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $argument->getDescription())."\n\n" : '')
|
|
|
51 |
.'* Is required: '.($argument->isRequired() ? 'yes' : 'no')."\n"
|
|
|
52 |
.'* Is array: '.($argument->isArray() ? 'yes' : 'no')."\n"
|
|
|
53 |
.'* Default: `'.str_replace("\n", '', var_export($argument->getDefault(), true)).'`'
|
|
|
54 |
);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
protected function describeInputOption(InputOption $option, array $options = [])
|
|
|
58 |
{
|
|
|
59 |
$name = '--'.$option->getName();
|
|
|
60 |
if ($option->isNegatable()) {
|
|
|
61 |
$name .= '|--no-'.$option->getName();
|
|
|
62 |
}
|
|
|
63 |
if ($option->getShortcut()) {
|
|
|
64 |
$name .= '|-'.str_replace('|', '|-', $option->getShortcut()).'';
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$this->write(
|
|
|
68 |
'#### `'.$name.'`'."\n\n"
|
|
|
69 |
.($option->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $option->getDescription())."\n\n" : '')
|
|
|
70 |
.'* Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n"
|
|
|
71 |
.'* Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n"
|
|
|
72 |
.'* Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n"
|
|
|
73 |
.'* Is negatable: '.($option->isNegatable() ? 'yes' : 'no')."\n"
|
|
|
74 |
.'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`'
|
|
|
75 |
);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
protected function describeInputDefinition(InputDefinition $definition, array $options = [])
|
|
|
79 |
{
|
|
|
80 |
if ($showArguments = \count($definition->getArguments()) > 0) {
|
|
|
81 |
$this->write('### Arguments');
|
|
|
82 |
foreach ($definition->getArguments() as $argument) {
|
|
|
83 |
$this->write("\n\n");
|
|
|
84 |
if (null !== $describeInputArgument = $this->describeInputArgument($argument)) {
|
|
|
85 |
$this->write($describeInputArgument);
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
if (\count($definition->getOptions()) > 0) {
|
|
|
91 |
if ($showArguments) {
|
|
|
92 |
$this->write("\n\n");
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
$this->write('### Options');
|
|
|
96 |
foreach ($definition->getOptions() as $option) {
|
|
|
97 |
$this->write("\n\n");
|
|
|
98 |
if (null !== $describeInputOption = $this->describeInputOption($option)) {
|
|
|
99 |
$this->write($describeInputOption);
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
protected function describeCommand(Command $command, array $options = [])
|
|
|
106 |
{
|
|
|
107 |
if ($options['short'] ?? false) {
|
|
|
108 |
$this->write(
|
|
|
109 |
'`'.$command->getName()."`\n"
|
|
|
110 |
.str_repeat('-', Helper::width($command->getName()) + 2)."\n\n"
|
|
|
111 |
.($command->getDescription() ? $command->getDescription()."\n\n" : '')
|
|
|
112 |
.'### Usage'."\n\n"
|
|
|
113 |
.array_reduce($command->getAliases(), function ($carry, $usage) {
|
|
|
114 |
return $carry.'* `'.$usage.'`'."\n";
|
|
|
115 |
})
|
|
|
116 |
);
|
|
|
117 |
|
|
|
118 |
return;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
$command->mergeApplicationDefinition(false);
|
|
|
122 |
|
|
|
123 |
$this->write(
|
|
|
124 |
'`'.$command->getName()."`\n"
|
|
|
125 |
.str_repeat('-', Helper::width($command->getName()) + 2)."\n\n"
|
|
|
126 |
.($command->getDescription() ? $command->getDescription()."\n\n" : '')
|
|
|
127 |
.'### Usage'."\n\n"
|
|
|
128 |
.array_reduce(array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()), function ($carry, $usage) {
|
|
|
129 |
return $carry.'* `'.$usage.'`'."\n";
|
|
|
130 |
})
|
|
|
131 |
);
|
|
|
132 |
|
|
|
133 |
if ($help = $command->getProcessedHelp()) {
|
|
|
134 |
$this->write("\n");
|
|
|
135 |
$this->write($help);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
$definition = $command->getDefinition();
|
|
|
139 |
if ($definition->getOptions() || $definition->getArguments()) {
|
|
|
140 |
$this->write("\n\n");
|
|
|
141 |
$this->describeInputDefinition($definition);
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
protected function describeApplication(Application $application, array $options = [])
|
|
|
146 |
{
|
|
|
147 |
$describedNamespace = $options['namespace'] ?? null;
|
|
|
148 |
$description = new ApplicationDescription($application, $describedNamespace);
|
|
|
149 |
$title = $this->getApplicationTitle($application);
|
|
|
150 |
|
|
|
151 |
$this->write($title."\n".str_repeat('=', Helper::width($title)));
|
|
|
152 |
|
|
|
153 |
foreach ($description->getNamespaces() as $namespace) {
|
|
|
154 |
if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
|
|
|
155 |
$this->write("\n\n");
|
|
|
156 |
$this->write('**'.$namespace['id'].':**');
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$this->write("\n\n");
|
|
|
160 |
$this->write(implode("\n", array_map(function ($commandName) use ($description) {
|
|
|
161 |
return sprintf('* [`%s`](#%s)', $commandName, str_replace(':', '', $description->getCommand($commandName)->getName()));
|
|
|
162 |
}, $namespace['commands'])));
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
foreach ($description->getCommands() as $command) {
|
|
|
166 |
$this->write("\n\n");
|
|
|
167 |
if (null !== $describeCommand = $this->describeCommand($command, $options)) {
|
|
|
168 |
$this->write($describeCommand);
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
private function getApplicationTitle(Application $application): string
|
|
|
174 |
{
|
|
|
175 |
if ('UNKNOWN' !== $application->getName()) {
|
|
|
176 |
if ('UNKNOWN' !== $application->getVersion()) {
|
|
|
177 |
return sprintf('%s %s', $application->getName(), $application->getVersion());
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
return $application->getName();
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
return 'Console Tool';
|
|
|
184 |
}
|
|
|
185 |
}
|