Request.php 1.43 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/**
 * Request class file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\console;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
16
class Request extends \yii\base\Request
Qiang Xue committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
{
	/**
	 * @var string the controller route specified by this request. If this is an empty string,
	 * it means the [[Application::defaultRoute|default route]] will be used.
	 * Note that the value of this property may not be a correct route. The console application
	 * will determine it is valid or not when it attempts to execute with this route.
	 */
	public $route;
	/**
	 * @var array
	 */
	public $params;

	public function init()
	{
		parent::init();
		$this->resolveRequest();
	}

	public function getRawParams()
	{
		return isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
	}

	protected function resolveRequest()
	{
		$rawParams = $this->getRawParams();
		array_shift($rawParams);  // the 1st argument is the yiic script name

		if (isset($rawParams[0])) {
			$this->route = $rawParams[0];
			array_shift($rawParams);
		} else {
			$this->route = '';
		}

		$this->params = array();
		foreach ($rawParams as $param) {
			if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
				$name = $matches[1];
				$this->params[$name] = isset($matches[3]) ? $matches[3] : true;
			} else {
59
				$this->params['args'][] = $param;
Qiang Xue committed
60 61 62 63
			}
		}
	}
}