Commit 706ab7f9 by Christian Nadolle

combine globalOptions() with local options into options($id)

adjust existing Console Controllers and docs
parent b0d7559a
......@@ -113,9 +113,9 @@ class PhpDocController extends Controller
/**
* @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)
......
......@@ -97,8 +97,8 @@ If a route does not contain an action ID, the default action will be executed.
### Options
By overriding the [[yii\console\Controller::globalOptions()]] 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.
By overriding the [[yii\console\Controller::options($id)]] method, you can specify options that are available
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`.
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
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
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.
......
......@@ -154,8 +154,8 @@ class ApiController extends BaseController
/**
* @inheritdoc
*/
public function globalOptions()
public function options($id)
{
return array_merge(parent::globalOptions(), ['template', 'guide']);
return array_merge(parent::options($id), ['template', 'guide']);
}
}
\ No newline at end of file
......@@ -109,8 +109,8 @@ class GuideController extends BaseController
/**
* @inheritdoc
*/
public function globalOptions()
public function options($id)
{
return array_merge(parent::globalOptions(), ['apiDocs']);
return array_merge(parent::options($id), ['apiDocs']);
}
}
\ No newline at end of file
......@@ -121,8 +121,8 @@ abstract class BaseController extends Controller
/**
* @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
* Returns 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'
]);
}
......
......@@ -67,18 +67,8 @@ class Controller extends \yii\base\Controller
public function runAction($id, $params = [])
{
if (!empty($params)) {
// extract valid local options first so that they don't throw "unknown option"- exception below.
$options = $this->localOptions($id);
foreach ($params as $name => $value) {
if (in_array($name, $options, true)) {
$default = $this->$name;
$this->$name = is_array($default) ? preg_split('/\s*,\s*/', $value) : $value;
unset($params[$name]);
}
}
// populate global options here so that they are available in beforeAction().
$options = $this->globalOptions();
// populate options here so that they are available in beforeAction().
$options = $this->options($id);
foreach ($params as $name => $value) {
if (in_array($name, $options, true)) {
$default = $this->$name;
......@@ -95,7 +85,7 @@ class Controller extends \yii\base\Controller
/**
* Binds the parameters to the action.
* 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.
* @param Action $action the action to be bound with parameters
* @param array $params the parameters to be bound to the action
......@@ -261,36 +251,22 @@ class Controller extends \yii\base\Controller
return Console::select($prompt, $options);
}
/**
* Returns the names of the global options for this command.
* A global option requires the existence of a public member variable whose
* name is the option name.
* Child classes may override this method to specify possible global options.
*
* Note that the values setting via global options are not available
* until [[beforeAction()]] is being called.
*
* @return array the names of the global options for this command.
*/
public function globalOptions()
{
return ['color', 'interactive'];
}
/**
* Returns the names of valid options local to the action (id)
* A local option requires the existence of a public member variable whose
* Returns the names of valid options for the action (id)
* An option requires the existence of a public member variable whose
* name is the option name.
* Child classes may override this method to specify possible local 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.
*
* @param $id action name
* @return array the names of the options valid for this action (in addition to global options above)
* @return array the names of the options valid for the action
*/
public function localOptions($id)
public function options($id)
{
return [];
// $id might be used in subclass to provide options specific to action id
return ['color', 'interactive'];
}
}
......@@ -61,9 +61,9 @@ class FixtureController extends Controller
* Returns 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'
]);
}
......
......@@ -359,15 +359,12 @@ class HelpController extends Controller
/**
* Returns the help information about the options available for a console controller.
* @param Controller $controller the console controller
* @param String|null $actionID name of the action, if set include local options for that action
* @param string $actionID name of the action, if set include local options for that action
* @return array the help information about the options
*/
protected function getOptionHelps($controller, $actionID = null)
protected function getOptionHelps($controller, $actionID)
{
$optionNames = $controller->globalOptions();
if (! empty($actionID)) {
$optionNames = array_merge($optionNames, $controller->localOptions($actionID));
}
$optionNames = $controller->options($actionID);
if (empty($optionNames)) {
return [];
}
......
......@@ -95,9 +95,9 @@ class MigrateController extends Controller
* Returns 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'
]);
}
......
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