yii2

Форк
1
/
Request.php 
108 строк · 3.3 Кб
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7

8
namespace yii\console;
9

10
/**
11
 * The console Request represents the environment information for a console application.
12
 *
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.
15
 *
16
 * @property array $params The command line arguments. It does not include the entry script name.
17
 *
18
 * @author Qiang Xue <qiang.xue@gmail.com>
19
 * @since 2.0
20
 */
21
class Request extends \yii\base\Request
22
{
23
    private $_params;
24

25

26
    /**
27
     * Returns the command line arguments.
28
     * @return array the command line arguments. It does not include the entry script name.
29
     */
30
    public function getParams()
31
    {
32
        if ($this->_params === null) {
33
            if (isset($_SERVER['argv'])) {
34
                $this->_params = $_SERVER['argv'];
35
                array_shift($this->_params);
36
            } else {
37
                $this->_params = [];
38
            }
39
        }
40

41
        return $this->_params;
42
    }
43

44
    /**
45
     * Sets the command line arguments.
46
     * @param array $params the command line arguments
47
     */
48
    public function setParams($params)
49
    {
50
        $this->_params = $params;
51
    }
52

53
    /**
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
57
     */
58
    public function resolve()
59
    {
60
        $rawParams = $this->getParams();
61
        $endOfOptionsFound = false;
62
        if (isset($rawParams[0])) {
63
            $route = array_shift($rawParams);
64

65
            if ($route === '--') {
66
                $endOfOptionsFound = true;
67
                $route = array_shift($rawParams);
68
            }
69
        } else {
70
            $route = '';
71
        }
72

73
        $params = [];
74
        $prevOption = null;
75
        foreach ($rawParams as $param) {
76
            if ($endOfOptionsFound) {
77
                $params[] = $param;
78
            } elseif ($param === '--') {
79
                $endOfOptionsFound = true;
80
            } elseif (preg_match('/^--([\w-]+)(?:=(.*))?$/', $param, $matches)) {
81
                $name = $matches[1];
82
                if (is_numeric(substr($name, 0, 1))) {
83
                    throw new Exception('Parameter "' . $name . '" is not valid');
84
                }
85

86
                if ($name !== Application::OPTION_APPCONFIG) {
87
                    $params[$name] = isset($matches[2]) ? $matches[2] : true;
88
                    $prevOption = &$params[$name];
89
                }
90
            } elseif (preg_match('/^-([\w-]+)(?:=(.*))?$/', $param, $matches)) {
91
                $name = $matches[1];
92
                if (is_numeric($name)) {
93
                    $params[] = $param;
94
                } else {
95
                    $params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
96
                    $prevOption = &$params['_aliases'][$name];
97
                }
98
            } elseif ($prevOption === true) {
99
                // `--option value` syntax
100
                $prevOption = $param;
101
            } else {
102
                $params[] = $param;
103
            }
104
        }
105

106
        return [$route, $params];
107
    }
108
}
109

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.