|
1
|
|
|
<?php |
|
2
|
|
|
namespace Zicht\Tool\Command\Descriptor; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Application; |
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
use Symfony\Component\Console\Descriptor\TextDescriptor as BaseDescriptor; |
|
9
|
|
|
use Symfony\Component\Console\Descriptor\ApplicationDescription; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
*/ |
|
13
|
|
|
class TextDescriptor extends BaseDescriptor |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param \Symfony\Component\Console\Input\InputOption $option |
|
17
|
|
|
* @return bool |
|
18
|
|
|
*/ |
|
19
|
9 |
|
public function isHiddenOption(InputOption $option) |
|
20
|
|
|
{ |
|
21
|
9 |
|
return in_array( |
|
22
|
9 |
|
$option->getName(), |
|
23
|
9 |
|
array('help', 'version', 'verbose', 'quiet', 'explain', 'force', 'plugin', 'debug') |
|
24
|
9 |
|
); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
1 |
|
protected function describeCommand(Command $command, array $options = array()) |
|
31
|
|
|
{ |
|
32
|
|
|
list($public, $internal) = $this->splitDefinition($command->getNativeDefinition()); |
|
33
|
|
|
|
|
34
|
|
|
$synopsis = new Command($command->getName()); |
|
35
|
|
|
$synopsis->setDefinition($public); |
|
36
|
|
|
|
|
37
|
|
|
$this->writeText('<comment>Usage:</comment>', $options); |
|
38
|
|
|
$this->writeText("\n"); |
|
39
|
|
|
$this->writeText(' ' . $synopsis->getSynopsis(), $options); |
|
40
|
|
|
$this->writeText("\n"); |
|
41
|
|
|
|
|
42
|
|
|
if (count($command->getAliases()) > 0) { |
|
43
|
|
|
$this->writeText("\n"); |
|
44
|
|
|
$this->writeText('<comment>Aliases:</comment> <info>' . implode(', ', $command->getAliases()) . '</info>', $options); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->writeText("\n"); |
|
48
|
1 |
|
$this->describeInputDefinition($public, $options); |
|
49
|
|
|
if (count($internal->getOptions())) { |
|
50
|
|
|
$this->writeText("\n\n" . '<comment>Global options:</comment>' . "\n"); |
|
51
|
|
|
$this->writeText(' Following global options are available for all task commands:'); |
|
52
|
|
|
$this->writeText(' ' . join(', ', array_map(function ($o) { return $o->getName(); }, $internal->getOptions()))); |
|
53
|
|
|
$this->writeText(' Read the application help for more info' . "\n"); |
|
54
|
|
|
} |
|
55
|
|
|
$this->writeText("\n"); |
|
56
|
|
|
|
|
57
|
|
|
if ($help = $command->getProcessedHelp()) { |
|
58
|
|
|
$this->writeText('<comment>Help:</comment>', $options); |
|
59
|
|
|
$this->writeText("\n"); |
|
60
|
|
|
$this->writeText(' ' . str_replace("\n", "\n ", $help), $options); |
|
61
|
|
|
$this->writeText("\n"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->writeText("\n"); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Splits the definition in an internal and public one, the latter representing whatever is shown to the user, |
|
69
|
|
|
* the former representing the options that are hidden. |
|
70
|
|
|
* |
|
71
|
|
|
* @param \Symfony\Component\Console\Input\InputDefinition $definition |
|
72
|
|
|
* @return InputDefinition[] |
|
73
|
|
|
*/ |
|
74
|
|
|
public function splitDefinition(InputDefinition $definition) |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var InputDefinition[] $ret */ |
|
77
|
|
|
$ret = array( |
|
78
|
|
|
new InputDefinition(), |
|
79
|
|
|
new InputDefinition() |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
foreach ($definition->getArguments() as $arg) { |
|
83
|
|
|
$ret[0]->addArgument($arg); |
|
84
|
|
|
$ret[1]->addArgument($arg); |
|
85
|
|
|
} |
|
86
|
|
|
foreach ($definition->getOptions() as $opt) { |
|
87
|
|
|
$ret[$this->isHiddenOption($opt) ? 1 : 0]->addOption($opt); |
|
88
|
|
|
} |
|
89
|
|
|
return $ret; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param \Symfony\Component\Console\Application $application |
|
95
|
|
|
* @param array $options |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function describeApplication(Application $application, array $options = array()) |
|
98
|
|
|
{ |
|
99
|
|
|
$describedNamespace = isset($options['namespace']) ? $options['namespace'] : null; |
|
100
|
|
|
$description = new ApplicationDescription($application, $describedNamespace); |
|
101
|
|
|
|
|
102
|
|
|
$width = $this->getColumnWidth($description->getCommands()); |
|
103
|
|
|
|
|
104
|
|
|
$this->writeText($application->getHelp(), $options); |
|
105
|
|
|
$this->writeText("\n\n"); |
|
106
|
|
|
|
|
107
|
|
|
$this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options); |
|
108
|
|
|
$this->writeText("\n"); |
|
109
|
|
|
$this->writeText("\n"); |
|
110
|
|
|
|
|
111
|
|
|
if ($describedNamespace) { |
|
112
|
|
|
$this->writeText(sprintf("<comment>Available commands for the \"%s\" namespace:</comment>", $describedNamespace), $options); |
|
113
|
|
|
} else { |
|
114
|
|
|
$this->writeText('<comment>Available commands:</comment>', $options); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// add commands by namespace |
|
118
|
|
|
foreach ($description->getNamespaces() as $namespace) { |
|
119
|
|
|
if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { |
|
120
|
|
|
$this->writeText("\n"); |
|
121
|
|
|
$this->writeText('<comment>' . $namespace['id'] . '</comment>', $options); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
foreach ($namespace['commands'] as $name) { |
|
125
|
|
|
$this->writeText("\n"); |
|
126
|
|
|
$this->writeText(sprintf(" <info>%-${width}s</info> %s", $name, $description->getCommand($name)->getDescription()), $options); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$this->writeText("\n"); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Because for some inapparent reason, the parent's implementation is private. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $content |
|
137
|
|
|
* @param array $options |
|
138
|
|
|
*/ |
|
139
|
|
|
private function writeText($content, array $options = array()) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
$this->write( |
|
142
|
|
|
isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content, |
|
143
|
|
|
isset($options['raw_output']) ? !$options['raw_output'] : true |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Because for some inapparent reason, the parent's implementation is private. |
|
149
|
|
|
*/ |
|
150
|
|
|
private function getColumnWidth(array $commands) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
$width = 0; |
|
153
|
|
|
foreach ($commands as $command) { |
|
154
|
|
|
$width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $width + 2; |
|
158
|
|
|
} |
|
159
|
|
|
} |
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.