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.
		
		
		
		
			
				
					279 lines
				
				8.2 KiB
			
		
		
			
		
	
	
					279 lines
				
				8.2 KiB
			| 
											14 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * Command class file.
 | ||
|  |  *
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											14 years ago
										 |  * @copyright Copyright © 2008-2012 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace yii\console;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * Command represents an executable console command.
 | ||
|  |  *
 | ||
|  |  * It works like {@link \yii\web\Controller} by parsing command line options and dispatching
 | ||
|  |  * the request to a specific action with appropriate option values.
 | ||
|  |  *
 | ||
|  |  * Users call a console command via the following command format:
 | ||
|  |  * <pre>
 | ||
|  |  * yiic CommandName ActionName --Option1=Value1 --Option2=Value2 ...
 | ||
|  |  * </pre>
 | ||
|  |  *
 | ||
|  |  * Child classes mainly needs to implement various action methods whose name must be
 | ||
|  |  * prefixed with "action". The parameters to an action method are considered as options
 | ||
|  |  * for that specific action. The action specified as {@link defaultAction} will be invoked
 | ||
|  |  * when a user does not specify the action name in his command.
 | ||
|  |  *
 | ||
|  |  * Options are bound to action parameters via parameter names. For example, the following
 | ||
|  |  * action method will allow us to run a command with <code>yiic sitemap --type=News</code>:
 | ||
|  |  * <pre>
 | ||
|  |  * class SitemapCommand {
 | ||
|  |  *     public function actionIndex($type) {
 | ||
|  |  *         ....
 | ||
|  |  *     }
 | ||
|  |  * }
 | ||
|  |  * </pre>
 | ||
|  |  *
 | ||
|  |  * @property string $name The command name.
 | ||
|  |  * @property CommandRunner $commandRunner The command runner instance.
 | ||
|  |  * @property string $help The command description. Defaults to 'Usage: php entry-script.php command-name'.
 | ||
|  |  * @property array $optionHelp The command option help information. Each array element describes
 | ||
|  |  * the help information for a single action.
 | ||
|  |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											14 years ago
										 | class Controller extends \yii\base\Controller
 | ||
