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('Current Paths:', 2);
$io->out('* app: ' . APP_DIR . DIRECTORY_SEPARATOR);
$io->out('* root: ' . ROOT . DIRECTORY_SEPARATOR);
$io->out('* core: ' . CORE_PATH);
$io->out('');
$io->out('Available Commands:', 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 `cake command_name [args|options]`');
$io->out('To get help on a specific command, type `cake command_name --help`', 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('');
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;
}
}