3
* @link https://www.yiiframework.com/
4
* @copyright Copyright (c) 2008 Yii Software LLC
5
* @license https://www.yiiframework.com/license/
11
* The console Request represents the environment information for a console application.
13
* It is a wrapper for the PHP `$_SERVER` variable which holds information about the
14
* currently running PHP script and the command line arguments given to it.
16
* @property array $params The command line arguments. It does not include the entry script name.
18
* @author Qiang Xue <qiang.xue@gmail.com>
21
class Request extends \yii\base\Request
27
* Returns the command line arguments.
28
* @return array the command line arguments. It does not include the entry script name.
30
public function getParams()
32
if ($this->_params === null) {
33
if (isset($_SERVER['argv'])) {
34
$this->_params = $_SERVER['argv'];
35
array_shift($this->_params);
41
return $this->_params;
45
* Sets the command line arguments.
46
* @param array $params the command line arguments
48
public function setParams($params)
50
$this->_params = $params;
54
* Resolves the current request into a route and the associated parameters.
55
* @return array the first element is the route, and the second is the associated parameters.
56
* @throws Exception when parameter is wrong and can not be resolved
58
public function resolve()
60
$rawParams = $this->getParams();
61
$endOfOptionsFound = false;
62
if (isset($rawParams[0])) {
63
$route = array_shift($rawParams);
65
if ($route === '--') {
66
$endOfOptionsFound = true;
67
$route = array_shift($rawParams);
75
foreach ($rawParams as $param) {
76
if ($endOfOptionsFound) {
78
} elseif ($param === '--') {
79
$endOfOptionsFound = true;
80
} elseif (preg_match('/^--([\w-]+)(?:=(.*))?$/', $param, $matches)) {
82
if (is_numeric(substr($name, 0, 1))) {
83
throw new Exception('Parameter "' . $name . '" is not valid');
86
if ($name !== Application::OPTION_APPCONFIG) {
87
$params[$name] = isset($matches[2]) ? $matches[2] : true;
88
$prevOption = &$params[$name];
90
} elseif (preg_match('/^-([\w-]+)(?:=(.*))?$/', $param, $matches)) {
92
if (is_numeric($name)) {
95
$params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
96
$prevOption = &$params['_aliases'][$name];
98
} elseif ($prevOption === true) {
99
// `--option value` syntax
100
$prevOption = $param;
106
return [$route, $params];