You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
422 lines
12 KiB
422 lines
12 KiB
13 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
13 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\console\controllers;
|
||
13 years ago
|
|
||
12 years ago
|
use Yii;
|
||
13 years ago
|
use yii\base\Application;
|
||
|
use yii\base\InlineAction;
|
||
|
use yii\console\Controller;
|
||
12 years ago
|
use yii\console\Exception;
|
||
12 years ago
|
use yii\console\Request;
|
||
12 years ago
|
use yii\helpers\StringHelper;
|
||
13 years ago
|
|
||
13 years ago
|
/**
|
||
13 years ago
|
* This command provides help information about console commands.
|
||
13 years ago
|
*
|
||
13 years ago
|
* This command displays the available command list in
|
||
|
* the application or the detailed instructions about using
|
||
|
* a specific command.
|
||
13 years ago
|
*
|
||
13 years ago
|
* This command can be used as follows on command line:
|
||
13 years ago
|
*
|
||
|
* ~~~
|
||
12 years ago
|
* yii help [command name]
|
||
13 years ago
|
* ~~~
|
||
|
*
|
||
13 years ago
|
* In the above, if the command name is not provided, all
|
||
|
* available commands will be displayed.
|
||
13 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
13 years ago
|
class HelpController extends Controller
|
||
13 years ago
|
{
|
||
13 years ago
|
/**
|
||
|
* Displays available commands or the detailed information
|
||
|
* about a particular command. For example,
|
||
|
*
|
||
|
* ~~~
|
||
12 years ago
|
* yii help # list available commands
|
||
|
* yii help message # display help info about "message"
|
||
13 years ago
|
* ~~~
|
||
|
*
|
||
12 years ago
|
* @param string $command The name of the command to show help about.
|
||
12 years ago
|
* If not provided, all available commands will be displayed.
|
||
13 years ago
|
* @return integer the exit status
|
||
12 years ago
|
* @throws Exception if the command for help is unknown
|
||
13 years ago
|
*/
|
||
12 years ago
|
public function actionIndex($command = null)
|
||
13 years ago
|
{
|
||
12 years ago
|
if ($command !== null) {
|
||
12 years ago
|
$result = Yii::$app->createController($command);
|
||
13 years ago
|
if ($result === false) {
|
||
12 years ago
|
throw new Exception(Yii::t('yii', 'No help for unknown command "{command}".', array(
|
||
12 years ago
|
'{command}' => $command,
|
||
12 years ago
|
)));
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
list($controller, $actionID) = $result;
|
||
13 years ago
|
|
||
12 years ago
|
$actions = $this->getActions($controller);
|
||
|
if ($actionID !== '' || count($actions) === 1 && $actions[0] === $controller->defaultAction) {
|
||
12 years ago
|
$this->getActionHelp($controller, $actionID);
|
||
12 years ago
|
} else {
|
||
|
$this->getControllerHelp($controller);
|
||
13 years ago
|
}
|
||
12 years ago
|
} else {
|
||
|
$this->getHelp();
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns all available command names.
|
||
|
* @return array all available command names
|
||
|
*/
|
||
|
public function getCommands()
|
||
|
{
|
||
12 years ago
|
$commands = $this->getModuleCommands(Yii::$app);
|
||
13 years ago
|
sort($commands);
|
||
|
return array_unique($commands);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns all available actions of the specified controller.
|
||
|
* @param Controller $controller the controller instance
|
||
|
* @return array all available action IDs.
|
||
|
*/
|
||
|
public function getActions($controller)
|
||
|
{
|
||
12 years ago
|
$actions = array_keys($controller->actions());
|
||
13 years ago
|
$class = new \ReflectionClass($controller);
|
||
|
foreach ($class->getMethods() as $method) {
|
||
|
$name = $method->getName();
|
||
12 years ago
|
if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') {
|
||
|
$actions[] = StringHelper::camel2id(substr($name, 6));
|
||
13 years ago
|
}
|
||
|
}
|
||
|
sort($actions);
|
||
|
return array_unique($actions);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns available commands of a specified module.
|
||
|
* @param \yii\base\Module $module the module instance
|
||
|
* @return array the available command names
|
||
|
*/
|
||
|
protected function getModuleCommands($module)
|
||
|
{
|
||
12 years ago
|
$prefix = $module instanceof Application ? '' : $module->getUniqueID() . '/';
|
||
13 years ago
|
|
||
|
$commands = array();
|
||
12 years ago
|
foreach (array_keys($module->controllerMap) as $id) {
|
||
13 years ago
|
$commands[] = $prefix . $id;
|
||
|
}
|
||
|
|
||
|
foreach ($module->getModules() as $id => $child) {
|
||
|
if (($child = $module->getModule($id)) === null) {
|
||
|
continue;
|
||
|
}
|
||
|
foreach ($this->getModuleCommands($child) as $command) {
|
||
|
$commands[] = $prefix . $id . '/' . $command;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$files = scandir($module->getControllerPath());
|
||
|
foreach ($files as $file) {
|
||
12 years ago
|
if (strcmp(substr($file, -14), 'Controller.php') === 0) {
|
||
13 years ago
|
$commands[] = $prefix . lcfirst(substr(basename($file), 0, -14));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $commands;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Displays all available commands.
|
||
|
*/
|
||
12 years ago
|
protected function getHelp()
|
||
13 years ago
|
{
|
||
13 years ago
|
$commands = $this->getCommands();
|
||
12 years ago
|
if (!empty($commands)) {
|
||
12 years ago
|
echo "The following commands are available:\n\n";
|
||
13 years ago
|
foreach ($commands as $command) {
|
||
12 years ago
|
echo "* $command\n";
|
||
13 years ago
|
}
|
||
12 years ago
|
echo "\nTo see the help of each command, enter:\n";
|
||
12 years ago
|
echo "\n yii help <command-name>\n\n";
|
||
13 years ago
|
} else {
|
||
|
echo "\nNo commands are found.\n";
|
||
|
}
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
/**
|
||
|
* Displays the overall information of the command.
|
||
|
* @param Controller $controller the controller instance
|
||
|
*/
|
||
12 years ago
|
protected function getControllerHelp($controller)
|
||
13 years ago
|
{
|
||
13 years ago
|
$class = new \ReflectionClass($controller);
|
||
|
$comment = strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($class->getDocComment(), '/'))), "\r", '');
|
||
|
if (preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) {
|
||
|
$comment = trim(substr($comment, 0, $matches[0][1]));
|
||
|
}
|
||
|
|
||
|
if ($comment !== '') {
|
||
12 years ago
|
echo "\nDESCRIPTION\n";
|
||
|
echo "\n" . $comment . "\n\n";
|
||
13 years ago
|
}
|
||
|
|
||
|
$actions = $this->getActions($controller);
|
||
12 years ago
|
if (!empty($actions)) {
|
||
12 years ago
|
echo "\nSUB-COMMANDS\n\n";
|
||
13 years ago
|
$prefix = $controller->getUniqueId();
|
||
|
foreach ($actions as $action) {
|
||
12 years ago
|
if ($action === $controller->defaultAction) {
|
||
12 years ago
|
echo "* $prefix/$action (default)";
|
||
12 years ago
|
} else {
|
||
12 years ago
|
echo "* $prefix/$action";
|
||
12 years ago
|
}
|
||
12 years ago
|
$summary = $this->getActionSummary($controller, $action);
|
||
|
if ($summary !== '') {
|
||
|
echo ': ' . $summary;
|
||
|
}
|
||
|
echo "\n";
|
||
13 years ago
|
}
|
||
12 years ago
|
echo "\n\nTo see the detailed information about individual sub-commands, enter:\n";
|
||
12 years ago
|
echo "\n yii help <sub-command>\n\n";
|
||
13 years ago
|
}
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
/**
|
||
12 years ago
|
* Returns the short summary of the action.
|
||
|
* @param Controller $controller the controller instance
|
||
|
* @param string $actionID action ID
|
||
|
* @return string the summary about the action
|
||
|
*/
|
||
|
protected function getActionSummary($controller, $actionID)
|
||
|
{
|
||
|
$action = $controller->createAction($actionID);
|
||
|
if ($action === null) {
|
||
|
return '';
|
||
|
}
|
||
|
if ($action instanceof InlineAction) {
|
||
|
$reflection = new \ReflectionMethod($controller, $action->actionMethod);
|
||
|
} else {
|
||
|
$reflection = new \ReflectionClass($action);
|
||
|
}
|
||
|
$tags = $this->parseComment($reflection->getDocComment());
|
||
|
if ($tags['description'] !== '') {
|
||
|
$limit = 73 - strlen($action->getUniqueId());
|
||
|
if ($actionID === $controller->defaultAction) {
|
||
|
$limit -= 10;
|
||
|
}
|
||
|
if ($limit < 0) {
|
||
|
$limit = 50;
|
||
|
}
|
||
|
$description = $tags['description'];
|
||
|
if (($pos = strpos($tags['description'], "\n")) !== false) {
|
||
|
$description = substr($description, 0, $pos);
|
||
|
}
|
||
|
$text = substr($description, 0, $limit);
|
||
|
return strlen($description) > $limit ? $text . '...' : $text;
|
||
|
} else {
|
||
|
return '';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Displays the detailed information of a command action.
|
||
|
* @param Controller $controller the controller instance
|
||
|
* @param string $actionID action ID
|
||
12 years ago
|
* @throws Exception if the action does not exist
|
||
13 years ago
|
*/
|
||
12 years ago
|
protected function getActionHelp($controller, $actionID)
|
||
13 years ago
|
{
|
||
13 years ago
|
$action = $controller->createAction($actionID);
|
||
|
if ($action === null) {
|
||
12 years ago
|
throw new Exception(Yii::t('yii', 'No help for unknown sub-command "{command}".', array(
|
||
12 years ago
|
'{command}' => rtrim($controller->getUniqueId() . '/' . $actionID, '/'),
|
||
12 years ago
|
)));
|
||
13 years ago
|
}
|
||
|
if ($action instanceof InlineAction) {
|
||
12 years ago
|
$method = new \ReflectionMethod($controller, $action->actionMethod);
|
||
13 years ago
|
} else {
|
||
|
$method = new \ReflectionMethod($action, 'run');
|
||
|
}
|
||
12 years ago
|
|
||
|
$tags = $this->parseComment($method->getDocComment());
|
||
12 years ago
|
$options = $this->getOptionHelps($controller);
|
||
12 years ago
|
|
||
|
if ($tags['description'] !== '') {
|
||
|
echo "\nDESCRIPTION";
|
||
|
echo "\n\n" . $tags['description'] . "\n\n";
|
||
|
}
|
||
12 years ago
|
|
||
|
echo "\nUSAGE\n\n";
|
||
|
if ($action->id === $controller->defaultAction) {
|
||
12 years ago
|
echo 'yii ' . $controller->getUniqueId();
|
||
13 years ago
|
} else {
|
||
12 years ago
|
echo "yii " . $action->getUniqueId();
|
||
12 years ago
|
}
|
||
12 years ago
|
list ($required, $optional) = $this->getArgHelps($method, isset($tags['param']) ? $tags['param'] : array());
|
||
|
if (!empty($required)) {
|
||
|
echo ' <' . implode('> <', array_keys($required)) . '>';
|
||
13 years ago
|
}
|
||
12 years ago
|
if (!empty($optional)) {
|
||
|
echo ' [' . implode('] [', array_keys($optional)) . ']';
|
||
12 years ago
|
}
|
||
12 years ago
|
if (!empty($options)) {
|
||
|
echo ' [...options...]';
|
||
|
}
|
||
12 years ago
|
echo "\n\n";
|
||
12 years ago
|
|
||
12 years ago
|
if (!empty($required) || !empty($optional)) {
|
||
|
echo implode("\n\n", array_merge($required, $optional)) . "\n\n";
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
12 years ago
|
$options = $this->getOptionHelps($controller);
|
||
12 years ago
|
if (!empty($options)) {
|
||
12 years ago
|
echo "\nOPTIONS\n\n";
|
||
|
echo implode("\n\n", $options) . "\n\n";
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
/**
|
||
12 years ago
|
* Returns the help information about arguments.
|
||
13 years ago
|
* @param \ReflectionMethod $method
|
||
12 years ago
|
* @param string $tags the parsed comment block related with arguments
|
||
|
* @return array the required and optional argument help information
|
||
13 years ago
|
*/
|
||
12 years ago
|
protected function getArgHelps($method, $tags)
|
||
13 years ago
|
{
|
||
12 years ago
|
if (is_string($tags)) {
|
||
|
$tags = array($tags);
|
||
|
}
|
||
13 years ago
|
$params = $method->getParameters();
|
||
12 years ago
|
$optional = $required = array();
|
||
|
foreach ($params as $i => $param) {
|
||
|
$name = $param->getName();
|
||
|
$tag = isset($tags[$i]) ? $tags[$i] : '';
|
||
|
if (preg_match('/^([^\s]+)\s+(\$\w+\s+)?(.*)/s', $tag, $matches)) {
|
||
|
$type = $matches[1];
|
||
|
$comment = $matches[3];
|
||
|
} else {
|
||
|
$type = null;
|
||
|
$comment = $tag;
|
||
|
}
|
||
|
if ($param->isDefaultValueAvailable()) {
|
||
12 years ago
|
$optional[$name] = $this->formatOptionHelp('* ' . $name, false, $type, $param->getDefaultValue(), $comment);
|
||
12 years ago
|
} else {
|
||
12 years ago
|
$required[$name] = $this->formatOptionHelp('* ' . $name, true, $type, null, $comment);
|
||
13 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
return array($required, $optional);
|
||
13 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Returns the help information about the options available for a console controller.
|
||
|
* @param Controller $controller the console controller
|
||
|
* @return array the help information about the options
|
||
13 years ago
|
*/
|
||
12 years ago
|
protected function getOptionHelps($controller)
|
||
13 years ago
|
{
|
||
12 years ago
|
$optionNames = $controller->globalOptions();
|
||
|
if (empty($optionNames)) {
|
||
|
return array();
|
||
|
}
|
||
|
|
||
|
$class = new \ReflectionClass($controller);
|
||
13 years ago
|
$options = array();
|
||
|
foreach ($class->getProperties() as $property) {
|
||
|
$name = $property->getName();
|
||
12 years ago
|
if (!in_array($name, $optionNames, true)) {
|
||
|
continue;
|
||
13 years ago
|
}
|
||
12 years ago
|
$defaultValue = $property->getValue($controller);
|
||
|
$tags = $this->parseComment($property->getDocComment());
|
||
|
if (isset($tags['var']) || isset($tags['property'])) {
|
||
|
$doc = isset($tags['var']) ? $tags['var'] : $tags['property'];
|
||
|
if (is_array($doc)) {
|
||
|
$doc = reset($doc);
|
||
13 years ago
|
}
|
||
12 years ago
|
if (preg_match('/^([^\s]+)(.*)/s', $doc, $matches)) {
|
||
|
$type = $matches[1];
|
||
|
$comment = $matches[2];
|
||
|
} else {
|
||
|
$type = null;
|
||
|
$comment = $doc;
|
||
|
}
|
||
12 years ago
|
$options[$name] = $this->formatOptionHelp('--' . $name, false, $type, $defaultValue, $comment);
|
||
12 years ago
|
} else {
|
||
12 years ago
|
$options[$name] = $this->formatOptionHelp('--' . $name, false, null, $defaultValue, '');
|
||
13 years ago
|
}
|
||
|
}
|
||
|
ksort($options);
|
||
|
return $options;
|
||
13 years ago
|
}
|
||
12 years ago
|
|
||
|
/**
|
||
|
* Parses the comment block into tags.
|
||
|
* @param string $comment the comment block
|
||
|
* @return array the parsed tags
|
||
|
*/
|
||
|
protected function parseComment($comment)
|
||
|
{
|
||
|
$tags = array();
|
||
|
$comment = "@description \n" . strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($comment, '/'))), "\r", '');
|
||
|
$parts = preg_split('/^\s*@/m', $comment, -1, PREG_SPLIT_NO_EMPTY);
|
||
|
foreach ($parts as $part) {
|
||
|
if (preg_match('/^(\w+)(.*)/ms', trim($part), $matches)) {
|
||
|
$name = $matches[1];
|
||
|
if (!isset($tags[$name])) {
|
||
|
$tags[$name] = trim($matches[2]);
|
||
|
} elseif (is_array($tags[$name])) {
|
||
|
$tags[$name][] = trim($matches[2]);
|
||
|
} else {
|
||
|
$tags[$name] = array($tags[$name], trim($matches[2]));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return $tags;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Generates a well-formed string for an argument or option.
|
||
|
* @param string $name the name of the argument or option
|
||
|
* @param boolean $required whether the argument is required
|
||
|
* @param string $type the type of the option or argument
|
||
|
* @param mixed $defaultValue the default value of the option or argument
|
||
|
* @param string $comment comment about the option or argument
|
||
|
* @return string the formatted string for the argument or option
|
||
|
*/
|
||
|
protected function formatOptionHelp($name, $required, $type, $defaultValue, $comment)
|
||
|
{
|
||
|
$doc = '';
|
||
|
$comment = trim($comment);
|
||
|
|
||
|
if ($defaultValue !== null && !is_array($defaultValue)) {
|
||
|
if ($type === null) {
|
||
|
$type = gettype($defaultValue);
|
||
|
}
|
||
|
$doc = "$type (defaults to " . var_export($defaultValue, true) . ")";
|
||
|
} elseif (trim($type) !== '') {
|
||
|
$doc = $type;
|
||
|
}
|
||
|
|
||
|
if ($doc === '') {
|
||
|
$doc = $comment;
|
||
|
} elseif ($comment !== '') {
|
||
|
$doc .= "\n" . preg_replace("/^/m", " ", $comment);
|
||
|
}
|
||
|
|
||
|
$name = $required ? "$name (required)" : $name;
|
||
|
return $doc === '' ? $name : "$name: $doc";
|
||
|
}
|
||
12 years ago
|
}
|