cakephp2-php8/lib/Cake/Console/Command/CommandListShell.php

175 lines
4.9 KiB
PHP
Raw Normal View History

<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
2012-03-13 02:46:46 +00:00
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
2012-03-13 02:46:46 +00:00
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP Project
* @package Cake.Console.Command
* @since CakePHP v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('AppShell', 'Console/Command');
App::uses('Inflector', 'Utility');
/**
* Shows a list of commands available from the console.
*
* @package Cake.Console.Command
*/
class CommandListShell extends AppShell {
/**
2010-10-20 03:25:42 +00:00
* startup
*
* @return void
*/
public function startup() {
if (empty($this->params['xml'])) {
parent::startup();
}
}
/**
* Main function Prints out the list of shells.
*
* @return void
*/
public function main() {
if (empty($this->params['xml'])) {
$this->out(__d('cake_console', "<info>Current Paths:</info>"), 2);
$this->out(" -app: " . APP_DIR);
$this->out(" -working: " . rtrim(APP, DS));
$this->out(" -root: " . rtrim(ROOT, DS));
$this->out(" -core: " . rtrim(CORE_PATH, DS));
$this->out("");
$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);
$this->out(__d('cake_console', "<info>Available Shells:</info>"), 2);
}
$shellList = $this->_getShellList();
if (empty($shellList)) {
return;
}
if (empty($this->params['xml'])) {
$this->_asText($shellList);
} else {
$this->_asXml($shellList);
}
}
/**
* Gets the shell command listing.
*
2011-08-16 03:55:08 +00:00
* @return array
*/
protected function _getShellList() {
$skipFiles = array('AppShell');
$plugins = CakePlugin::loaded();
$shellList = array_fill_keys($plugins, null) + array('CORE' => null, 'app' => null);
$corePath = App::core('Console/Command');
$shells = App::objects('file', $corePath[0]);
$shells = array_diff($shells, $skipFiles);
$this->_appendShells('CORE', $shells, $shellList);
$appShells = App::objects('Console/Command', null, false);
$appShells = array_diff($appShells, $shells, $skipFiles);
$this->_appendShells('app', $appShells, $shellList);
foreach ($plugins as $plugin) {
$pluginShells = App::objects($plugin . '.Console/Command');
$this->_appendShells($plugin, $pluginShells, $shellList);
}
return array_filter($shellList);
}
/**
* Scan the provided paths for shells, and append them into $shellList
*
2011-07-29 02:03:44 +00:00
* @param string $type
* @param array $shells
* @param array $shellList
2012-11-28 22:30:47 +00:00
* @return void
*/
protected function _appendShells($type, $shells, &$shellList) {
foreach ($shells as $shell) {
$shellList[$type][] = Inflector::underscore(str_replace('Shell', '', $shell));
}
}
/**
* Output text.
*
2011-07-29 02:03:44 +00:00
* @param array $shellList
* @return void
*/
protected function _asText($shellList) {
foreach ($shellList as $plugin => $commands) {
sort($commands);
$this->out(sprintf('[<info>%s</info>] %s', $plugin, implode(', ', $commands)));
$this->out();
}
$this->out(__d('cake_console', "To run an app or core command, type <info>cake shell_name [args]</info>"));
$this->out(__d('cake_console', "To run a plugin command, type <info>cake Plugin.shell_name [args]</info>"));
$this->out(__d('cake_console', "To get help on a specific command, type <info>cake shell_name --help</info>"), 2);
}
/**
* Output as XML
*
2011-07-29 02:03:44 +00:00
* @param array $shellList
* @return void
*/
protected function _asXml($shellList) {
$plugins = CakePlugin::loaded();
$shells = new SimpleXmlElement('<shells></shells>');
foreach ($shellList as $plugin => $commands) {
foreach ($commands as $command) {
$callable = $command;
if (in_array($plugin, $plugins)) {
$callable = Inflector::camelize($plugin) . '.' . $command;
}
$shell = $shells->addChild('shell');
$shell->addAttribute('name', $command);
$shell->addAttribute('call_as', $callable);
$shell->addAttribute('provider', $plugin);
$shell->addAttribute('help', $callable . ' -h');
}
}
$this->stdout->outputAs(ConsoleOutput::RAW);
$this->out($shells->saveXml());
}
/**
* get the option parser
*
* @return void
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
return $parser->description(__d('cake_console', 'Get the list of available shells for this CakePHP application.'))
->addOption('sort', array(
'help' => __d('cake_console', 'Does nothing (deprecated)'),
'boolean' => true
))
->addOption('xml', array(
'help' => __d('cake_console', 'Get the listing as XML.'),
'boolean' => true
));
}
}