Controller.php 4.55 KB
Newer Older
Alexander Makarov committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Alexander Makarov committed
5 6 7 8 9
 * @license http://www.yiiframework.com/license/
 */

namespace yii\console;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\base\Action;
Qiang Xue committed
12
use yii\base\InlineAction;
Qiang Xue committed
13
use yii\base\InvalidRouteException;
Qiang Xue committed
14

Alexander Makarov committed
15
/**
Qiang Xue committed
16
 * Controller is the base class of console command classes.
Alexander Makarov committed
17
 *
Qiang Xue committed
18 19 20
 * A controller consists of one or several actions known as sub-commands.
 * Users call a console command by specifying the corresponding route which identifies a controller action.
 * The `yiic` program is used when calling a console command, like the following:
Alexander Makarov committed
21
 *
Qiang Xue committed
22
 * ~~~
Qiang Xue committed
23
 * yiic <route> [--param1=value1 --param2 ...]
Qiang Xue committed
24
 * ~~~
Alexander Makarov committed
25 26 27 28
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
29
class Controller extends \yii\base\Controller
Alexander Makarov committed
30
{
Qiang Xue committed
31 32 33 34 35 36
	/**
	 * @var boolean whether the call of [[confirm()]] requires a user input.
	 * If false, [[confirm()]] will always return true no matter what user enters or not.
	 */
	public $interactive = true;

Qiang Xue committed
37 38 39 40 41 42 43 44 45 46 47 48
	/**
	 * Runs an action with the specified action ID and parameters.
	 * If the action ID is empty, the method will use [[defaultAction]].
	 * @param string $id the ID of the action to be executed.
	 * @param array $params the parameters (name-value pairs) to be passed to the action.
	 * @return integer the status of the action execution. 0 means normal, other values mean abnormal.
	 * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully.
	 * @see createAction
	 */
	public function runAction($id, $params = array())
	{
		if ($params !== array()) {
Qiang Xue committed
49
			$options = $this->globalOptions();
Qiang Xue committed
50
			foreach ($params as $name => $value) {
Qiang Xue committed
51 52 53
				if (in_array($name, $options, true)) {
					$this->$name = $value;
					unset($params[$name]);
Qiang Xue committed
54 55 56 57 58 59
				}
			}
		}
		return parent::runAction($id, $params);
	}

Qiang Xue committed
60
	/**
Qiang Xue committed
61 62 63 64 65 66 67 68
	 * 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]]
	 * 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
	 * @return array the valid parameters that the action can run with.
	 * @throws Exception if there are unknown options or missing arguments
Qiang Xue committed
69
	 */
Qiang Xue committed
70
	public function bindActionParams($action, $params)
Qiang Xue committed
71
	{
Qiang Xue committed
72 73 74 75 76 77 78 79 80
		if ($params !== array()) {
			$options = $this->globalOptions();
			foreach ($params as $name => $value) {
				if (in_array($name, $options, true)) {
					$this->$name = $value;
					unset($params[$name]);
				}
			}
		}
Qiang Xue committed
81

Qiang Xue committed
82 83 84
		$args = isset($params[Request::ANONYMOUS_PARAMS]) ? $params[Request::ANONYMOUS_PARAMS] : array();
		unset($params[Request::ANONYMOUS_PARAMS]);
		if ($params !== array()) {
85
			throw new Exception(Yii::t('yii|Unknown options: {params}', array(
Qiang Xue committed
86
				'{params}' => implode(', ', array_keys($params)),
Qiang Xue committed
87
			)));
Qiang Xue committed
88
		}
Qiang Xue committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

		if ($action instanceof InlineAction) {
			$method = new \ReflectionMethod($this, $action->actionMethod);
		} else {
			$method = new \ReflectionMethod($action, 'run');
		}

		$missing = array();
		foreach ($method->getParameters() as $i => $param) {
			$name = $param->getName();
			if (!isset($args[$i])) {
				if ($param->isDefaultValueAvailable()) {
					$args[$i] = $param->getDefaultValue();
				} else {
					$missing[] = $name;
				}
			}
		}

		if ($missing !== array()) {
109
			throw new Exception(Yii::t('yii|Missing required arguments: {params}', array(
Qiang Xue committed
110
				'{params}' => implode(', ', $missing),
Qiang Xue committed
111
			)));
Alexander Makarov committed
112
		}
Qiang Xue committed
113 114

		return $args;
Alexander Makarov committed
115
	}
Alexander Makarov committed
116 117 118 119 120 121 122 123 124 125

	/**
	 * Asks user to confirm by typing y or n.
	 *
	 * @param string $message to echo out before waiting for user input
	 * @param boolean $default this value is returned if no selection is made.
	 * @return boolean whether user confirmed
	 */
	public function confirm($message, $default = false)
	{
Qiang Xue committed
126 127 128 129 130 131 132 133 134
		if ($this->interactive) {
			echo $message . ' (yes|no) [' . ($default ? 'yes' : 'no') . ']:';
			$input = trim(fgets(STDIN));
			return empty($input) ? $default : !strncasecmp($input, 'y', 1);
		} else {
			return true;
		}
	}

Qiang Xue committed
135 136
	/**
	 * Returns the names of the global options for this command.
137
	 * A global option requires the existence of a public member variable whose
Qiang Xue committed
138 139
	 * name is the option name.
	 * Child classes may override this method to specify possible global options.
140 141 142 143
	 *
	 * Note that the values setting via global options are not available
	 * until [[beforeAction()]] is being called.
	 *
Qiang Xue committed
144 145
	 * @return array the names of the global options for this command.
	 */
Qiang Xue committed
146 147 148
	public function globalOptions()
	{
		return array();
Alexander Makarov committed
149
	}
Zander Baldwin committed
150
}