Completed
Push — master ( 8e919b...76847d )
by Andrii
03:06
created

Application::setExtraParams()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 5.667

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 3
cp 0.3333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 1
crap 5.667
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development.
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\base;
12
13
use Exception;
14
use hidev\helpers\ConfigPlugin;
15
use Yii;
16
use yii\base\InvalidParamException;
17
use yii\base\ViewContextInterface;
18
use yii\console\Exception as ConsoleException;
19
use yii\helpers\ArrayHelper;
20
21
/**
22
 * The Application.
23
 */
24
class Application extends \yii\console\Application implements ViewContextInterface
25
{
26
    protected $_viewPath;
27
28
    protected $_config;
29 5
30
    protected $_first = true;
31 5
32 5
    public function __construct($config = [])
33 5
    {
34
        $this->_config = $config;
35 2
        parent::__construct($config);
36
    }
37 2
38 2
    public function loadExtraConfig($path)
39
    {
40 2
        $this->setExtraConfig(static::readConfig($path));
41
    }
42 2
43
    public static function readConfig($path)
44 2
    {
45
        $path = Yii::getAlias($path);
46
47
        return file_exists($path) ? require $path : [];
48
    }
49
50
    /**
51
     * Load extra config files.
52
     * @param string $vendor path to vendor dir
53
     */
54
    public function loadExtraVendor($vendor)
55
    {
56
        $this->setExtraEnv(static::readVendorConfig($vendor, 'dotenv'));
57
        $this->setExtraConfig(static::readVendorConfig($vendor, 'hidev'));
58
    }
59
60
    public function readVendorConfig($vendor, $name)
61
    {
62
        return static::readConfig(ConfigPlugin::path($vendor, $name));
63
    }
64
65
    /**
66
     * Sets extra environment variables.
67
     * @param array $config
0 ignored issues
show
Bug introduced by
There is no parameter named $config. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
68
     */
69
    public function setExtraEnv($vars)
0 ignored issues
show
Coding Style introduced by
setExtraEnv uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
70
    {
71
        foreach ($vars as $key => $value) {
72
            $_ENV[$key] = $value;
73
        }
74
    }
75
76
    /**
77 2
     * Implements extra configuration.
78
     * @param array $config
79 2
     */
80 2
    public function setExtraConfig($config)
81 2
    {
82
        $this->_config = $config = ArrayHelper::merge($config, $this->_config);
83 2
        $backup = $this->get('config')->getItems();
84 2
        $this->clear('config');
85 2
86 2
        foreach (['params', 'aliases', 'modules', 'components'] as $key) {
87 2
            if (isset($config[$key])) {
88
                $this->{'setExtra' . ucfirst($key)}($config[$key]);
89 2
            }
90 2
        }
91
92
        $this->get('config')->mergeItems($backup);
93
    }
94
95
    /**
96 2
     * Implements extra params.
97
     * @param array $params
98 2
     */
99
    public function setExtraParams($params)
100
    {
101 2
        if (is_array($params) && !empty($params)) {
102
            $this->params = ArrayHelper::merge($this->params, $params);
103
        }
104
    }
105
106
    /**
107
     * Implements extra aliases.
108
     * @param array $aliases
109
     */
110
    public function setExtraAliases($aliases)
111
    {
112
        if (is_array($aliases) && !empty($aliases)) {
113
            $this->setAliases($aliases);
114
        }
115
    }
116
117
    /**
118
     * Implements extra modules.
119
     * @param array $modules
120
     */
121
    public function setExtraModules($modules)
122
    {
123
        if (is_array($modules) && !empty($modules)) {
124
            $this->setModules($modules);
125
        }
126
    }
127
128
    /**
129
     * Implements extra components.
130 2
     * Does NOT touch already instantiated components.
131
     * @param array $components
132 2
     */
133 2
    public function setExtraComponents($components)
134 2
    {
135
        if (is_array($components) && !empty($components)) {
136
            foreach ($components as $id => $component) {
137 2
                if ($this->has($id, true)) {
138 2
                    unset($components[$id]);
139 2
                }
140 2
            }
141
            $this->setComponents($components);
142 5
        }
143
    }
144
145 5
    public function createControllerByID($id)
146 5
    {
147 5
        /// skip start for init goal
148 5
        if ($this->_first) {
149 2
            $this->_first = false;
150 2
            static $skips = ['init' => 1, 'clone' => 1, '--version' => 1];
151 5
            if (!isset($skips[$id])) {
152
                $this->runRequest('start');
153 5
            }
154 5
        }
155
156
        if ($this->get('config')->hasGoal($id)) {
157 4
            return $this->get('config')->get($id);
158 4
        }
159
160 4
        $controller = parent::createControllerByID($id);
161
        $this->get('config')->set($id, $controller);
162
163
        return $controller;
164
    }
165
166
    /**
167
     * Run request.
168 5
     * @param string|array $query
169
     * @return Response
170 5
     */
171 5
    public function runRequest($query)
172 5
    {
173 5
        $request = Yii::createObject([
174
            'class'  => 'hidev\base\Request',
175 5
            'params' => is_array($query) ? $query : array_filter(explode(' ', $query)),
176
        ]);
177
178
        return $this->handleRequest($request);
179
    }
180
}
181