You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					265 lines
				
				8.0 KiB
			
		
		
			
		
	
	
					265 lines
				
				8.0 KiB
			| 
											14 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace yii\console;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | use Yii;
 | ||
| 
											14 years ago
										 | use yii\base\Action;
 | ||
| 
											13 years ago
										 | use yii\base\InlineAction;
 | ||
| 
											13 years ago
										 | use yii\base\InvalidRouteException;
 | ||
| 
											13 years ago
										 | use yii\helpers\Console;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											14 years ago
										 | /**
 | ||
| 
											14 years ago
										 |  * Controller is the base class of console command classes.
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * 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:
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
| 
											13 years ago
										 |  * yiic <route> [--param1=value1 --param2 ...]
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											14 years ago
										 | class Controller extends \yii\base\Controller
 | ||
| 
											14 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @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;
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var bool whether to enable ANSI style in output.
 | ||
|  | 	 * Setting this will affect [[ansiFormat()]], [[stdout()]] and [[stderr()]].
 | ||
|  | 	 * If not set it will be auto detected using [[yii\helpers\Console::streamSupportsAnsiColors()]] with STDOUT
 | ||
|  | 	 * for [[ansiFormat()]] and [[stdout()]] and STDERR for [[stderr()]].
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $colors;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * 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())
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		if (!empty($params)) {
 | ||
| 
											13 years ago
										 | 			$options = $this->globalOptions();
 | ||
| 
											13 years ago
										 | 			foreach ($params as $name => $value) {
 | ||
| 
											13 years ago
										 | 				if (in_array($name, $options, true)) {
 | ||
|  | 					$this->$name = $value;
 | ||
|  | 					unset($params[$name]);
 | ||
| 
											13 years ago
										 | 				}
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		return parent::runAction($id, $params);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * 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
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function bindActionParams($action, $params)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if (!empty($params)) {
 | ||
| 
											13 years ago
										 | 			$options = $this->globalOptions();
 | ||
|  | 			foreach ($params as $name => $value) {
 | ||
|  | 				if (in_array($name, $options, true)) {
 | ||
|  | 					$this->$name = $value;
 | ||
|  | 					unset($params[$name]);
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		$args = isset($params[Request::ANONYMOUS_PARAMS]) ? $params[Request::ANONYMOUS_PARAMS] : array();
 | ||
|  | 		unset($params[Request::ANONYMOUS_PARAMS]);
 | ||
| 
											13 years ago
										 | 		if (!empty($params)) {
 | ||
| 
											13 years ago
										 | 			throw new Exception(Yii::t('yii|Unknown options: {params}', array(
 | ||
| 
											13 years ago
										 | 				'{params}' => implode(', ', array_keys($params)),
 | ||
| 
											13 years ago
										 | 			)));
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 		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;
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		if (!empty($missing)) {
 | ||
| 
											13 years ago
										 | 			throw new Exception(Yii::t('yii|Missing required arguments: {params}', array(
 | ||
| 
											13 years ago
										 | 				'{params}' => implode(', ', $missing),
 | ||
| 
											13 years ago
										 | 			)));
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 		return $args;
 | ||
| 
											14 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Formats a string with ANSI codes
 | ||
|  | 	 *
 | ||
|  | 	 * You may pass additional parameters using the constants defined in [[yii\helpers\base\Console]].
 | ||
|  | 	 *
 | ||
|  | 	 * Example:
 | ||
|  | 	 * ~~~
 | ||
| 
											13 years ago
										 | 	 * $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
 | ||
| 
											13 years ago
										 | 	 * ~~~
 | ||
|  | 	 *
 | ||
|  | 	 * @param string $string the string to be formatted
 | ||
|  | 	 * @return string
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function ansiFormat($string)
 | ||
| 
											13 years ago
										 | 	{
 | ||
|  | 		if ($this->ansi === true || $this->ansi === null && Console::streamSupportsAnsiColors(STDOUT)) {
 | ||
|  | 			$args = func_get_args();
 | ||
|  | 			array_shift($args);
 | ||
|  | 			$string = Console::ansiFormat($string, $args);
 | ||
|  | 		}
 | ||
|  | 		return $string;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Prints a string to STDOUT
 | ||
|  | 	 *
 | ||
|  | 	 * You may optionally format the string with ANSI codes by
 | ||
|  | 	 * passing additional parameters using the constants defined in [[yii\helpers\base\Console]].
 | ||
|  | 	 *
 | ||
|  | 	 * Example:
 | ||
|  | 	 * ~~~
 | ||
|  | 	 * $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
 | ||
|  | 	 * ~~~
 | ||
|  | 	 *
 | ||
|  | 	 * @param string $string the string to print
 | ||
|  | 	 * @return int|boolean Number of bytes printed or false on error
 | ||
|  | 	 */
 | ||
