This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListCommandsCommand.php
183 lines (153 loc) · 4.54 KB
/
ListCommandsCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Command\Command;
use Ulrack\Command\Common\Command\InputInterface;
use Ulrack\Command\Common\Command\OutputInterface;
use Ulrack\Command\Common\Command\CommandInterface;
use Ulrack\Command\Common\Dao\CommandConfigurationInterface;
class ListCommandsCommand implements CommandInterface
{
/**
* Contains the current command configuration.
*
* @var CommandConfigurationInterface
*/
private $commandConfiguration;
/**
* Constructor.
*
* @param CommandConfigurationInterface $commandConfiguration
*/
public function __construct(
CommandConfigurationInterface $commandConfiguration
) {
$this->commandConfiguration = $commandConfiguration;
}
/**
* Executes the command.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
public function __invoke(
InputInterface $input,
OutputInterface $output
): void {
$output->outputText(
'Command list: ' . implode(' ', $input->getCommand()),
true,
'title'
);
$description = $this->commandConfiguration->getDescription();
if (!empty($description)) {
$output->outputText('Description: ' . $description);
}
// Output an empty line.
$output->writeLine('');
$output->outputExplainedList(
$this->constructCommandList($this->commandConfiguration),
'command-explained-list-key',
'command-explained-list-description'
);
if (count($this->commandConfiguration->getParameters()) > 0) {
$output->writeLine('');
$output->outputText(
'Parameters: ',
true,
'title'
);
$output->outputExplainedList(
$this->constructParametersList()
);
}
if (count($this->commandConfiguration->getFlags()) > 0) {
$output->writeLine('');
$output->outputText(
'Flags: ',
true,
'title'
);
$output->outputExplainedList(
$this->constructFlagsList()
);
}
}
/**
* Constructs the explained flags list.
*
* @return array
*/
private function constructFlagsList(): array
{
$list = [];
foreach ($this->commandConfiguration->getFlags() as $flag) {
$options = [];
if (isset($flag['long'])) {
$options[] = $flag['long'];
}
if (isset($flag['short'])) {
$options[] = $flag['short'];
}
$list[sprintf(
'[%s]',
implode('|', $options)
)] = $flag['description'] ?? '';
}
return $list;
}
/**
* Constructs the explained parameters list.
*
* @return array
*/
private function constructParametersList(): array
{
$list = [];
foreach ($this->commandConfiguration->getParameters() as $parameter) {
$options = [];
if (isset($parameter['long'])) {
$options[] = $parameter['long'];
}
if (isset($parameter['short'])) {
$options[] = $parameter['short'];
}
$list[sprintf(
'[%s](%s)%s',
implode('|', $options),
$parameter['type'],
isset($parameter['required']) && $parameter['required']
? '*'
: ''
)] = $parameter['description'] ?? '';
}
return $list;
}
/**
* Constructs the command list from the configuration.
*
* @param CommandConfigurationInterface $configuration
* @param string $prefix
*
* @return array
*/
private function constructCommandList(
CommandConfigurationInterface $configuration,
string $prefix = ''
): array {
$list = [];
foreach ($configuration->getCommands() as $command) {
$subConfiguration = $configuration->getCommand($command);
$list[$prefix . $command] = $subConfiguration->getDescription();
$list = array_merge($list, $this->constructCommandList(
$subConfiguration,
$prefix . $command . '.'
));
}
return $list;
}
}