forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListCommands.php
More file actions
139 lines (112 loc) · 3.17 KB
/
Copy pathListCommands.php
File metadata and controls
139 lines (112 loc) · 3.17 KB
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
<?php namespace CodeIgniter\Commands;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
/**
* CI Help command for the ci.php script.
*
* Lists the basic usage information for the ci.php script,
* and provides a way to list help for other commands.
*
* @package CodeIgniter\Commands
*/
class ListCommands extends BaseCommand
{
protected $group = 'CodeIgniter';
/**
* The Command's name
*
* @var string
*/
protected $name = 'list';
/**
* the Command's short description
*
* @var string
*/
protected $description = 'Lists the available commands.';
/**
* The length of the longest command name.
* Used during display in columns.
*
* @var int
*/
protected $maxFirstLength = 0;
//--------------------------------------------------------------------
/**
* Displays the help for the ci.php cli script itself.
*
* @param array $params
*/
public function run(array $params)
{
$commands = $this->commands->getCommands();
$this->describeCommands($commands);
CLI::newLine();
}
//--------------------------------------------------------------------
/**
* Displays the commands on the CLI.
*
* @param array $commands
*/
protected function describeCommands(array $commands = [])
{
arsort($commands);
$names = array_keys($commands);
$descs = array_column($commands, 'description');
$groups = array_column($commands, 'group');
$lastGroup = '';
// Pad each item to the same length
$names = $this->padArray($names, 2, 2);
for ($i = 0; $i < count($names); $i++)
{
$lastGroup = $this->describeGroup($groups[$i], $lastGroup);
$out = CLI::color($names[$i], 'yellow');
if (isset($descs[$i]))
{
$out .= CLI::wrap($descs[$i], 125, strlen($names[$i]));
}
CLI::write($out);
}
}
//--------------------------------------------------------------------
/**
* Outputs the description, if necessary.
*
* @param string $new
* @param string $old
*
* @return string
*/
protected function describeGroup(string $new, string $old)
{
if ($new == $old)
{
return $old;
}
CLI::newLine();
CLI::write($new);
return $new;
}
//--------------------------------------------------------------------
/**
* Returns a new array where all of the string elements have
* been padding with trailing spaces to be the same length.
*
* @param array $array
* @param int $extra // How many extra spaces to add at the end
*
* @return array
*/
protected function padArray($array, $extra = 2, $indent=0)
{
$max = max(array_map('strlen', $array))+$extra+$indent;
foreach ($array as &$item)
{
$item = str_repeat(' ', $indent).$item;
$item = str_pad($item, $max);
}
return $array;
}
//--------------------------------------------------------------------
}