123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- /**
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- namespace yii\console;
- /**
- * The console Request represents the environment information for a console application.
- *
- * It is a wrapper for the PHP `$_SERVER` variable which holds information about the
- * currently running PHP script and the command line arguments given to it.
- *
- * @property array $params The command line arguments. It does not include the entry script name.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @since 2.0
- */
- class Request extends \yii\base\Request
- {
- private $_params;
- /**
- * Returns the command line arguments.
- * @return array the command line arguments. It does not include the entry script name.
- */
- public function getParams()
- {
- if ($this->_params === null) {
- if (isset($_SERVER['argv'])) {
- $this->_params = $_SERVER['argv'];
- array_shift($this->_params);
- } else {
- $this->_params = [];
- }
- }
- return $this->_params;
- }
- /**
- * Sets the command line arguments.
- * @param array $params the command line arguments
- */
- public function setParams($params)
- {
- $this->_params = $params;
- }
- /**
- * 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.
- * @throws Exception when parameter is wrong and can not be resolved
- */
- public function resolve()
- {
- $rawParams = $this->getParams();
- $endOfOptionsFound = false;
- if (isset($rawParams[0])) {
- $route = array_shift($rawParams);
- if ($route === '--') {
- $endOfOptionsFound = true;
- $route = array_shift($rawParams);
- }
- } else {
- $route = '';
- }
- $params = [];
- $prevOption = null;
- foreach ($rawParams as $param) {
- if ($endOfOptionsFound) {
- $params[] = $param;
- } elseif ($param === '--') {
- $endOfOptionsFound = true;
- } elseif (preg_match('/^--([\w-]+)(?:=(.*))?$/', $param, $matches)) {
- $name = $matches[1];
- if (is_numeric(substr($name, 0, 1))) {
- throw new Exception('Parameter "' . $name . '" is not valid');
- }
- if ($name !== Application::OPTION_APPCONFIG) {
- $params[$name] = isset($matches[2]) ? $matches[2] : true;
- $prevOption = &$params[$name];
- }
- } elseif (preg_match('/^-([\w-]+)(?:=(.*))?$/', $param, $matches)) {
- $name = $matches[1];
- if (is_numeric($name)) {
- $params[] = $param;
- } else {
- $params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
- $prevOption = &$params['_aliases'][$name];
- }
- } elseif ($prevOption === true) {
- // `--option value` syntax
- $prevOption = $param;
- } else {
- $params[] = $param;
- }
- }
- return [$route, $params];
- }
- }
|