|  | 	public function stdout($string)
 | ||
|  | 	{
 | ||
|  | 		if ($this->ansi === true || $this->ansi === null && Console::streamSupportsAnsiColors(STDOUT)) {
 | ||
|  | 			$args = func_get_args();
 | ||
|  | 			array_shift($args);
 | ||
|  | 			$string = Console::ansiFormat($string, $args);
 | ||
|  | 		}
 | ||
|  | 		return Console::stdout($string);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Prints a string to STDERR
 | ||
|  | 	 *
 | ||
|  | 	 * You may optionally format the string with ANSI codes by
 | ||
|  | 	 * passing additional parameters using the constants defined in [[yii\helpers\base\Console]].
 | ||
|  | 	 *
 | ||
|  | 	 * Example:
 | ||
|  | 	 * ~~~
 | ||
|  | 	 * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
 | ||
|  | 	 * ~~~
 | ||
|  | 	 *
 | ||
|  | 	 * @param string $string the string to print
 | ||
|  | 	 * @return int|boolean Number of bytes printed or false on error
 | ||
|  | 	 */
 | ||
|  | 	public function stderr($string)
 | ||
|  | 	{
 | ||
|  | 		if ($this->ansi === true || $this->ansi === null && Console::streamSupportsAnsiColors(STDERR)) {
 | ||
|  | 			$args = func_get_args();
 | ||
|  | 			array_shift($args);
 | ||
|  | 			$string = Console::ansiFormat($string, $args);
 | ||
|  | 		}
 | ||
|  | 		return fwrite(STDERR, $string);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Prompts the user for input and validates it
 | ||
|  | 	 *
 | ||
|  | 	 * @param string $text prompt string
 | ||
|  | 	 * @param array $options the options to validate the input:
 | ||
|  | 	 *  - required: whether it is required or not
 | ||
|  | 	 *  - default: default value if no input is inserted by the user
 | ||
|  | 	 *  - pattern: regular expression pattern to validate user input
 | ||
|  | 	 *  - validator: a callable function to validate input. The function must accept two parameters:
 | ||
|  | 	 *      - $input: the user input to validate
 | ||
|  | 	 *      - $error: the error value passed by reference if validation failed.
 | ||
|  | 	 * @return string the user input
 | ||
|  | 	 */
 | ||
|  | 	public function prompt($text, $options = array())
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		if ($this->interactive) {
 | ||
|  | 			return Console::prompt($text, $options);
 | ||
|  | 		} else {
 | ||
|  | 			return isset($options['default']) ? $options['default'] : '';
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * 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)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		if ($this->interactive) {
 | ||
| 
											13 years ago
										 | 			return Console::confirm($message, $default);
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
|  | 			return true;
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * Gives the user an option to choose from. Giving '?' as an input will show
 | ||
|  | 	 * a list of options to choose from and their explanations.
 | ||
|  | 	 *
 | ||
|  | 	 * @param string $prompt the prompt message
 | ||
|  | 	 * @param array  $options Key-value array of options to choose from
 | ||
|  | 	 *
 | ||
|  | 	 * @return string An option character the user chose
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function select($prompt, $options = array())
 | ||
| 
											13 years ago
										 | 	{
 | ||
|  | 		return Console::select($prompt, $options);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Returns the names of the global options for this command.
 | ||
| 
											13 years ago
										 | 	 * A global option requires the existence of a public member variable whose
 | ||
| 
											13 years ago
										 | 	 * name is the option name.
 | ||
|  | 	 * Child classes may override this method to specify possible global options.
 | ||
| 
											13 years ago
										 | 	 *
 | ||
|  | 	 * Note that the values setting via global options are not available
 | ||
|  | 	 * until [[beforeAction()]] is being called.
 | ||
|  | 	 *
 | ||
| 
											13 years ago
										 | 	 * @return array the names of the global options for this command.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function globalOptions()
 | ||
|  | 	{
 | ||
|  | 		return array();
 | ||
| 
											13 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | }
 |