Repository navigation
Expand file tree
/
Copy pathHelpCommand.php
More file actions
164 lines (147 loc) · 4.93 KB
/
Copy pathHelpCommand.php
File metadata and controls
164 lines (147 loc) · 4.93 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
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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.6.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Command;
use Cake\Console\Arguments;
use Cake\Console\Command;
use Cake\Console\CommandCollection;
use Cake\Console\CommandCollectionAwareInterface;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Console\ConsoleOutput;
use SimpleXMLElement;
/**
* Print out command list
*/
class HelpCommand extends Command implements CommandCollectionAwareInterface
{
/**
* The command collection to get help on.
*
* @var \Cake\Console\CommandCollection
*/
protected $commands;
/**
* {@inheritDoc}
*/
public function setCommandCollection(CommandCollection $commands)
{
$this->commands = $commands;
}
/**
* Main function Prints out the list of commands.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int
*/
public function execute(Arguments $args, ConsoleIo $io)
{
if (!$args->getOption('xml')) {
$io->out('<info>Current Paths:</info>', 2);
$io->out('* app: ' . APP_DIR . DIRECTORY_SEPARATOR);
$io->out('* root: ' . ROOT . DIRECTORY_SEPARATOR);
$io->out('* core: ' . CORE_PATH);
$io->out('');
$io->out('<info>Available Commands:</info>', 2);
}
$commands = $this->commands->getIterator();
$commands->ksort();
if ($args->getOption('xml')) {
$this->asXml($io, $commands);
return static::CODE_SUCCESS;
}
$this->asText($io, $commands);
return static::CODE_SUCCESS;
}
/**
* Output text.
*
* @param \Cake\Console\ConsoleIo $io The console io
* @param \ArrayIterator $commands The command collection to output.
* @return void
*/
protected function asText($io, $commands)
{
$invert = [];
foreach ($commands as $name => $class) {
if (is_object($class)) {
$class = get_class($class);
}
if (!isset($invert[$class])) {
$invert[$class] = [];
}
$invert[$class][] = $name;
}
foreach ($commands as $name => $class) {
if (is_object($class)) {
$class = get_class($class);
}
if (count($invert[$class]) == 1) {
$io->out('- ' . $name);
}
if (count($invert[$class]) > 1) {
// Sort by length so we can get the shortest name.
usort($invert[$class], function ($a, $b) {
return strlen($a) - strlen($b);
});
$io->out('- ' . array_shift($invert[$class]));
// Empty the list to prevent duplicates
$invert[$class] = [];
}
}
$io->out('');
$io->out('To run a command, type <info>`cake command_name [args|options]`</info>');
$io->out('To get help on a specific command, type <info>`cake command_name --help`</info>', 2);
}
/**
* Output as XML
*
* @param \Cake\Console\ConsoleIo $io The console io
* @param \ArrayIterator $commands The command collection to output
* @return void
*/
protected function asXml($io, $commands)
{
$shells = new SimpleXMLElement('<shells></shells>');
foreach ($commands as $name => $class) {
if (is_object($class)) {
$class = get_class($class);
}
$shell = $shells->addChild('shell');
$shell->addAttribute('name', $name);
$shell->addAttribute('call_as', $name);
$shell->addAttribute('provider', $class);
$shell->addAttribute('help', $name . ' -h');
}
$io->setOutputAs(ConsoleOutput::RAW);
$io->out($shells->saveXML());
}
/**
* Gets the option parser instance and configures it.
*
* @param \Cake\Console\ConsoleOptionParser $parser The parser to build
* @return \Cake\Console\ConsoleOptionParser
*/
protected function buildOptionParser(ConsoleOptionParser $parser)
{
$parser->setDescription(
'Get the list of available commands for this application.'
)->addOption('xml', [
'help' => 'Get the listing as XML.',
'boolean' => true
]);
return $parser;
}
}