1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Vasoft\VersionIncrement\Core; |
6
|
|
|
|
7
|
|
|
use Vasoft\VersionIncrement\Contract\ApplicationHandlerInterface; |
8
|
|
|
|
9
|
|
|
class Help implements ApplicationHandlerInterface |
10
|
|
|
{ |
11
|
|
|
public const SECTION_KEYS = 'Keys'; |
12
|
|
|
public const SECTION_TYPES = 'Type'; |
13
|
|
|
public const KEY = '--help'; |
14
|
|
|
/** |
15
|
|
|
* @var ApplicationHandlerInterface[] |
16
|
|
|
*/ |
17
|
|
|
private array $handlers = []; |
18
|
|
|
|
19
|
12 |
|
public function handle(array $argv): ?int |
20
|
|
|
{ |
21
|
12 |
|
if (!in_array(self::KEY, $argv, true)) { |
22
|
8 |
|
return null; |
23
|
|
|
} |
24
|
4 |
|
$this->display(); |
25
|
|
|
|
26
|
4 |
|
return 0; |
27
|
|
|
} |
28
|
|
|
|
29
|
4 |
|
private function display(): void |
30
|
|
|
{ |
31
|
|
|
echo <<<'HELP' |
32
|
|
|
Vasoft Semantic Version Increment |
33
|
|
|
run vs-version-increment [keys] [type] |
34
|
|
|
|
35
|
|
|
HELP; |
36
|
4 |
|
$this->displaySections(); |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
private function displaySections(): void |
40
|
|
|
{ |
41
|
4 |
|
$registeredRows = $this->getRows(); |
42
|
4 |
|
foreach ($registeredRows as $sectionName => $rows) { |
43
|
4 |
|
echo $sectionName, ':', PHP_EOL; |
44
|
4 |
|
$this->displayRows($rows); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param HelpRow[] $rows |
50
|
|
|
*/ |
51
|
4 |
|
private function displayRows(array $rows): void |
52
|
|
|
{ |
53
|
4 |
|
$maxKeyLength = array_reduce( |
54
|
4 |
|
$rows, |
55
|
4 |
|
static fn($result, HelpRow $row): int => max($result, strlen($row->key)), |
56
|
4 |
|
5, |
57
|
4 |
|
); |
58
|
4 |
|
$existsKeys = []; |
59
|
4 |
|
foreach ($rows as $row) { |
60
|
4 |
|
$key = trim($row->key); |
61
|
4 |
|
if ('' !== $key) { |
62
|
4 |
|
if (isset($existsKeys[$key])) { |
63
|
1 |
|
continue; |
64
|
|
|
} |
65
|
4 |
|
$existsKeys[$key] = true; |
66
|
|
|
} |
67
|
4 |
|
$formattedKey = str_pad($key, $maxKeyLength); |
68
|
4 |
|
echo ' ', $formattedKey . str_repeat(' ', 3), $row->description, PHP_EOL; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
private function getRows(): array |
73
|
|
|
{ |
74
|
4 |
|
$sections = []; |
75
|
|
|
|
76
|
4 |
|
foreach ($this->handlers as $handler) { |
77
|
3 |
|
$rows = $handler->getHelp(); |
78
|
3 |
|
foreach ($rows as $row) { |
79
|
3 |
|
$sections[$row->section][] = $row; |
80
|
|
|
} |
81
|
|
|
} |
82
|
4 |
|
$sections[self::SECTION_KEYS][] = new HelpRow(self::SECTION_KEYS, self::KEY, 'Display this help message'); |
83
|
|
|
|
84
|
4 |
|
return $sections; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param ApplicationHandlerInterface[] $handlers |
89
|
|
|
*/ |
90
|
10 |
|
public function registerHandlersFromArray(array $handlers): void |
91
|
|
|
{ |
92
|
10 |
|
$this->handlers = $handlers; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function getHelp(): array |
96
|
|
|
{ |
97
|
1 |
|
return []; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|