| 
											14 years ago
										 | {
 | ||
|  | 	/**
 | ||
|  | 	 * Executes the command.
 | ||
|  | 	 * The default implementation will parse the input parameters and
 | ||
|  | 	 * dispatch the command request to an appropriate action with the corresponding
 | ||
|  | 	 * option values
 | ||
|  | 	 * @param array $args command line parameters for this command.
 | ||
|  | 	 */
 | ||
|  | 	public function run($args)
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		list($action, $options, $args) = $this->resolveRequest($args);
 | ||
|  | 		$methodName = 'action' . $action;
 | ||
|  | 		if (!preg_match('/^\w+$/', $action) || !method_exists($this, $methodName))
 | ||
|  | 				{
 | ||
|  | 					$this->usageError("Unknown action: " . $action);
 | ||
|  | 				}
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											14 years ago
										 | 		$method = new \ReflectionMethod($this, $methodName);
 | ||
|  | 		$params = array();
 | ||
| 
											14 years ago
										 | 		// named and unnamed options
 | ||
| 
											14 years ago
										 | 		foreach ($method->getParameters() as $param) {
 | ||
|  | 			$name = $param->getName();
 | ||
|  | 			if (isset($options[$name])) {
 | ||
|  | 				if ($param->isArray())
 | ||
|  | 					$params[] = is_array($options[$name]) ? $options[$name] : array($options[$name]);
 | ||
|  | 				else if (!is_array($options[$name]))
 | ||
|  | 					$params[] = $options[$name];
 | ||
| 
											14 years ago
										 | 				else
 | ||
|  | 					$this->usageError("Option --$name requires a scalar. Array is given.");
 | ||
| 
											14 years ago
										 | 			} else if ($name === 'args')
 | ||
|  | 				$params[] = $args;
 | ||
|  | 			else if ($param->isDefaultValueAvailable())
 | ||
|  | 				$params[] = $param->getDefaultValue();
 | ||
| 
											14 years ago
										 | 			else
 | ||
|  | 				$this->usageError("Missing required option --$name.");
 | ||
|  | 			unset($options[$name]);
 | ||
|  | 		}
 | ||
|  | 
 | ||
|  | 		// try global options
 | ||
| 
											14 years ago
										 | 		if (!empty($options)) {
 | ||
|  | 			$class = new \ReflectionClass(get_class($this));
 | ||
|  | 			foreach ($options as $name => $value) {
 | ||
|  | 				if ($class->hasProperty($name)) {
 | ||
|  | 					$property = $class->getProperty($name);
 | ||
|  | 					if ($property->isPublic() && !$property->isStatic()) {
 | ||
|  | 						$this->$name = $value;
 | ||
| 
											14 years ago
										 | 						unset($options[$name]);
 | ||
|  | 					}
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											14 years ago
										 | 		if (!empty($options))
 | ||
|  | 			$this->usageError("Unknown options: " . implode(', ', array_keys($options)));
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											14 years ago
										 | 		if ($this->beforeAction($action, $params)) {
 | ||
|  | 			$method->invokeArgs($this, $params);
 | ||
|  | 			$this->afterAction($action, $params);
 | ||
| 
											14 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Parses the command line arguments and determines which action to perform.
 | ||
|  | 	 * @param array $args command line arguments
 | ||
|  | 	 * @return array the action name, named options (name=>value), and unnamed options
 | ||
|  | 	 */
 | ||
|  | 	protected function resolveRequest($args)
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		$options = array(); // named parameters
 | ||
|  | 		$params = array(); // unnamed parameters
 | ||
|  | 		foreach ($args as $arg) {
 | ||
|  | 			if (preg_match('/^--(\w+)(=(.*))?$/', $arg, $matches)) // an option
 | ||
| 
											14 years ago
										 | 			{
 | ||
| 
											14 years ago
										 | 				$name = $matches[1];
 | ||
|  | 				$value = isset($matches[3]) ? $matches[3] : true;
 | ||
|  | 				if (isset($options[$name])) {
 | ||
|  | 					if (!is_array($options[$name]))
 | ||
|  | 						$options[$name] = array($options[$name]);
 | ||
|  | 					$options[$name][] = $value;
 | ||
| 
											14 years ago
										 | 				} else
 | ||
| 
											14 years ago
										 | 					$options[$name] = $value;
 | ||
|  | 			} else if (isset($action))
 | ||
|  | 				$params[] = $arg;
 | ||
| 
											14 years ago
										 | 			else
 | ||
| 
											14 years ago
										 | 				$action = $arg;
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 		if (!isset($action))
 | ||
|  | 			$action = $this->defaultAction;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											14 years ago
										 | 		return array($action, $options, $params);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return string the command name.
 | ||
|  | 	 */
 | ||
|  | 	public function getName()
 | ||
|  | 	{
 | ||
|  | 		return $this->_name;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Provides the command description.
 | ||
|  | 	 * This method may be overridden to return the actual command description.
 | ||
|  | 	 * @return string the command description. Defaults to 'Usage: php entry-script.php command-name'.
 | ||
|  | 	 */
 | ||
|  | 	public function getHelp()
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		$help = 'Usage: ' . $this->getCommandRunner()->getScriptName() . ' ' . $this->getName();
 | ||
|  | 		$options = $this->getOptionHelp();
 | ||
|  | 		if (empty($options))
 | ||
| 
											14 years ago
										 | 			return $help;
 | ||
| 
											14 years ago
										 | 		if (count($options) === 1)
 | ||
|  | 			return $help . ' ' . $options[0];
 | ||
|  | 		$help .= " <action>\nActions:\n";
 | ||
|  | 		foreach ($options as $option)
 | ||
|  | 			$help .= '    ' . $option . "\n";
 | ||
| 
											14 years ago
										 | 		return $help;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Provides the command option help information.
 | ||
|  | 	 * The default implementation will return all available actions together with their
 | ||
|  | 	 * corresponding option information.
 | ||
|  | 	 * @return array the command option help information. Each array element describes
 | ||
|  | 	 * the help information for a single action.
 | ||
|  | 	 */
 | ||
|  | 	public function getOptionHelp()
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		$options = array();
 | ||
|  | 		$class = new \ReflectionClass(get_class($this));
 | ||
|  | 		foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
 | ||
|  | 			$name = $method->getName();
 | ||
|  | 			if (!strncasecmp($name, 'action', 6) && strlen($name) > 6) {
 | ||
|  | 				$name = substr($name, 6);
 | ||
|  | 				$name[0] = strtolower($name[0]);
 | ||
|  | 				$help = $name;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											14 years ago
										 | 				foreach ($method->getParameters() as $param) {
 | ||
|  | 					$optional = $param->isDefaultValueAvailable();
 | ||
|  | 					$defaultValue = $optional ? $param->getDefaultValue() : null;
 | ||
|  | 					$name = $param->getName();
 | ||
|  | 					if ($optional)
 | ||
|  | 						$help .= " [--$name=$defaultValue]";
 | ||
| 
											14 years ago
										 | 					else
 | ||
| 
											14 years ago
										 | 						$help .= " --$name=value";
 | ||
| 
											14 years ago
										 | 				}
 | ||
| 
											14 years ago
										 | 				$options[] = $help;
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		return $options;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Displays a usage error.
 | ||
|  | 	 * This method will then terminate the execution of the current application.
 | ||
|  | 	 * @param string $message the error message
 | ||
|  | 	 */
 | ||
|  | 	public function usageError($message)
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		echo "Error: $message\n\n" . $this->getHelp() . "\n";
 | ||
| 
											14 years ago
										 | 		exit(1);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders a view file.
 | ||
|  | 	 * @param string $_viewFile_ view file path
 | ||
|  | 	 * @param array $_data_ optional data to be extracted as local view variables
 | ||
|  | 	 * @param boolean $_return_ whether to return the rendering result instead of displaying it
 | ||
|  | 	 * @return mixed the rendering result if required. Null otherwise.
 | ||
|  | 	 */
 | ||
| 
											14 years ago
										 | 	public function renderFile($_viewFile_, $_data_ = null, $_return_ = false)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											14 years ago
										 | 		if (is_array($_data_))
 | ||
|  | 			extract($_data_, EXTR_PREFIX_SAME, 'data');
 | ||
| 
											14 years ago
										 | 		else
 | ||
| 
											14 years ago
										 | 			$data = $_data_;
 | ||
|  | 		if ($_return_) {
 | ||
| 
											14 years ago
										 | 			ob_start();
 | ||
|  | 			ob_implicit_flush(false);
 | ||
|  | 			require($_viewFile_);
 | ||
|  | 			return ob_get_clean();
 | ||
| 
											14 years ago
										 | 		} else
 | ||
| 
											14 years ago
										 | 			require($_viewFile_);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Reads input via the readline PHP extension if that's available, or fgets() if readline is not installed.
 | ||
|  | 	 *
 | ||
|  | 	 * @param string $message to echo out before waiting for user input
 | ||
| 
											14 years ago
										 | 	 * @param string $default the default string to be returned when user does not write anything.
 | ||
|  | 	 * Defaults to null, means that default string is disabled.
 | ||
| 
											14 years ago
										 | 	 * @return mixed line read as a string, or false if input has been closed
 | ||
|  | 	 */
 | ||
| 
											14 years ago
										 | 	public function prompt($message, $default = null)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											14 years ago
										 | 		if ($default !== null) {
 | ||
| 
											14 years ago
										 | 			$message .= " [$default] ";
 | ||
|  | 		}
 | ||
|  | 		else {
 | ||
|  | 			$message .= ' ';
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											14 years ago
										 | 		if (extension_loaded('readline')) {
 | ||
| 
											14 years ago
										 | 			$input = readline($message);
 | ||
| 
											14 years ago
										 | 			if ($input) {
 | ||
| 
											14 years ago
										 | 				readline_add_history($input);
 | ||
|  | 			}
 | ||
|  | 		} else {
 | ||
|  | 			echo $message;
 | ||
|  | 			$input = fgets(STDIN);
 | ||
|  | 		}
 | ||
| 
											14 years ago
										 | 		if ($input === false) {
 | ||
| 
											14 years ago
										 | 			return false;
 | ||
|  | 		}
 | ||
|  | 		else {
 | ||
| 
											14 years ago
										 | 			$input = trim($input);
 | ||
| 
											14 years ago
										 | 			return ($input === '' && $default !== null) ? $default : $input;
 | ||
| 
											14 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Asks user to confirm by typing y or n.
 | ||
|  | 	 *
 | ||
|  | 	 * @param string $message to echo out before waiting for user input
 | ||
|  | 	 * @return bool if user confirmed
 | ||
|  | 	 */
 | ||
|  | 	public function confirm($message)
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		echo $message . ' [yes|no] ';
 | ||
|  | 		return !strncasecmp(trim(fgets(STDIN)), 'y', 1);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | }
 |