Fix #16855: Ignore console commands that have no actions

This commit is contained in:
Dmitry V. Alekseev
2018-12-18 01:26:41 +03:00
committed by Alexander Makarov
parent 062ebf5c0b
commit 6e2b1782e5
8 changed files with 174 additions and 96 deletions

View File

@ -6,7 +6,7 @@ Yii Framework 2 Change Log
- Bug #17549: Fix `yii\db\ExpressionInterface` not supported in `yii\db\conditions\SimpleConditionBuilder` (razvanphp) - Bug #17549: Fix `yii\db\ExpressionInterface` not supported in `yii\db\conditions\SimpleConditionBuilder` (razvanphp)
- Bug #17434: Fix regular expression illegal character; Repeated fix for Internet Explorer 11 AJAX redirect bug in case of 301 and 302 response codes (`XMLHttpRequest: Network Error 0x800c0008`) (kamarton) - Bug #17434: Fix regular expression illegal character; Repeated fix for Internet Explorer 11 AJAX redirect bug in case of 301 and 302 response codes (`XMLHttpRequest: Network Error 0x800c0008`) (kamarton)
- Bug #16855: Ignore console commands that have no actions (alexeevdv)
2.0.26 September 03, 2019 2.0.26 September 03, 2019
------------------------- -------------------------
@ -255,6 +255,7 @@ Yii Framework 2 Change Log
- Enh: `yii\helpers\UnsetArrayValue`, `yii\helpers\ReplaceArrayValue` object now can be restored after serialization using `var_export()` function (silvefire) - Enh: `yii\helpers\UnsetArrayValue`, `yii\helpers\ReplaceArrayValue` object now can be restored after serialization using `var_export()` function (silvefire)
- Chg #16192: `yii\db\Command::logQuery()` is now protected, extracted `getCacheKey()` from `queryInternal()` (drlibra) - Chg #16192: `yii\db\Command::logQuery()` is now protected, extracted `getCacheKey()` from `queryInternal()` (drlibra)
- Chg #16941: Set `yii\console\controllers\MigrateController::useTablePrefix` to true as default value (GHopperMSK) - Chg #16941: Set `yii\console\controllers\MigrateController::useTablePrefix` to true as default value (GHopperMSK)
- Bug #16687: Add missing translations for `nl-NL` durations used in `yii\i18n\Formatter::asDuration()` (alexeevdv)
2.0.15.1 March 21, 2018 2.0.15.1 March 21, 2018

View File

@ -76,23 +76,20 @@ class UnknownCommandException extends Exception
$availableActions = []; $availableActions = [];
foreach ($helpController->getCommands() as $command) { foreach ($helpController->getCommands() as $command) {
$result = $this->application->createController($command); $result = $this->application->createController($command);
if ($result === false) {
continue;
}
// add the command itself (default action)
$availableActions[] = $command;
// add all actions of this controller
/** @var $controller Controller */ /** @var $controller Controller */
list($controller, $actionID) = $result; list($controller, $actionID) = $result;
if ($controller->createAction($controller->defaultAction) !== null) {
// add the command itself (default action)
$availableActions[] = $command;
}
// add all actions of this controller
$actions = $helpController->getActions($controller); $actions = $helpController->getActions($controller);
if (!empty($actions)) {
$prefix = $controller->getUniqueId(); $prefix = $controller->getUniqueId();
foreach ($actions as $action) { foreach ($actions as $action) {
$availableActions[] = $prefix . '/' . $action; $availableActions[] = $prefix . '/' . $action;
} }
} }
}
return $this->filterBySimilarity($availableActions, $this->command); return $this->filterBySimilarity($availableActions, $this->command);
} }

View File

