Completed
Push — master ( 684031...b4acf4 )
by Dmitry
11:26
created

Request   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 84.78%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 79
ccs 39
cts 46
cp 0.8478
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getParams() 0 13 3
A setParams() 0 4 1
C resolve() 0 42 12
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 = $rawParams[0];
63 5
            array_shift($rawParams);
64
65 5
            if ($route === '--') {
66 1
                $endOfOptionsFound = true;
67 1
                $route = $rawParams[0];
68 1
                array_shift($rawParams);
69 1
            }
70 5
        } else {
71
            $route = '';
72
        }
73
74 5
        $params = [];
75 5
        foreach ($rawParams as $param) {
76 4
            if ($endOfOptionsFound) {
77 2
                $params[] = $param;
78 4
            } elseif ($param === '--') {
79 1
                $endOfOptionsFound = true;
80 3
            } elseif (preg_match('/^--(\w+)(?:=(.*))?$/', $param, $matches)) {
81 2
                $name = $matches[1];
82 2
                if ($name !== Application::OPTION_APPCONFIG) {
83 2
                    $params[$name] = isset($matches[2]) ? $matches[2] : true;
84 2
                }
85 3
            } elseif (preg_match('/^-(\w+)(?:=(.*))?$/', $param, $matches)) {
86 2
                $name = $matches[1];
87 2
                if (is_numeric($name)) {
88 2
                    $params[] = $param;
89 2
                } else {
90 2
                    $params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
91
                }
92 2
            } else {
93 3
                $params[] = $param;
94
            }
95 5
        }
96
97 5
        return [$route, $params];
98
    }
99
}
100