Completed
Push — cache-closure ( 7d9053...380edd )
by Dmitry
35:54
created

Request::resolve()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 9.0058

Importance

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