2010-10-04 03:42:24 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CommandListTest file
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc.
|
2010-10-04 03:42:24 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
|
2010-10-04 03:42:24 +00:00
|
|
|
* @link http://cakephp.org CakePHP Project
|
|
|
|
* @since CakePHP v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
|
2011-03-08 18:45:44 +00:00
|
|
|
App::uses('Inflector', 'Utility');
|
|
|
|
|
2010-10-04 03:42:24 +00:00
|
|
|
/**
|
|
|
|
* Shows a list of commands available from the console.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console.Command
|
2010-10-04 03:42:24 +00:00
|
|
|
*/
|
|
|
|
class CommandListShell extends Shell {
|
2010-10-20 03:25:23 +00:00
|
|
|
|
|
|
|
/**
|
2010-10-20 03:25:42 +00:00
|
|
|
* startup
|
2010-10-20 03:25:23 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function startup() {
|
|
|
|
if (empty($this->params['xml'])) {
|
|
|
|
parent::startup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-04 03:42:24 +00:00
|
|
|
/**
|
|
|
|
* Main function Prints out the list of shells.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function main() {
|
2010-10-20 03:25:23 +00:00
|
|
|
if (empty($this->params['xml'])) {
|
2011-04-25 19:17:59 +00:00
|
|
|
$this->out(__d('cake_console', "<info>Current Paths:</info>"), 2);
|
2010-10-24 19:11:30 +00:00
|
|
|
$this->out(" -app: ". APP_DIR);
|
|
|
|
$this->out(" -working: " . rtrim(APP_PATH, DS));
|
|
|
|
$this->out(" -root: " . rtrim(ROOT, DS));
|
2010-10-20 03:25:23 +00:00
|
|
|
$this->out(" -core: " . rtrim(CORE_PATH, DS));
|
|
|
|
$this->out("");
|
2011-04-25 19:17:59 +00:00
|
|
|
$this->out(__d('cake_console', "<info>Changing Paths:</info>"), 2);
|
|
|
|
$this->out(__d('cake_console', "Your working path should be the same as your application path to change your path use the '-app' param."));
|
|
|
|
$this->out(__d('cake_console', "Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp"), 2);
|
2010-10-04 03:42:24 +00:00
|
|
|
|
2011-04-25 19:17:59 +00:00
|
|
|
$this->out(__d('cake_console', "<info>Available Shells:</info>"), 2);
|
2010-10-20 03:25:23 +00:00
|
|
|
}
|
2010-10-20 03:21:24 +00:00
|
|
|
|
|
|
|
$shellList = $this->_getShellList();
|
|
|
|
|
|
|
|
if ($shellList) {
|
|
|
|
ksort($shellList);
|
|
|
|
if (empty($this->params['xml'])) {
|
2010-10-27 02:34:27 +00:00
|
|
|
if (!empty($this->params['sort'])) {
|
|
|
|
$this->_asSorted($shellList);
|
|
|
|
} else {
|
|
|
|
$this->_asText($shellList);
|
|
|
|
}
|
2010-10-20 03:21:24 +00:00
|
|
|
} else {
|
|
|
|
$this->_asXml($shellList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the shell command listing.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _getShellList() {
|
2010-10-04 03:42:24 +00:00
|
|
|
$shellList = array();
|
2010-10-17 21:32:37 +00:00
|
|
|
|
2011-03-08 18:45:44 +00:00
|
|
|
$shells = App::objects('file', App::core('Console/Command'));
|
|
|
|
$shellList = $this->_appendShells('CORE', $shells, $shellList);
|
2010-10-17 21:32:37 +00:00
|
|
|
|
2011-03-08 18:45:44 +00:00
|
|
|
$appShells = App::objects('Console/Command', null, false);
|
|
|
|
$shellList = $this->_appendShells('app', $appShells, $shellList);
|
2010-10-17 21:32:37 +00:00
|
|
|
|
2011-05-12 04:50:29 +00:00
|
|
|
$plugins = CakePlugin::loaded();
|
2010-10-17 21:32:37 +00:00
|
|
|
foreach ($plugins as $plugin) {
|
2011-03-08 18:45:44 +00:00
|
|
|
$pluginShells = App::objects($plugin . '.Console/Command');
|
|
|
|
$shellList = $this->_appendShells($plugin, $pluginShells, $shellList);
|
2010-10-04 03:42:24 +00:00
|
|
|
}
|
2011-03-08 18:45:44 +00:00
|
|
|
|
2010-10-20 03:21:24 +00:00
|
|
|
return $shellList;
|
2010-10-17 21:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan the provided paths for shells, and append them into $shellList
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-03-08 18:45:44 +00:00
|
|
|
protected function _appendShells($type, $shells, $shellList) {
|
|
|
|
foreach ($shells as $shell) {
|
|
|
|
$shell = Inflector::underscore(str_replace('Shell', '', $shell));
|
|
|
|
$shellList[$shell][$type] = $type;
|
2010-10-17 21:32:37 +00:00
|
|
|
}
|
|
|
|
return $shellList;
|
2010-10-04 03:42:24 +00:00
|
|
|
}
|
2010-10-20 03:21:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Output text.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _asText($shellList) {
|
|
|
|
if (DS === '/') {
|
|
|
|
$width = exec('tput cols') - 2;
|
|
|
|
}
|
|
|
|
if (empty($width)) {
|
|
|
|
$width = 80;
|
|
|
|
}
|
|
|
|
$columns = max(1, floor($width / 30));
|
|
|
|
$rows = ceil(count($shellList) / $columns);
|
|
|
|
|
|
|
|
foreach ($shellList as $shell => $types) {
|
|
|
|
sort($types);
|
|
|
|
$shellList[$shell] = str_pad($shell . ' [' . implode ($types, ', ') . ']', $width / $columns);
|
|
|
|
}
|
|
|
|
$out = array_chunk($shellList, $rows);
|
|
|
|
for ($i = 0; $i < $rows; $i++) {
|
|
|
|
$row = '';
|
|
|
|
for ($j = 0; $j < $columns; $j++) {
|
|
|
|
if (!isset($out[$j][$i])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$row .= $out[$j][$i];
|
|
|
|
}
|
|
|
|
$this->out(" " . $row);
|
|
|
|
}
|
|
|
|
$this->out();
|
2011-04-25 19:17:59 +00:00
|
|
|
$this->out(__d('cake_console', "To run a command, type <info>cake shell_name [args]</info>"));
|
|
|
|
$this->out(__d('cake_console', "To get help on a specific command, type <info>cake shell_name --help</info>"), 2);
|
2010-10-20 03:21:24 +00:00
|
|
|
}
|
|
|
|
|
2010-10-27 02:34:27 +00:00
|
|
|
/**
|
|
|
|
* Generates the shell list sorted by where the shells are found.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _asSorted($shellList) {
|
|
|
|
$grouped = array();
|
|
|
|
foreach ($shellList as $shell => $types) {
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$type = Inflector::camelize($type);
|
|
|
|
if (empty($grouped[$type])) {
|
|
|
|
$grouped[$type] = array();
|
|
|
|
}
|
|
|
|
$grouped[$type][] = $shell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($grouped['App'])) {
|
|
|
|
sort($grouped['App'], SORT_STRING);
|
|
|
|
$this->out('[ App ]');
|
|
|
|
$this->out(' ' . implode(', ', $grouped['App']), 2);
|
|
|
|
unset($grouped['App']);
|
|
|
|
}
|
|
|
|
foreach ($grouped as $section => $shells) {
|
|
|
|
if ($section == 'CORE') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sort($shells, SORT_STRING);
|
|
|
|
$this->out('[ ' . $section . ' ]');
|
|
|
|
$this->out(' ' . implode(', ', $shells), 2);
|
|
|
|
}
|
|
|
|
if (!empty($grouped['CORE'])) {
|
|
|
|
sort($grouped['CORE'], SORT_STRING);
|
|
|
|
$this->out('[ Core ]');
|
|
|
|
$this->out(' ' . implode(', ', $grouped['CORE']), 2);
|
|
|
|
}
|
|
|
|
$this->out();
|
|
|
|
}
|
|
|
|
|
2010-10-20 03:21:24 +00:00
|
|
|
/**
|
|
|
|
* Output as XML
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _asXml($shellList) {
|
2011-05-12 04:50:29 +00:00
|
|
|
$plugins = CakePlugin::loaded();
|
2010-10-20 03:21:24 +00:00
|
|
|
$shells = new SimpleXmlElement('<shells></shells>');
|
|
|
|
foreach ($shellList as $name => $location) {
|
|
|
|
$source = current($location);
|
|
|
|
$callable = $name;
|
|
|
|
if (in_array($source, $plugins)) {
|
2011-05-12 04:50:29 +00:00
|
|
|
$callable = Inflector::camelize($source) . '.' . $name;
|
2010-10-20 03:21:24 +00:00
|
|
|
}
|
|
|
|
$shell = $shells->addChild('shell');
|
|
|
|
$shell->addAttribute('name', $name);
|
|
|
|
$shell->addAttribute('call_as', $callable);
|
|
|
|
$shell->addAttribute('provider', $source);
|
|
|
|
$shell->addAttribute('help', $callable . ' -h');
|
|
|
|
}
|
2011-06-21 16:44:36 +00:00
|
|
|
$this->stdout->outputAs(ConsoleOutput::RAW);
|
2010-10-20 03:21:24 +00:00
|
|
|
$this->out($shells->saveXml());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the option parser
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getOptionParser() {
|
|
|
|
$parser = parent::getOptionParser();
|
2011-04-25 19:17:59 +00:00
|
|
|
return $parser->description(__d('cake_console', 'Get the list of available shells for this CakePHP application.'))
|
2010-10-20 03:21:24 +00:00
|
|
|
->addOption('xml', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Get the listing as XML.'),
|
2010-10-20 03:21:24 +00:00
|
|
|
'boolean' => true
|
2010-10-27 02:34:27 +00:00
|
|
|
))->addOption('sort', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Sorts the commands by where they are located.'),
|
2010-10-27 02:34:27 +00:00
|
|
|
'boolean' => true
|
2010-10-20 03:21:24 +00:00
|
|
|
));
|
|
|
|
}
|
2010-10-04 03:42:24 +00:00
|
|
|
}
|