From fadfdf690ac6033cc8b1c7036d30fb620705f906 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 4 Sep 2014 19:47:52 -0400 Subject: [PATCH] Refactored help system. [skip ci] --- framework/console/Action.php | 48 ------------------- framework/console/Controller.php | 52 ++++++++------------ framework/console/controllers/HelpController.php | 60 +++++++----------------- framework/helpers/BaseConsole.php | 6 +-- 4 files changed, 40 insertions(+), 126 deletions(-) delete mode 100644 framework/console/Action.php diff --git a/framework/console/Action.php b/framework/console/Action.php deleted file mode 100644 index 7349841..0000000 --- a/framework/console/Action.php +++ /dev/null @@ -1,48 +0,0 @@ - - * @since 2.0 - */ -class Action extends \yii\base\Action -{ - /** - * Returns one-line short summary describing this action. - * - * You may override this method to return customized summary. - * The default implementation returns first line from the PHPDoc comment. - * - * @return string - */ - public function getHelpSummary() - { - return HelpParser::getSummary(new \ReflectionClass($this)); - } - - /** - * Returns help information for this action. - * - * You may override this method to return customized help. - * The default implementation returns help information retrieved from the PHPDoc comment. - * @return string - */ - public function getHelp() - { - return HelpParser::getDetail(new \ReflectionClass($this)); - } -} diff --git a/framework/console/Controller.php b/framework/console/Controller.php index 5e0aaaa..2c97537 100644 --- a/framework/console/Controller.php +++ b/framework/console/Controller.php @@ -289,27 +289,6 @@ class Controller extends \yii\base\Controller } /** - * Returns one-line short summary describing this controller action. - * - * You may override this method to return customized summary. - * The default implementation returns first line from the PHPDoc comment. - * - * @param string $actionID action to get summary for - * @return string - */ - public function getActionHelpSummary($actionID) - { - $action = $this->createAction($actionID); - if ($action instanceof InlineAction) { - $class = new \ReflectionMethod($this, $action->actionMethod); - } else { - $class = new \ReflectionClass($action); - } - - return HelpParser::getSummary($class); - } - - /** * Returns help information for this controller. * * You may override this method to return customized help. @@ -322,23 +301,30 @@ class Controller extends \yii\base\Controller } /** - * Returns help information for this controller action. - * - * You may override this method to return customized help. - * The default implementation returns help information retrieved from the PHPDoc comment. - * @param string $actionID action to get help for - * @return string + * Returns a one-line short summary describing the specified action. + * @param \yii\base\Action $action action to get summary for + * @return string a one-line short summary describing the specified action. */ - public function getActionHelp($actionID) + public function getActionHelpSummary($action) { - $action = $this->createAction($actionID); - if ($action instanceof InlineAction) { - $class = new \ReflectionMethod($this, $action->actionMethod); + return HelpParser::getSummary(new \ReflectionMethod($this, $action->actionMethod)); } else { - $class = new \ReflectionClass($action); + return HelpParser::getSummary(new \ReflectionMethod($action, 'run')); } + } - return HelpParser::getDetail($class); + /** + * Returns the detailed help information for the specified action. + * @param \yii\base\Action $action action to get help for + * @return string the detailed help information for the specified action. + */ + public function getActionHelp($action) + { + if ($action instanceof InlineAction) { + return HelpParser::getDetail(new \ReflectionMethod($this, $action->actionMethod)); + } else { + return HelpParser::getDetail(new \ReflectionMethod($action, 'run')); + } } } diff --git a/framework/console/controllers/HelpController.php b/framework/console/controllers/HelpController.php index 3fc7606..a55e8ce 100644 --- a/framework/console/controllers/HelpController.php +++ b/framework/console/controllers/HelpController.php @@ -62,12 +62,12 @@ class HelpController extends Controller $actions = $this->getActions($controller); if ($actionID !== '' || count($actions) === 1 && $actions[0] === $controller->defaultAction) { - $this->getControllerActionHelp($controller, $actionID); + $this->getSubCommandHelp($controller, $actionID); } else { - $this->getControllerHelp($controller); + $this->getCommandHelp($controller); } } else { - $this->getGlobalHelp(); + $this->getDefaultHelp(); } } @@ -182,7 +182,7 @@ class HelpController extends Controller /** * Displays all available commands. */ - protected function getGlobalHelp() + protected function getDefaultHelp() { $commands = $this->getCommandDescriptions(); if (!empty($commands)) { @@ -211,7 +211,7 @@ class HelpController extends Controller * Displays the overall information of the command. * @param Controller $controller the controller instance */ - protected function getControllerHelp($controller) + protected function getCommandHelp($controller) { $controller->color = $this->color; @@ -230,7 +230,7 @@ class HelpController extends Controller if ($action === $controller->defaultAction) { $this->stdout(' (default)', Console::FG_GREEN); } - $summary = $this->getActionSummary($controller, $action); + $summary = $controller->getActionHelpSummary($controller->createAction($action)); if ($summary !== '') { echo ': ' . $summary; } @@ -244,32 +244,12 @@ class HelpController extends Controller } /** - * 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) - { - $description = null; - $action = $controller->createAction($actionID); - - if ($action instanceof Action) { - $description = $action->getHelpSummary(); - } - if ($description === null) { - $description = $controller->getActionHelpSummary($actionID); - } - return $description; - } - - /** * Displays the detailed information of a command action. * @param Controller $controller the controller instance * @param string $actionID action ID * @throws Exception if the action does not exist */ - protected function getControllerActionHelp($controller, $actionID) + protected function getSubCommandHelp($controller, $actionID) { $action = $controller->createAction($actionID); if ($action === null) { @@ -277,22 +257,9 @@ class HelpController extends Controller 'command' => rtrim($controller->getUniqueId() . '/' . $actionID, '/'), ])); } - $description = null; - if ($action instanceof InlineAction) { - $method = new \ReflectionMethod($controller, $action->actionMethod); - } else { - /** @var Action $action */ - $description = $action->getHelp(); - $method = new \ReflectionMethod($action, 'run'); - } - - $tags = $this->parseComment($method->getDocComment()); - $options = $this->getOptionHelps($controller, $actionID); - if ($description === null) { - $description = $controller->getActionHelp($actionID); - } - if ($description !== '') { + $description = $controller->getActionHelp($action); + if ($description != '') { $this->stdout("\nDESCRIPTION\n", Console::BOLD); $this->stdout("\n$description\n\n"); } @@ -304,6 +271,15 @@ class HelpController extends Controller } else { echo $scriptName . ' ' . $this->ansiFormat($action->getUniqueId(), Console::FG_YELLOW); } + + if ($action instanceof InlineAction) { + $method = new \ReflectionMethod($controller, $action->actionMethod); + } else { + $method = new \ReflectionMethod($action, 'run'); + } + $tags = $this->parseComment($method->getDocComment()); + $options = $this->getOptionHelps($controller, $actionID); + list ($required, $optional) = $this->getArgHelps($method, isset($tags['param']) ? $tags['param'] : []); foreach ($required as $arg => $description) { $this->stdout(' <' . $arg . '>', Console::FG_CYAN); diff --git a/framework/helpers/BaseConsole.php b/framework/helpers/BaseConsole.php index 98f1659..d2cda22 100644 --- a/framework/helpers/BaseConsole.php +++ b/framework/helpers/BaseConsole.php @@ -491,15 +491,15 @@ class BaseConsole * * First param is the string to convert, second is an optional flag if * colors should be used. It defaults to true, if set to false, the - * colorcodes will just be removed (And %% will be transformed into %) + * color codes will just be removed (And %% will be transformed into %) * * @param string $string String to convert * @param boolean $colored Should the string be colored? * @return string */ - // TODO rework/refactor according to https://github.com/yiisoft/yii2/issues/746 public static function renderColoredString($string, $colored = true) { + // TODO rework/refactor according to https://github.com/yiisoft/yii2/issues/746 static $conversions = [ '%y' => [self::FG_YELLOW], '%g' => [self::FG_GREEN], @@ -562,9 +562,9 @@ class BaseConsole * @access public * @return string */ - // TODO rework/refactor according to https://github.com/yiisoft/yii2/issues/746 public static function escape($string) { + // TODO rework/refactor according to https://github.com/yiisoft/yii2/issues/746 return str_replace('%', '%%', $string); }