Completed
Pull Request — master (#13304)
by
unknown
21:22
created

Request::getParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5.667

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 9
cp 0.3333
cc 3
eloc 8
nc 3
nop 0
crap 5.667
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
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 <[email protected]>
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 5
    public function getParams()
31
    {
32 5
        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 5
        return $this->_params;
42
    }
43
44
    /**
45
     * Sets the command line arguments.
46
     * @param array $params the command line arguments
47
     */
48 5
    public function setParams($params)
49
    {
50 5
        $this->_params = $params;
51 5
    }
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
     */
57 5
    public function resolve()
58
    {
59 5
        $rawParams = $this->getParams();
60 5
        $endOfOptionsFound = false;
61 5
        if (isset($rawParams[0])) {
62 5
            $route = array_shift($rawParams);
63
64 5
            if ($route === '--') {
65 1
                $endOfOptionsFound = true;
66 1
                $route = array_shift($rawParams);
67 1
            }
68 5
        } else {
69
            $route = '';
70
        }
71
72 5
        $params = [];
73 5
        foreach ($rawParams as $param) {
74 4
            if ($endOfOptionsFound) {
75 2
                $params[] = $param;
76 4
            } elseif ($param === '--') {
77 1
                $endOfOptionsFound = true;
78 3
            } elseif (preg_match('/^--(\w+)(?:=(.*))?$/', $param, $matches)) {
79 2
                $name = $matches[1];
80 2
                if ($name !== Application::OPTION_APPCONFIG) {
81 2
                    $params[$name] = isset($matches[2]) ? $matches[2] : true;
82 2
                }
83 3
            } elseif (preg_match('/^-(\w+)(?:=(.*))?$/', $param, $matches)) {
84 2
                $name = $matches[1];
85 2
                if (is_numeric($name)) {
86 2
                    $params[] = $param;
87 2
                } else {
88 2
                    $params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
89
                }
90 2
            } else {
91 3
                $params[] = $param;
92
            }
93 5
        }
94
95 5
        return [$route, $params];
96
    }
97
}
98