Commit dbac35b1 by Alexander Makarov

Removed description and help code duplication

parent d74e89a1
......@@ -14,32 +14,33 @@ use yii\helpers\Console;
* Action is the base class for all controller action classes.
*
* @inheritdoc
* @property \yii\console\Controller $controller
*
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class Action extends \yii\base\Action
{
/**
* Returns a short description (one line) of information about the action.
*
* The default implementation returns help information retrieved from the PHPDoc comments.
*
* @return string
*/
public function getDescription()
{
$class = new \ReflectionClass($this);
$docLines = preg_split('~(\n|\r|\r\n)~', $class->getDocComment());
if (isset($docLines[1])) {
return trim($docLines[1], ' *');
}
return '';
return null;
}
/**
* Returns help information for the action.
*
* The default implementation returns help information retrieved from the PHPDoc comments.
* @return string
*/
public function getHelp()
{
$class = new \ReflectionClass($this);
$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 !== '') {
return rtrim(Console::renderColoredString(Console::markdownToAnsi($comment)));
}
return '';
return null;
}
}
......@@ -284,18 +284,30 @@ class Controller extends \yii\base\Controller
}
/**
* Returns a short description (one line) of information about this controller or an action.
* Returns a short description (one line) of information about this controller or it's action (if specified).
*
* You may override this method to return customized help information for this controller.
* You may override this method to return customized description.
* The default implementation returns help information retrieved from the PHPDoc comments
* of the controller class.
*
* @param string $actionID action to get description for. null means overall controller description.
* @return string
*/
public function getDescription()
public function getDescription($actionID = null)
{
$action = null;
if ($actionID === null) {
$class = new \ReflectionClass($this);
$docLines = preg_split('~(\n|\r|\r\n)~', $class->getDocComment());
} else {
$action = $this->createAction($actionID);
$class = new \ReflectionClass($action);
}
if ($action instanceof InlineAction) {
$class = new \ReflectionMethod($this, $action->actionMethod);
}
$docLines = preg_split('~\R~', $class->getDocComment());
if (isset($docLines[1])) {
return trim($docLines[1], ' *');
}
......@@ -303,15 +315,27 @@ class Controller extends \yii\base\Controller
}
/**
* Returns help information for this controller.
* Returns help information for this controller or it's action (if specified).
*
* You may override this method to return customized help.
* The default implementation returns help information retrieved from the PHPDoc comments
* of the controller class.
* @param string $actionID action to get help for. null means overall controller help.
* @return string
*/
public function getHelp()
public function getHelp($actionID = null)
{
$action = null;
if ($actionID === null) {
$class = new \ReflectionClass($this);
} else {
$class = new \ReflectionClass($this->createAction($actionID));
}
if ($action instanceof InlineAction) {
$class = new \ReflectionMethod($this, $action->actionMethod);
}
$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]));
......
......@@ -20,26 +20,27 @@ use yii\helpers\Console;
*/
class InlineAction extends \yii\base\InlineAction
{
/**
* Returns a short description (one line) of information about the action.
*
* The default implementation returns help information retrieved from the PHPDoc comments.
*
* @return string
*/
public function getDescription()
{
$class = new \ReflectionMethod($this->controller, $this->actionMethod);
$docLines = preg_split('~(\n|\r|\r\n)~', $class->getDocComment());
if (isset($docLines[1])) {
return trim($docLines[1], ' *');
}
return '';
return null;
}
/**
* Returns help information for the action.
*
* The default implementation returns help information retrieved from the PHPDoc comments.
* @return string
*/
public function getHelp()
{
$class = new \ReflectionMethod($this->controller, $this->actionMethod);
$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 !== '') {
return rtrim(Console::renderColoredString(Console::markdownToAnsi($comment)));
}
return '';
return null;
}
}
......@@ -250,11 +250,15 @@ class HelpController extends Controller
*/
protected function getActionSummary($controller, $actionID)
{
$description = '';
$action = $controller->createAction($actionID);
if ($action instanceof InlineAction || $action instanceof Action) {
return $action->getDescription();
$description = $action->getDescription();
if ($description === null) {
$description = $controller->getDescription($actionID);
}
}
return '';
return $description;
}
/**
......@@ -281,6 +285,9 @@ class HelpController extends Controller
$options = $this->getOptionHelps($controller, $actionID);
$description = $action->getHelp();
if ($description === null) {
$description = $controller->getHelp($actionID);
}
if ($description !== '') {
$this->stdout("\nDESCRIPTION\n", Console::BOLD);
$this->stdout("\n$description\n\n");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment