Commit 05f7df30 by Christian Nadolle

console/Controller support local options per action

parent 80cb3e71
...@@ -67,6 +67,16 @@ class Controller extends \yii\base\Controller ...@@ -67,6 +67,16 @@ class Controller extends \yii\base\Controller
public function runAction($id, $params = []) public function runAction($id, $params = [])
{ {
if (!empty($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(). // populate global options here so that they are available in beforeAction().
$options = $this->globalOptions(); $options = $this->globalOptions();
foreach ($params as $name => $value) { foreach ($params as $name => $value) {
...@@ -266,4 +276,21 @@ class Controller extends \yii\base\Controller ...@@ -266,4 +276,21 @@ class Controller extends \yii\base\Controller
{ {
return ['color', 'interactive']; 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
* name is the option name.
* Child classes may override this method to specify possible local options.
*
* Note that the values setting via global 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)
*/
public function localOptions($id)
{
return [];
}
} }
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