From 607294a4f1ae59b1fc0667bd39a7590bb78bbf6a Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 24 Jun 2014 10:50:43 -0400 Subject: [PATCH] Fixes #2821: Console help command incorrectly lists non-console controllers as available commands --- framework/CHANGELOG.md | 1 + .../console/controllers/HelpController.php | 20 ++++++++++++++++++- framework/yii | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 8d13093be5..9ca41ee697 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -8,6 +8,7 @@ Yii Framework 2 Change Log - Bug #2314: Gii model generator does not generate correct relation type in some special case (qiangxue) - Bug #2563: Theming is not working if the path map of the theme contains ".." or "." in the paths (qiangxue) - Bug #2801: Fixed the issue that GridView gets footer content before data cells content (ElisDN) +- Bug #2821: Console help command incorrectly lists non-console controllers as available commands (qiangxue) - Bug #2853: ActiveRecord did not handle resource-typed columns well (chris68, qiangxue) - Bug #3042: `yii\widgets\Pjax` should end application right after it finishes responding to a pjax request (qiangxue) - Bug #3066: `yii\db\mssql\Schema::getTableSchema()` should return null when the table does not exist (qiangxue) diff --git a/framework/console/controllers/HelpController.php b/framework/console/controllers/HelpController.php index b6990a5b57..43e8c955ff 100644 --- a/framework/console/controllers/HelpController.php +++ b/framework/console/controllers/HelpController.php @@ -157,7 +157,10 @@ class HelpController extends Controller $files = scandir($controllerPath); foreach ($files as $file) { if (strcmp(substr($file, -14), 'Controller.php') === 0) { - $commands[] = $prefix . Inflector::camel2id(substr(basename($file), 0, -14)); + $controllerClass = $module->controllerNamespace . '\\' . substr(basename($file), 0, -4); + if ($this->validateControllerClass($controllerClass)) { + $commands[] = $prefix . Inflector::camel2id(substr(basename($file), 0, -14)); + } } } } @@ -165,6 +168,21 @@ class HelpController extends Controller return $commands; } + /** + * Validates if the given class is a valid console controller class. + * @param string $controllerClass + * @return bool + */ + protected function validateControllerClass($controllerClass) + { + if (class_exists($controllerClass)) { + $class = new \ReflectionClass($controllerClass); + return !$class->isAbstract() && $class->isSubclassOf('yii\console\Controller'); + } else { + return false; + } + } + /** * Displays all available commands. */ diff --git a/framework/yii b/framework/yii index 4024f4a20b..d7fa01c792 100755 --- a/framework/yii +++ b/framework/yii @@ -19,6 +19,7 @@ require(__DIR__ . '/Yii.php'); $application = new yii\console\Application([ 'id' => 'yii-console', 'basePath' => __DIR__ . '/console', + 'controllerNamespace' => 'yii\console\controllers', ]); $exitCode = $application->run(); exit($exitCode);