Completed
Push — master ( 502445...002137 )
by Andrii
17:02
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 hidev\helpers\ConfigPlugin;
14
use Yii;
15
use yii\base\ViewContextInterface;
16
use yii\helpers\ArrayHelper;
17
18
/**
19
 * The Application.
20
 */
21
class Application extends \yii\console\Application implements ViewContextInterface
22
{
23
    protected $_viewPath;
24
25
    protected $_config;
26
27
    protected $_first = true;
28
29 5
    public function __construct($config = [])
30
    {
31 5
        $this->_config = $config;
32 5
        parent::__construct($config);
33 5
    }
34
35 2
    public function loadExtraConfig($path)
36
    {
37 2
        $this->setExtraConfig(static::readConfig($path));
38 2
    }
39
40 2
    public static function readConfig($path)
41
    {
42 2
        $path = Yii::getAlias($path);
43
44 2
        return file_exists($path) ? require $path : [];
45
    }
46
47
    /**
48
     * Load extra config files.
49
     * @param string $vendor path to vendor dir
50
     */
51
    public function loadExtraVendor($vendor)
52
    {
53
        $this->setExtraEnv(static::readVendorConfig($vendor, 'dotenv'));
54
        $this->setExtraConfig(static::readVendorConfig($vendor, 'hidev'));
55
    }
56
57
    public function readVendorConfig($vendor, $name)
58
    {
59
        return static::readConfig(ConfigPlugin::path($vendor, $name));
60
    }
61
62
    /**
63
     * Sets extra environment variables.
64
     * @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...
65
     */
66
    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...
67
    {
68
        foreach ($vars as $key => $value) {
69
            $_ENV[$key] = $value;
70
        }
71
    }
72
73
    /**
74
     * Implements extra configuration.
75
     * @param array $config
76
     */
77 2
    public function setExtraConfig($config)
78
    {
79 2
        $this->_config = $config = ArrayHelper::merge($config, $this->_config);
80 2
        $backup = $this->get('config')->getItems();
81 2
        $this->clear('config');
82
83 2
        foreach (['params', 'aliases', 'modules', 'components', 'container'] as $key) {
84 2
            if (isset($config[$key])) {
85 2
                $this->{'setExtra' . ucfirst($key)}($config[$key]);
86
            }
87
        }
88
89 2
        $this->get('config')->mergeItems($backup);
90 2
    }
91
92
    /**
93
     * Implements extra params.
94
     * @param array $params
95
     */
96 2
    public function setExtraParams($params)
97
    {
98 2
        if (is_array($params) && !empty($params)) {
99
            $this->params = ArrayHelper::merge($this->params, $params);
100
        }
101 2
    }
102
103
    /**
104
     * Implements extra aliases.
105
     * @param array $aliases
106
     */
107
    public function setExtraAliases($aliases)
108
    {
109
        if (is_array($aliases) && !empty($aliases)) {
110
            $this->setAliases($aliases);
111
        }
112
    }
113
114
    /**
115
     * Implements extra modules.
116
     * @param array $modules
117
     */
118
    public function setExtraModules($modules)
119
    {
120
        if (is_array($modules) && !empty($modules)) {
121
            $this->setModules($modules);
122
        }
123
    }
124
125
    /**
126
     * Implements extra components.
127
     * Does NOT touch already instantiated components.
128
     * @param array $components
129
     */
130 2
    public function setExtraComponents($components)
131
    {
132 2
        if (is_array($components) && !empty($components)) {
133 2
            foreach ($components as $id => $component) {
134 2
                if ($this->has($id, true)) {
135
                    unset($components[$id]);
136
                }
137
            }
138 2
            $this->setComponents($components);
139
        }
140 2
    }
141
142
    /**
143
     * Implements extra container config.
144
     * @param array $container config
145
     */
146 2
    public function setExtraContainer($container)
147
    {
148 2
        $this->setContainer($container);
149 2
    }
150
151 5
    public function createControllerByID($id)
152
    {
153
        /// skip start for init goal
154 5
        if ($this->_first) {
155 5
            $this->_first = false;
156 5
            static $skips = ['init' => 1, 'clone' => 1, '--version' => 1];
157 5
            if (!isset($skips[$id])) {
158 2
                $this->runRequest('start');
159
            }
160
        }
161
162 5
        if ($this->get('config')->hasGoal($id)) {
163 5
            return $this->get('config')->get($id);
164
        }
165
166 4
        $controller = parent::createControllerByID($id);
167 4
        $this->get('config')->set($id, $controller);
168
169 4
        return $controller;
170
    }
171
172
    /**
173
     * Run request.
174
     * @param string|array $query
175
     * @return Response
176
     */
177 5
    public function runRequest($query)
178
    {
179 5
        $request = Yii::createObject([
180 5
            'class'  => 'hidev\base\Request',
181 5
            'params' => is_array($query) ? $query : array_filter(explode(' ', $query)),
182
        ]);
183
184 5
        return $this->handleRequest($request);
185
    }
186
}
187