Completed
Push — master ( aa6d20...c2ee93 )
by Andrii
03:32
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
        /// XXX SHOCK! first time reads wrong file :(
54
        /// XXX Don't know why! need help!
55
        static::readVendorConfig($vendor, 'dotenv');
56
        $this->setExtraEnv(static::readVendorConfig($vendor, 'dotenv'));
57
        static::readVendorConfig($vendor, 'hidev');
58
        $this->setExtraConfig(static::readVendorConfig($vendor, 'hidev'));
59
    }
60
61
    public function readVendorConfig($vendor, $name)
62
    {
63
        return static::readConfig(ConfigPlugin::path($vendor, $name));
64
    }
65
66
    /**
67
     * Sets extra environment variables.
68
     * @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...
69
     */
70
    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...
71
    {
72
        foreach ($vars as $key => $value) {
73
            $_ENV[$key] = $value;
74
        }
75
    }
76
77
    /**
78
     * Implements extra configuration.
79
     * @param array $config
80
     */
81 2
    public function setExtraConfig($config)
82
    {
83 2
        $this->_config = $config = ArrayHelper::merge($config, $this->_config);
84 2
        $backup = $this->get('config')->getItems();
85 2
        $this->clear('config');
86
87 2
        foreach (['params', 'aliases', 'modules', 'components'] as $key) {
88 2
            if (isset($config[$key])) {
89 2
                $this->{'setExtra' . ucfirst($key)}($config[$key]);
90
            }
91
        }
92
93 2
        $this->get('config')->mergeItems($backup);
94 2
    }
95
96
    /**
97
     * Implements extra params.
98
     * @param array $params
99
     */
100 2
    public function setExtraParams($params)
101
    {
102 2
        if (is_array($params) && !empty($params)) {
103
            $this->params = ArrayHelper::merge($this->params, $params);
104
        }
105 2
    }
106
107
    /**
108
     * Implements extra aliases.
109
     * @param array $aliases
110
     */
111
    public function setExtraAliases($aliases)
112
    {
113
        if (is_array($aliases) && !empty($aliases)) {
114
            $this->setAliases($aliases);
115
        }
116
    }
117
118
    /**
119
     * Implements extra modules.
120
     * @param array $modules
121
     */
122
    public function setExtraModules($modules)
123
    {
124
        if (is_array($modules) && !empty($modules)) {
125
            $this->setModules($modules);
126
        }
127
    }
128
129
    /**
130
     * Implements extra components.
131
     * Does NOT touch already instantiated components.
132
     * @param array $components
133
     */
134 2
    public function setExtraComponents($components)
135
    {
136 2
        if (is_array($components) && !empty($components)) {
137 2
            foreach ($components as $id => $component) {
138 2
                if ($this->has($id, true)) {
139
                    unset($components[$id]);
140
                }
141
            }
142 2
            $this->setComponents($components);
143
        }
144 2
    }
145
146 5
    public function createControllerByID($id)
147
    {
148
        /// skip start for init goal
149 5
        if ($this->_first) {
150 5
            $this->_first = false;
151 5
            static $skips = ['init' => 1, 'clone' => 1, '--version' => 1];
152 5
            if (!isset($skips[$id])) {
153 2
                $this->runRequest('start');
154
            }
155
        }
156
157 5
        if ($this->get('config')->hasGoal($id)) {
158 5
            return $this->get('config')->get($id);
159
        }
160
161 4
        $controller = parent::createControllerByID($id);
162 4
        $this->get('config')->set($id, $controller);
163
164 4
        return $controller;
165
    }
166
167
    /**
168
     * Run request.
169
     * @param string|array $query
170
     * @return Response
171
     */
172 5
    public function runRequest($query)
173
    {
174 5
        $request = Yii::createObject([
175 5
            'class'  => 'hidev\base\Request',
176 5
            'params' => is_array($query) ? $query : array_filter(explode(' ', $query)),
177
        ]);
178
179 5
        return $this->handleRequest($request);
180
    }
181
}
182