@ -77,21 +77,18 @@ class HelpController extends Controller
{ {
foreach ($this->getCommandDescriptions() as $command => $description) { foreach ($this->getCommandDescriptions() as $command => $description) {
$result = Yii::$app->createController($command); $result = Yii::$app->createController($command);
if ($result === false || !($result[0] instanceof Controller)) {
continue;
}
/** @var $controller Controller */ /** @var $controller Controller */
list($controller, $actionID) = $result; list($controller, $actionID) = $result;
$actions = $this->getActions($controller); $actions = $this->getActions($controller);
if (!empty($actions)) {
$prefix = $controller->getUniqueId(); $prefix = $controller->getUniqueId();
if ($controller->createAction($controller->defaultAction) !== null) {
$this->stdout("$prefix\n"); $this->stdout("$prefix\n");
}
foreach ($actions as $action) { foreach ($actions as $action) {
$this->stdout("$prefix/$action\n"); $this->stdout("$prefix/$action\n");
} }
} }
} }
}
/** /**
* List all available options for the $action in machine readable format. * List all available options for the $action in machine readable format.
@ -174,7 +171,15 @@ class HelpController extends Controller
{ {
$commands = $this->getModuleCommands(Yii::$app); $commands = $this->getModuleCommands(Yii::$app);
sort($commands); sort($commands);
return array_unique($commands); return array_filter(array_unique($commands), function ($command) {
$result = Yii::$app->createController($command);
if ($result === false || !$result[0] instanceof Controller) {
return false;
}
list($controller, $actionID) = $result;
$actions = $this->getActions($controller);
return $actions !== [];
});
} }
/** /**
@ -185,16 +190,10 @@ class HelpController extends Controller
{ {
$descriptions = []; $descriptions = [];
foreach ($this->getCommands() as $command) { foreach ($this->getCommands() as $command) {
$description = '';
$result = Yii::$app->createController($command); $result = Yii::$app->createController($command);
if ($result !== false && $result[0] instanceof Controller) {
list($controller, $actionID) = $result;
/** @var Controller $controller */ /** @var Controller $controller */
$description = $controller->getHelpSummary(); list($controller, $actionID) = $result;
} $descriptions[$command] = $controller->getHelpSummary();
$descriptions[$command] = $description;
} }
return $descriptions; return $descriptions;
@ -292,42 +291,35 @@ class HelpController extends Controller
{ {
$commands = $this->getCommandDescriptions(); $commands = $this->getCommandDescriptions();
$this->stdout($this->getDefaultHelpHeader()); $this->stdout($this->getDefaultHelpHeader());
if (!empty($commands)) { if (empty($commands)) {
$this->stdout("\nNo commands are found.\n\n", Console::BOLD);
return;
}
$this->stdout("\nThe following commands are available:\n\n", Console::BOLD); $this->stdout("\nThe following commands are available:\n\n", Console::BOLD);
$len = 0; $maxLength = 0;
foreach ($commands as $command => $description) { foreach ($commands as $command => $description) {
$result = Yii::$app->createController($command); $result = Yii::$app->createController($command);
if ($result !== false && $result[0] instanceof Controller) {
/** @var $controller Controller */ /** @var $controller Controller */
list($controller, $actionID) = $result; list($controller, $actionID) = $result;
$actions = $this->getActions($controller); $actions = $this->getActions($controller);
if (!empty($actions)) {
$prefix = $controller->getUniqueId(); $prefix = $controller->getUniqueId();
foreach ($actions as $action) { foreach ($actions as $action) {
$string = $prefix . '/' . $action; $string = $prefix . '/' . $action;
if ($action === $controller->defaultAction) { if ($action === $controller->defaultAction) {
$string .= ' (default)'; $string .= ' (default)';
} }
if (($l = strlen($string)) > $len) { $maxLength = max($maxLength, strlen($string));
$len = $l;
}
}
}
} elseif (($l = strlen($command)) > $len) {
$len = $l;
} }
} }
foreach ($commands as $command => $description) { foreach ($commands as $command => $description) {
$this->stdout('- ' . $this->ansiFormat($command, Console::FG_YELLOW));
$this->stdout(str_repeat(' ', $len + 4 - strlen($command)));
$this->stdout(Console::wrapText($description, $len + 4 + 2), Console::BOLD);
$this->stdout("\n");
$result = Yii::$app->createController($command); $result = Yii::$app->createController($command);
if ($result !== false && $result[0] instanceof Controller) {
list($controller, $actionID) = $result; list($controller, $actionID) = $result;
$actions = $this->getActions($controller); $actions = $this->getActions($controller);
if (!empty($actions)) { $this->stdout('- ' . $this->ansiFormat($command, Console::FG_YELLOW));
$this->stdout(str_repeat(' ', $maxLength + 4 - strlen($command)));
$this->stdout(Console::wrapText($description, $maxLength + 4 + 2), Console::BOLD);
$this->stdout("\n");
$prefix = $controller->getUniqueId(); $prefix = $controller->getUniqueId();
foreach ($actions as $action) { foreach ($actions as $action) {
$string = ' ' . $prefix . '/' . $action; $string = ' ' . $prefix . '/' . $action;
@ -338,22 +330,17 @@ class HelpController extends Controller
} }
$summary = $controller->getActionHelpSummary($controller->createAction($action)); $summary = $controller->getActionHelpSummary($controller->createAction($action));
if ($summary !== '') { if ($summary !== '') {
$this->stdout(str_repeat(' ', $len + 4 - strlen($string))); $this->stdout(str_repeat(' ', $maxLength + 4 - strlen($string)));
$this->stdout(Console::wrapText($summary, $len + 4 + 2)); $this->stdout(Console::wrapText($summary, $maxLength + 4 + 2));
} }
$this->stdout("\n"); $this->stdout("\n");
} }
}
$this->stdout("\n"); $this->stdout("\n");
} }
}
$scriptName = $this->getScriptName(); $scriptName = $this->getScriptName();
$this->stdout("\nTo see the help of each command, enter:\n", Console::BOLD); $this->stdout("\nTo see the help of each command, enter:\n", Console::BOLD);
$this->stdout("\n $scriptName " . $this->ansiFormat('help', Console::FG_YELLOW) . ' ' $this->stdout("\n $scriptName " . $this->ansiFormat('help', Console::FG_YELLOW) . ' '
. $this->ansiFormat('<command-name>', Console::FG_CYAN) . "\n\n"); . $this->ansiFormat('<command-name>', Console::FG_CYAN) . "\n\n");
} else {
$this->stdout("\nNo commands are found.\n\n", Console::BOLD);
}
} }
/** /**
@ -378,9 +365,7 @@ class HelpController extends Controller
$maxlen = 5; $maxlen = 5;
foreach ($actions as $action) { foreach ($actions as $action) {
$len = strlen($prefix . '/' . $action) + 2 + ($action === $controller->defaultAction ? 10 : 0); $len = strlen($prefix . '/' . $action) + 2 + ($action === $controller->defaultAction ? 10 : 0);
if ($maxlen < $len) { $maxlen = max($maxlen, $len);
$maxlen = $len;
}
} }
foreach ($actions as $action) { foreach ($actions as $action) {
$this->stdout('- ' . $this->ansiFormat($prefix . '/' . $action, Console::FG_YELLOW)); $this->stdout('- ' . $this->ansiFormat($prefix . '/' . $action, Console::FG_YELLOW));

View File

@ -0,0 +1,27 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\data\console\controllers;
use yii\console\Controller;
/**
* @author Dmitry V. Alekseev <mail@alexeevdv.ru>
* @since 2.0.16
*/
class FakeController extends Controller
{
public $defaultAction = 'default';
public function actionDefault()
{
}
public function actionSecond()
{
}
}

