Request.php 1.18 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7 8 9 10 11 12 13
 * @license http://www.yiiframework.com/license/
 */

namespace yii\console;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
14
class Request extends \yii\base\Request
Qiang Xue committed
15
{
Qiang Xue committed
16
	const ANONYMOUS_PARAMS = '-args';
Qiang Xue committed
17

Qiang Xue committed
18 19 20 21 22
	public function getRawParams()
	{
		return isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
	}

Qiang Xue committed
23 24 25 26
	/**
	 * Resolves the current request into a route and the associated parameters.
	 * @return array the first element is the route, and the second is the associated parameters.
	 */
Qiang Xue committed
27
	public function resolve()
Qiang Xue committed
28 29 30 31 32
	{
		$rawParams = $this->getRawParams();
		array_shift($rawParams);  // the 1st argument is the yiic script name

		if (isset($rawParams[0])) {
Qiang Xue committed
33
			$route = $rawParams[0];
Qiang Xue committed
34 35
			array_shift($rawParams);
		} else {
Qiang Xue committed
36
			$route = '';
Qiang Xue committed
37 38
		}

Qiang Xue committed
39
		$params = array(self::ANONYMOUS_PARAMS => array());
Qiang Xue committed
40 41 42
		foreach ($rawParams as $param) {
			if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
				$name = $matches[1];
Qiang Xue committed
43
				$params[$name] = isset($matches[3]) ? $matches[3] : true;
Qiang Xue committed
44
			} else {
Qiang Xue committed
45
				$params[self::ANONYMOUS_PARAMS][] = $param;
Qiang Xue committed
46 47
			}
		}
Qiang Xue committed
48 49

		return array($route, $params);
Qiang Xue committed
50 51
	}
}