1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Genesis\Commands; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Genesis\Cli; |
8
|
|
|
use Genesis\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Adam Bisek <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Help extends Command |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
private $sections = []; |
18
|
|
|
|
19
|
|
|
|
20
|
4 |
|
public function addSection($name, $description = NULL) |
21
|
|
|
{ |
22
|
4 |
|
$this->sections[$name] = [ |
23
|
4 |
|
'description' => $description, |
24
|
4 |
|
'tasks' => [], |
25
|
|
|
]; |
26
|
4 |
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $name |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
3 |
|
public function hasSection($name) |
34
|
|
|
{ |
35
|
3 |
|
return isset($this->sections[$name]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getSections() |
43
|
|
|
{ |
44
|
|
|
return $this->sections; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $section |
50
|
|
|
* @param array $tasks |
51
|
|
|
*/ |
52
|
4 |
|
public function setSectionTasks($section, array $tasks) |
53
|
|
|
{ |
54
|
4 |
|
if (!isset($this->sections[$section])) { |
55
|
|
|
throw new InvalidArgumentException("Section '$section' not found."); |
56
|
|
|
} |
57
|
4 |
|
$this->sections[$section]['tasks'] = $tasks; |
58
|
4 |
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
4 |
|
public function execute() |
62
|
|
|
{ |
63
|
|
|
// detect max width |
64
|
4 |
|
$minColumnWidth = 30; |
65
|
4 |
|
foreach ($this->sections as $sectionName => $data) { |
66
|
4 |
|
if (strlen($sectionName) > $minColumnWidth) { |
67
|
|
|
$minColumnWidth = strlen($sectionName) + 5; |
68
|
|
|
} |
69
|
4 |
|
foreach ($data['tasks'] as $taskName => $description) { |
70
|
4 |
|
if (strlen($taskName) > $minColumnWidth) { |
71
|
|
|
$minColumnWidth = strlen($taskName) + 2 + 5; |
72
|
|
|
} |
73
|
4 |
|
} |
74
|
4 |
|
} |
75
|
|
|
// empty section first |
76
|
4 |
|
if (isset($this->sections[''])) { |
77
|
3 |
|
$val = $this->sections['']; |
78
|
3 |
|
unset($this->sections['']); |
79
|
3 |
|
$this->sections = ['' => $val] + $this->sections; |
80
|
3 |
|
} |
81
|
|
|
|
82
|
4 |
|
echo Cli::getColoredString(str_repeat('-', 14), 'green') . PHP_EOL; |
83
|
4 |
|
echo Cli::getColoredString('HELP', 'green') . PHP_EOL; |
84
|
4 |
|
echo Cli::getColoredString(str_repeat('-', 14), 'green') . PHP_EOL . PHP_EOL; |
85
|
4 |
|
echo "Available tasks:" . PHP_EOL; |
86
|
4 |
|
foreach ($this->sections as $sectionName => $data) { |
87
|
4 |
|
echo Cli::getColoredString($sectionName, 'yellow'); |
88
|
4 |
|
echo str_repeat(" ", $minColumnWidth - strlen($sectionName) + 2); // +2 = two spaces before taskName (below) |
89
|
4 |
|
echo Cli::getColoredString($data['description'], 'dark_gray'); |
90
|
4 |
|
echo PHP_EOL; |
91
|
4 |
|
foreach ($data['tasks'] as $taskName => $description) { |
92
|
4 |
|
echo " " . Cli::getColoredString($taskName, 'light_blue'); |
93
|
4 |
|
echo str_repeat(" ", $minColumnWidth - strlen($taskName)); |
94
|
4 |
|
echo Cli::getColoredString($description, 'gray'); |
95
|
4 |
|
echo PHP_EOL; |
96
|
4 |
|
} |
97
|
4 |
|
echo PHP_EOL; |
98
|
4 |
|
} |
99
|
4 |
|
} |
100
|
|
|
|
101
|
|
|
} |