View File

@ -0,0 +1,18 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\data\console\controllers;
use yii\console\Controller;
/**
* @author Dmitry V. Alekseev <mail@alexeevdv.ru>
* @since 2.0.16
*/
class FakeEmptyController extends Controller
{
}

View File

@ -0,0 +1,23 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\data\console\controllers;
use yii\console\Controller;
/**
* @author Dmitry V. Alekseev <mail@alexeevdv.ru>
* @since 2.0.16
*/
class FakeNoDefaultController extends Controller
{
public $defaultAction = 'not-exist';
public function actionIndex()
{
}
}

View File

@ -24,6 +24,9 @@ class UnknownCommandExceptionTest extends TestCase
'cache' => 'yii\console\controllers\CacheController', 'cache' => 'yii\console\controllers\CacheController',
'migrate' => 'yii\console\controllers\MigrateController', 'migrate' => 'yii\console\controllers\MigrateController',
'message' => 'yii\console\controllers\MessageController', 'message' => 'yii\console\controllers\MessageController',
'whatever' => 'yiiunit\data\console\controllers\FakeController',
'whatever-empty' => 'yiiunit\data\console\controllers\FakeEmptyController',
'whatever-no-default' => 'yiiunit\data\console\controllers\FakeNoDefaultController',
], ],
]); ]);
} }
@ -49,6 +52,7 @@ class UnknownCommandExceptionTest extends TestCase
[str_repeat('asdw1234', 31), []], [str_repeat('asdw1234', 31), []],
[str_repeat('asdw1234', 32), []], [str_repeat('asdw1234', 32), []],
[str_repeat('asdw1234', 33), []], [str_repeat('asdw1234', 33), []],
['what', ['whatever', 'whatever/default', 'whatever/second', 'whatever-no-default/index']],
]; ];
} }

View File

@ -68,10 +68,8 @@ help/index
help/list help/list
help/list-action-options help/list-action-options
help/usage help/usage
magic/e-tag
magic/e-tag/delete magic/e-tag/delete
magic/e-tag/list-e-tags magic/e-tag/list-e-tags
magic/subFolder/sub
magic/subFolder/sub/test magic/subFolder/sub/test
STRING STRING
@ -187,6 +185,31 @@ STRING
$this->assertContains('--port, -p: int (defaults to 8080)', $result); $this->assertContains('--port, -p: int (defaults to 8080)', $result);
$this->assertContains('--router, -r: string', $result); $this->assertContains('--router, -r: string', $result);
} }
public function testActionListContainsNoEmptyCommands()
{
$this->mockApplication([
'enableCoreCommands' => false,
'controllerNamespace' => 'yiiunit\data\console\controllers',
]);
$result = Console::stripAnsiFormat($this->runControllerAction('list'));
$this->assertNotContains("fake-empty\n", $result);
$this->assertNotContains("fake-no-default\n", $result);
$this->assertContains("fake-no-default/index\n", $result);
}
public function testActionIndexContainsNoEmptyCommands()
{
$this->mockApplication([
'enableCoreCommands' => false,
'controllerNamespace' => 'yiiunit\data\console\controllers',
]);
$result = Console::stripAnsiFormat($this->runControllerAction('index'));
$this->assertNotContains("- fake-empty", $result);
$this->assertContains("- fake-no-default", $result);
$this->assertContains(" fake-no-default/index", $result);
$this->assertNotContains(" fake-no-default/index (default)", $result);
}
} }