Request.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\console;
  8. /**
  9. * The console Request represents the environment information for a console application.
  10. *
  11. * It is a wrapper for the PHP `$_SERVER` variable which holds information about the
  12. * currently running PHP script and the command line arguments given to it.
  13. *
  14. * @property array $params The command line arguments. It does not include the entry script name.
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. class Request extends \yii\base\Request
  20. {
  21. private $_params;
  22. /**
  23. * Returns the command line arguments.
  24. * @return array the command line arguments. It does not include the entry script name.
  25. */
  26. public function getParams()
  27. {
  28. if ($this->_params === null) {
  29. if (isset($_SERVER['argv'])) {
  30. $this->_params = $_SERVER['argv'];
  31. array_shift($this->_params);
  32. } else {
  33. $this->_params = [];
  34. }
  35. }
  36. return $this->_params;
  37. }
  38. /**
  39. * Sets the command line arguments.
  40. * @param array $params the command line arguments
  41. */
  42. public function setParams($params)
  43. {
  44. $this->_params = $params;
  45. }
  46. /**
  47. * Resolves the current request into a route and the associated parameters.
  48. * @return array the first element is the route, and the second is the associated parameters.
  49. * @throws Exception when parameter is wrong and can not be resolved
  50. */
  51. public function resolve()
  52. {
  53. $rawParams = $this->getParams();
  54. $endOfOptionsFound = false;
  55. if (isset($rawParams[0])) {
  56. $route = array_shift($rawParams);
  57. if ($route === '--') {
  58. $endOfOptionsFound = true;
  59. $route = array_shift($rawParams);
  60. }
  61. } else {
  62. $route = '';
  63. }
  64. $params = [];
  65. $prevOption = null;
  66. foreach ($rawParams as $param) {
  67. if ($endOfOptionsFound) {
  68. $params[] = $param;
  69. } elseif ($param === '--') {
  70. $endOfOptionsFound = true;
  71. } elseif (preg_match('/^--([\w-]+)(?:=(.*))?$/', $param, $matches)) {
  72. $name = $matches[1];
  73. if (is_numeric(substr($name, 0, 1))) {
  74. throw new Exception('Parameter "' . $name . '" is not valid');
  75. }
  76. if ($name !== Application::OPTION_APPCONFIG) {
  77. $params[$name] = isset($matches[2]) ? $matches[2] : true;
  78. $prevOption = &$params[$name];
  79. }
  80. } elseif (preg_match('/^-([\w-]+)(?:=(.*))?$/', $param, $matches)) {
  81. $name = $matches[1];
  82. if (is_numeric($name)) {
  83. $params[] = $param;
  84. } else {
  85. $params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
  86. $prevOption = &$params['_aliases'][$name];
  87. }
  88. } elseif ($prevOption === true) {
  89. // `--option value` syntax
  90. $prevOption = $param;
  91. } else {
  92. $params[] = $param;
  93. }
  94. }
  95. return [$route, $params];
  96. }
  97. }