2013-09-14 00:36:57 +02:00
|
|
|
<?php
|
2013-09-16 14:11:27 +02:00
|
|
|
/**
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP Project
|
|
|
|
* @package Cake.Console.Command
|
|
|
|
* @since CakePHP v 2.5
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
|
*/
|
|
|
|
|
2013-09-14 00:36:57 +02:00
|
|
|
App::uses('CommandListShell', 'Console/Command');
|
|
|
|
|
|
|
|
/**
|
2013-09-16 14:11:27 +02:00
|
|
|
* Provide command completion shells such as bash.
|
|
|
|
*
|
|
|
|
* @package Cake.Console.Command
|
2013-09-14 00:36:57 +02:00
|
|
|
*/
|
|
|
|
class CompletionShell extends CommandListShell {
|
|
|
|
|
|
|
|
/**
|
2013-09-16 14:11:27 +02:00
|
|
|
* Echo no header by overriding the startup method
|
2013-09-14 00:36:57 +02:00
|
|
|
*
|
2013-09-16 14:11:27 +02:00
|
|
|
* @return void
|
2013-09-14 00:36:57 +02:00
|
|
|
*/
|
|
|
|
public function startup() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-16 14:11:27 +02:00
|
|
|
* Not called by the autocomplete shell - this is for curious users
|
|
|
|
*
|
|
|
|
* @return void
|
2013-09-14 00:36:57 +02:00
|
|
|
*/
|
|
|
|
public function main() {
|
2013-09-16 14:11:27 +02:00
|
|
|
return $this->out($this->OptionParser->help());
|
2013-09-14 00:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* list commands
|
2013-09-16 14:11:27 +02:00
|
|
|
*
|
|
|
|
* @return void
|
2013-09-14 00:36:57 +02:00
|
|
|
*/
|
|
|
|
public function commands() {
|
|
|
|
$options = $this->_commands();
|
|
|
|
return $this->_output($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* list options for the named command
|
2013-09-16 14:11:27 +02:00
|
|
|
*
|
|
|
|
* @return void
|
2013-09-14 00:36:57 +02:00
|
|
|
*/
|
|
|
|
public function options() {
|
|
|
|
if (!$this->args) {
|
|
|
|
$parser = new ConsoleOptionParser();
|
|
|
|
} else {
|
|
|
|
$Shell = $this->_getShell($this->args[0]);
|
|
|
|
if (!$Shell) {
|
|
|
|
$parser = new ConsoleOptionParser();
|
|
|
|
} else {
|
|
|
|
$parser = $Shell->getOptionParser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = array();
|
|
|
|
$array = $parser->options();
|
|
|
|
foreach ($array as $name => $obj) {
|
|
|
|
$options[] = "--$name";
|
|
|
|
$short = $obj->short();
|
|
|
|
if ($short) {
|
|
|
|
$options[] = "-$short";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->_output($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* list subcommands for the named command
|
2013-09-16 14:11:27 +02:00
|
|
|
*
|
|
|
|
* @return void
|
2013-09-14 00:36:57 +02:00
|
|
|
*/
|
|
|
|
public function subCommands() {
|
|
|
|
if (!$this->args) {
|
|
|
|
return $this->_output();
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = $this->_subCommands($this->args[0]);
|
|
|
|
return $this->_output($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Guess autocomplete from the whole argument string
|
2013-09-16 14:11:27 +02:00
|
|
|
*
|
|
|
|
* @return void
|
2013-09-14 00:36:57 +02:00
|
|
|
*/
|
|
|
|
public function fuzzy() {
|
|
|
|
return $this->_output();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getOptionParser for _this_ shell
|
2013-09-16 14:11:27 +02:00
|
|
|
*
|
|
|
|
* @return ConsoleOptionParser
|
2013-09-14 00:36:57 +02:00
|
|
|
*/
|
|
|
|
public function getOptionParser() {
|
2013-09-23 14:44:21 +02:00
|
|
|
$parser = parent::getOptionParser();
|
2013-09-14 00:36:57 +02:00
|
|
|
|
2013-09-23 14:44:21 +02:00
|
|
|
$parser->description(__d('cake_console', 'Used by shells like bash to autocomplete command name, options and arguments'))
|
2013-09-14 00:36:57 +02:00
|
|
|
->addSubcommand('commands', array(
|
2013-09-23 14:44:21 +02:00
|
|
|
'help' => __d('cake_console', 'Output a list of available commands'),
|
2013-09-14 00:36:57 +02:00
|
|
|
'parser' => array(
|
2013-09-23 14:44:21 +02:00
|
|
|
'description' => __d('cake_console', 'List all availables'),
|
2013-09-14 00:36:57 +02:00
|
|
|
'arguments' => array(
|
|
|
|
)
|
|
|
|
)
|
|
|
|
))->addSubcommand('subcommands', array(
|
2013-09-23 14:44:21 +02:00
|
|
|
'help' => __d('cake_console', 'Output a list of available subcommands'),
|
2013-09-14 00:36:57 +02:00
|
|
|
'parser' => array(
|
2013-09-23 14:44:21 +02:00
|
|
|
'description' => __d('cake_console', 'List subcommands for a command'),
|
2013-09-14 00:36:57 +02:00
|
|
|
'arguments' => array(
|
|
|
|
'command' => array(
|
2013-09-23 14:44:21 +02:00
|
|
|
'help' => __d('cake_console', 'The command name'),
|
2013-09-14 00:36:57 +02:00
|
|
|
'required' => true,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
))->addSubcommand('options', array(
|
2013-09-23 14:44:21 +02:00
|
|
|
'help' => __d('cake_console', 'Output a list of available options'),
|
2013-09-14 00:36:57 +02:00
|
|
|
'parser' => array(
|
2013-09-23 14:44:21 +02:00
|
|
|
'description' => __d('cake_console', 'List options'),
|
2013-09-14 00:36:57 +02:00
|
|
|
'arguments' => array(
|
|
|
|
'command' => array(
|
2013-09-23 14:44:21 +02:00
|
|
|
'help' => __d('cake_console', 'The command name'),
|
2013-09-14 00:36:57 +02:00
|
|
|
'required' => false,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
))->epilog(
|
|
|
|
array(
|
2013-09-23 14:44:21 +02:00
|
|
|
__d('cake_console', 'This command is not intended to be called manually'),
|
2013-09-14 00:36:57 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
return $parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of all commands
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _commands() {
|
|
|
|
$shellList = $this->_getShellList();
|
|
|
|
unset($shellList['Completion']);
|
|
|
|
|
|
|
|
$options = array();
|
|
|
|
foreach ($shellList as $type => $commands) {
|
|
|
|
$prefix = '';
|
2013-09-23 14:44:21 +02:00
|
|
|
if (!in_array(strtolower($type), array('app', 'core'))) {
|
2013-09-14 00:36:57 +02:00
|
|
|
$prefix = $type . '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($commands as $shell) {
|
|
|
|
$options[] = $prefix . $shell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of subcommands for a given command
|
|
|
|
*
|
|
|
|
* @param string $commandName
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _subCommands($commandName) {
|
|
|
|
$Shell = $this->_getShell($commandName);
|
|
|
|
|
|
|
|
if (!$Shell) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$taskMap = TaskCollection::normalizeObjectArray((array)$Shell->tasks);
|
2013-09-23 14:44:21 +02:00
|
|
|
$return = array_keys($taskMap);
|
2013-09-14 00:36:57 +02:00
|
|
|
$return = array_map('Inflector::underscore', $return);
|
|
|
|
|
|
|
|
$ShellReflection = new ReflectionClass('AppShell');
|
|
|
|
$shellMethods = $ShellReflection->getMethods(ReflectionMethod::IS_PUBLIC);
|
|
|
|
$shellMethodNames = array('main', 'help');
|
|
|
|
foreach ($shellMethods as $method) {
|
|
|
|
$shellMethodNames[] = $method->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
$Reflection = new ReflectionClass($Shell);
|
|
|
|
$methods = $Reflection->getMethods(ReflectionMethod::IS_PUBLIC);
|
|
|
|
$methodNames = array();
|
|
|
|
foreach ($methods as $method) {
|
|
|
|
$methodNames[] = $method->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
$return += array_diff($methodNames, $shellMethodNames);
|
|
|
|
sort($return);
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-16 14:11:27 +02:00
|
|
|
* Get Shell instance for the given command
|
2013-09-14 00:36:57 +02:00
|
|
|
*
|
|
|
|
* @param mixed $commandName
|
2013-09-16 14:11:27 +02:00
|
|
|
* @return mixed
|
2013-09-14 00:36:57 +02:00
|
|
|
*/
|
|
|
|
protected function _getShell($commandName) {
|
2013-09-23 14:44:21 +02:00
|
|
|
list($pluginDot, $name) = pluginSplit($commandName, true);
|
2013-09-14 00:36:57 +02:00
|
|
|
|
2013-09-23 14:44:21 +02:00
|
|
|
if (in_array(strtolower($pluginDot), array('app.', 'core.'))) {
|
2013-09-16 14:11:27 +02:00
|
|
|
$commandName = $name;
|
2013-09-23 14:44:21 +02:00
|
|
|
$pluginDot = '';
|
2013-09-16 14:11:27 +02:00
|
|
|
}
|
|
|
|
|
2013-09-14 00:36:57 +02:00
|
|
|
if (!in_array($commandName, $this->_commands())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = Inflector::camelize($name);
|
2013-09-23 14:44:21 +02:00
|
|
|
$pluginDot = Inflector::camelize($pluginDot);
|
2013-09-14 00:36:57 +02:00
|
|
|
$class = $name . 'Shell';
|
2013-09-23 14:44:21 +02:00
|
|
|
APP::uses($class, $pluginDot . 'Console/Command');
|
2013-09-14 00:36:57 +02:00
|
|
|
|
|
|
|
$Shell = new $class();
|
2013-09-23 14:44:21 +02:00
|
|
|
$Shell->plugin = trim($pluginDot, '.');
|
2013-09-14 00:36:57 +02:00
|
|
|
$Shell->initialize();
|
|
|
|
$Shell->loadTasks();
|
|
|
|
|
|
|
|
return $Shell;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emit results as a string, space delimited
|
|
|
|
*
|
|
|
|
* @param array $options
|
2013-09-16 14:11:27 +02:00
|
|
|
* @return void
|
2013-09-14 00:36:57 +02:00
|
|
|
*/
|
|
|
|
protected function _output($options = array()) {
|
|
|
|
if ($options) {
|
2013-09-16 14:11:27 +02:00
|
|
|
return $this->out(implode($options, ' '));
|
2013-09-14 00:36:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|