1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Conia\Cli; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
abstract class Command |
10
|
|
|
{ |
11
|
|
|
protected string $name = ''; |
12
|
|
|
protected string $group = ''; |
13
|
|
|
protected string $prefix = ''; |
14
|
|
|
protected string $description = ''; |
15
|
|
|
protected ?Output $output = null; |
16
|
|
|
|
17
|
|
|
abstract public function run(): string|int; |
18
|
|
|
|
19
|
14 |
|
public function name(): string |
20
|
|
|
{ |
21
|
14 |
|
return $this->name; |
22
|
|
|
} |
23
|
|
|
|
24
|
14 |
|
public function group(): string |
25
|
|
|
{ |
26
|
14 |
|
return $this->group; |
27
|
|
|
} |
28
|
|
|
|
29
|
14 |
|
public function prefix(): string |
30
|
|
|
{ |
31
|
14 |
|
return empty($this->prefix) ? strtolower($this->group) : $this->prefix; |
32
|
|
|
} |
33
|
|
|
|
34
|
4 |
|
public function description(): string |
35
|
|
|
{ |
36
|
4 |
|
return $this->description; |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
public function script(): string |
40
|
|
|
{ |
41
|
3 |
|
return $_SERVER['argv'][0] ?? ''; |
42
|
|
|
} |
43
|
|
|
|
44
|
6 |
|
public function output(Output $output): static |
45
|
|
|
{ |
46
|
6 |
|
$this->output = $output; |
47
|
|
|
|
48
|
6 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
5 |
|
public function echo(string $message): void |
52
|
|
|
{ |
53
|
5 |
|
if ($this->output) { |
54
|
4 |
|
$this->output->echo($message); |
55
|
|
|
|
56
|
4 |
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
throw new RuntimeException('Output missing'); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
public function color(string $text, string $color, string $background = null): string |
63
|
|
|
{ |
64
|
3 |
|
if ($this->output) { |
65
|
2 |
|
return $this->output->color($text, $color, $background); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
throw new RuntimeException('Output missing'); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
public function indent( |
72
|
|
|
string $text, |
73
|
|
|
int $indent, |
74
|
|
|
?int $max = null, |
75
|
|
|
): string { |
76
|
2 |
|
if ($this->output) { |
77
|
1 |
|
return $this->output->indent($text, $indent, $max); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
throw new RuntimeException('Output missing'); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function help(): void |
84
|
|
|
{ |
85
|
1 |
|
$this->helpHeader(withOptions: false); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
protected function helpHeader(bool $withOptions = false): void |
89
|
|
|
{ |
90
|
2 |
|
$script = $this->script(); |
91
|
2 |
|
$name = $this->name; |
92
|
2 |
|
$prefix = $this->prefix(); |
93
|
2 |
|
$desc = $this->description; |
94
|
|
|
|
95
|
2 |
|
if (!empty($desc)) { |
96
|
2 |
|
$label = $this->color('Description:', 'brown') . "\n"; |
97
|
2 |
|
$this->echo("{$label} {$desc}\n\n"); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
$usage = $this->color('Usage:', 'brown') . "\n php {$script} {$prefix}:{$name}"; |
101
|
|
|
|
102
|
2 |
|
if ($withOptions) { |
103
|
1 |
|
$this->echo("{$usage} [options]\n\n"); |
104
|
1 |
|
$this->echo($this->color('Options:', 'brown') . "\n"); |
105
|
|
|
} else { |
106
|
1 |
|
$this->echo("{$usage}\n"); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
protected function helpOption(string $option, string $description): void |
111
|
|
|
{ |
112
|
1 |
|
$this->echo(' ' . $this->color($option, 'green') . "\n"); |
113
|
1 |
|
$this->echo($this->indent($description, 8, 80) . "\n"); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|