Commit 19989353 by Qiang Xue

Merge pull request #2671 from hqx/2670-ConsoleLocalOptions

2670 console local options
parents d1a5c854 bf5de411
...@@ -113,9 +113,9 @@ class PhpDocController extends Controller ...@@ -113,9 +113,9 @@ class PhpDocController extends Controller
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function globalOptions() public function options($id)
{ {
return array_merge(parent::globalOptions(), ['updateFiles']); return array_merge(parent::options($id), ['updateFiles']);
} }
protected function updateClassPropertyDocs($file, $className, $propertyDoc) protected function updateClassPropertyDocs($file, $className, $propertyDoc)
......
...@@ -97,8 +97,8 @@ If a route does not contain an action ID, the default action will be executed. ...@@ -97,8 +97,8 @@ If a route does not contain an action ID, the default action will be executed.
### Options ### Options
By overriding the [[yii\console\Controller::globalOptions()]] method, you can specify options that are available By overriding the [[yii\console\Controller::options($id)]] method, you can specify options that are available
to a console command. The method should return a list of public property names of the controller class. to a console command (controller/actionID). The method should return a list of public property names of the controller class.
When running a command, you may specify the value of an option using the syntax `--OptionName=OptionValue`. When running a command, you may specify the value of an option using the syntax `--OptionName=OptionValue`.
This will assign `OptionValue` to the `OptionName` property of the controller class. This will assign `OptionValue` to the `OptionName` property of the controller class.
......
...@@ -296,7 +296,7 @@ Each console controller is like `CConsoleCommand` in 1.1. It consists of one or ...@@ -296,7 +296,7 @@ Each console controller is like `CConsoleCommand` in 1.1. It consists of one or
actions. You use the `yii <route>` command to execute a console command, where `<route>` actions. You use the `yii <route>` command to execute a console command, where `<route>`
stands for a controller route (e.g. `sitemap/index`). Additional anonymous arguments stands for a controller route (e.g. `sitemap/index`). Additional anonymous arguments
are passed as the parameters to the corresponding controller action method, and named arguments are passed as the parameters to the corresponding controller action method, and named arguments
are treated as global options declared in `globalOptions()`. are treated as options declared in `options($id)`.
Yii 2.0 supports automatic generation of command help information from comment blocks. Yii 2.0 supports automatic generation of command help information from comment blocks.
......
...@@ -154,8 +154,8 @@ class ApiController extends BaseController ...@@ -154,8 +154,8 @@ class ApiController extends BaseController
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function globalOptions() public function options($id)
{ {
return array_merge(parent::globalOptions(), ['template', 'guide']); return array_merge(parent::options($id), ['template', 'guide']);
} }
} }
...@@ -109,8 +109,8 @@ class GuideController extends BaseController ...@@ -109,8 +109,8 @@ class GuideController extends BaseController
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function globalOptions() public function options($id)
{ {
return array_merge(parent::globalOptions(), ['apiDocs']); return array_merge(parent::options($id), ['apiDocs']);
} }
} }
...@@ -121,8 +121,8 @@ abstract class BaseController extends Controller ...@@ -121,8 +121,8 @@ abstract class BaseController extends Controller
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function globalOptions() public function options($id)
{ {
return array_merge(parent::globalOptions(), ['template', 'exclude']); return array_merge(parent::options($id), ['template', 'exclude']);
} }
} }
...@@ -174,9 +174,9 @@ class FixtureController extends \yii\console\controllers\FixtureController ...@@ -174,9 +174,9 @@ class FixtureController extends \yii\console\controllers\FixtureController
* Returns the names of the global options for this command. * Returns the names of the global options for this command.
* @return array the names of the global options for this command. * @return array the names of the global options for this command.
*/ */
public function globalOptions() public function options($id)
{ {
return array_merge(parent::globalOptions(), [ return array_merge(parent::options($id), [
'templatePath', 'language', 'fixtureDataPath' 'templatePath', 'language', 'fixtureDataPath'
]); ]);
} }
......
...@@ -137,6 +137,7 @@ Yii Framework 2 Change Log ...@@ -137,6 +137,7 @@ Yii Framework 2 Change Log
- Enh #2526: Allow for null values in batchInsert (skotos) - Enh #2526: Allow for null values in batchInsert (skotos)
- Enh #2646: Added support for specifying hostinfo in the pattern of a URL rule (qiangxue) - Enh #2646: Added support for specifying hostinfo in the pattern of a URL rule (qiangxue)
- Enh #2661: Added boolean column type support for SQLite (qiangxue) - Enh #2661: Added boolean column type support for SQLite (qiangxue)
- Enh #2670: Changed `console\Controller::globalOptions()` to `options($actionId)` to (make it possible to) differentiate options per action (hqx)
- Enh: Added support for using arrays as option values for console commands (qiangxue) - Enh: Added support for using arrays as option values for console commands (qiangxue)
- Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark) - Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark)
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) - Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
......
...@@ -67,8 +67,8 @@ class Controller extends \yii\base\Controller ...@@ -67,8 +67,8 @@ class Controller extends \yii\base\Controller
public function runAction($id, $params = []) public function runAction($id, $params = [])
{ {
if (!empty($params)) { if (!empty($params)) {
// populate global options here so that they are available in beforeAction(). // populate options here so that they are available in beforeAction().
$options = $this->globalOptions(); $options = $this->options($id);
foreach ($params as $name => $value) { foreach ($params as $name => $value) {
if (in_array($name, $options, true)) { if (in_array($name, $options, true)) {
$default = $this->$name; $default = $this->$name;
...@@ -85,7 +85,7 @@ class Controller extends \yii\base\Controller ...@@ -85,7 +85,7 @@ class Controller extends \yii\base\Controller
/** /**
* Binds the parameters to the action. * Binds the parameters to the action.
* This method is invoked by [[Action]] when it begins to run with the given parameters. * This method is invoked by [[Action]] when it begins to run with the given parameters.
* This method will first bind the parameters with the [[globalOptions()|global options]] * This method will first bind the parameters with the [[options()|options]]
* available to the action. It then validates the given arguments. * available to the action. It then validates the given arguments.
* @param Action $action the action to be bound with parameters * @param Action $action the action to be bound with parameters
* @param array $params the parameters to be bound to the action * @param array $params the parameters to be bound to the action
...@@ -251,19 +251,22 @@ class Controller extends \yii\base\Controller ...@@ -251,19 +251,22 @@ class Controller extends \yii\base\Controller
return Console::select($prompt, $options); return Console::select($prompt, $options);
} }
/** /**
* Returns the names of the global options for this command. * Returns the names of valid options for the action (id)
* A global option requires the existence of a public member variable whose * An option requires the existence of a public member variable whose
* name is the option name. * name is the option name.
* Child classes may override this method to specify possible global options. * Child classes may override this method to specify possible options.
* *
* Note that the values setting via global options are not available * Note that the values setting via options are not available
* until [[beforeAction()]] is being called. * until [[beforeAction()]] is being called.
* *
* @return array the names of the global options for this command. * @param $id action name
* @return array the names of the options valid for the action
*/ */
public function globalOptions() public function options($id)
{ {
// $id might be used in subclass to provide options specific to action id
return ['color', 'interactive']; return ['color', 'interactive'];
} }
} }
...@@ -61,9 +61,9 @@ class FixtureController extends Controller ...@@ -61,9 +61,9 @@ class FixtureController extends Controller
* Returns the names of the global options for this command. * Returns the names of the global options for this command.
* @return array the names of the global options for this command. * @return array the names of the global options for this command.
*/ */
public function globalOptions() public function options($id)
{ {
return array_merge(parent::globalOptions(), [ return array_merge(parent::options($id), [
'namespace', 'globalFixtures' 'namespace', 'globalFixtures'
]); ]);
} }
......
...@@ -287,7 +287,7 @@ class HelpController extends Controller ...@@ -287,7 +287,7 @@ class HelpController extends Controller
} }
$tags = $this->parseComment($method->getDocComment()); $tags = $this->parseComment($method->getDocComment());
$options = $this->getOptionHelps($controller); $options = $this->getOptionHelps($controller, $actionID);
if ($tags['description'] !== '') { if ($tags['description'] !== '') {
$this->stdout("\nDESCRIPTION\n", Console::BOLD); $this->stdout("\nDESCRIPTION\n", Console::BOLD);
...@@ -317,7 +317,6 @@ class HelpController extends Controller ...@@ -317,7 +317,6 @@ class HelpController extends Controller
echo implode("\n\n", array_merge($required, $optional)) . "\n\n"; echo implode("\n\n", array_merge($required, $optional)) . "\n\n";
} }
$options = $this->getOptionHelps($controller);
if (!empty($options)) { if (!empty($options)) {
$this->stdout("\nOPTIONS\n\n", Console::BOLD); $this->stdout("\nOPTIONS\n\n", Console::BOLD);
echo implode("\n\n", $options) . "\n\n"; echo implode("\n\n", $options) . "\n\n";
...@@ -360,11 +359,12 @@ class HelpController extends Controller ...@@ -360,11 +359,12 @@ class HelpController extends Controller
/** /**
* Returns the help information about the options available for a console controller. * Returns the help information about the options available for a console controller.
* @param Controller $controller the console controller * @param Controller $controller the console controller
* @param string $actionID name of the action, if set include local options for that action
* @return array the help information about the options * @return array the help information about the options
*/ */
protected function getOptionHelps($controller) protected function getOptionHelps($controller, $actionID)
{ {
$optionNames = $controller->globalOptions(); $optionNames = $controller->options($actionID);
if (empty($optionNames)) { if (empty($optionNames)) {
return []; return [];
} }
......
...@@ -95,11 +95,12 @@ class MigrateController extends Controller ...@@ -95,11 +95,12 @@ class MigrateController extends Controller
* Returns the names of the global options for this command. * Returns the names of the global options for this command.
* @return array the names of the global options for this command. * @return array the names of the global options for this command.
*/ */
public function globalOptions() public function options($id)
{ {
return array_merge(parent::globalOptions(), [ return array_merge(parent::options($id),
'migrationPath', 'migrationTable', 'db', 'templateFile', 'interactive', 'color' ['migrationPath', 'migrationTable', 'db'], // global for all actions
]); ($id == 'create') ? ['templateFile'] : [] // action create
);
} }
/** /**
......
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