Completed
Push — master ( 5f528b...a6b26f )
by Andrii
03:24
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1.0156
1
<?php
2
3
/*
4
 * Automation tool mixed with code generator for easier continuous development
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\base;
13
14
use Exception;
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
30
    protected $_first = true;
31
32 5
    public function __construct($config = [])
33
    {
34 5
        $this->_config = $config;
35
        parent::__construct($config);
36 5
    }
37
38
    /**
39
     * Creates application with given config and runs it.
40
     * @param array $config
41
     * @return int exit code
42
     */
43
    public static function main(array $config)
44
    {
45
        try {
46
            $app = static::create($config);
47
            $exitCode = $app->run();
48
        } catch (Exception $e) {
49
            /*if ($e instanceof InvalidParamException || $e instanceof ConsoleException) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
                Yii::error($e->getMessage());
51
                $exitCode = 1;
52
            } else {
53
                throw $e;
54
            }*/
55
                throw $e;
56
        }
57
58
        return $exitCode;
59
    }
60
61 5
    public static function create(array $config)
62
    {
63
        Yii::setLogger(Yii::createObject('hidev\base\Logger'));
64
        $config = ArrayHelper::merge(
65 5
            static::readExtraVendor($config['vendorPath']),
66
            $config
67
        );
68
69
        return new static($config);
70 5
    }
71
72
    public static function readExtraVendor($dir)
73
    {
74
        return static::readExtraConfig($dir . '/hiqdev/config/hidev.php');
75
    }
76
77
    public static function readExtraConfig($path)
78
    {
79
        $path = Yii::getAlias($path);
80
81
        return file_exists($path) ? require $path : [];
82
    }
83
84 2
    public function loadExtraConfig($path)
85
    {
86
        $this->setExtraConfig(static::readExtraConfig($path));
87 2
    }
88
89
    /**
90
     * Load extra config files.
91
     * @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...
92
     */
93
    public function loadExtraVendor($dir)
94
    {
95
        $this->setExtraConfig(static::readExtraVendor($dir));
96
    }
97
98
    /**
99
     * Implements extra configuration.
100
     * @param array $config
101
     */
102 2
    public function setExtraConfig($config)
103
    {
104
        $this->_config = $config = ArrayHelper::merge($config, $this->_config);
105
        $backup = $this->get('config')->getItems();
106
        $this->clear('config');
107
108 2
        foreach (['params', 'aliases', 'modules', 'components'] as $key) {
109
            if (isset($config[$key])) {
110
                $this->{'setExtra' . ucfirst($key)}($config[$key]);
111
            }
112
        }
113
114
        $this->get('config')->mergeItems($backup);
115 2
    }
116
117
    /**
118
     * Implements extra params.
119
     * @param array $params
120
     */
121
    public function setExtraParams($params)
122
    {
123
        if (is_array($params) && !empty($params)) {
124
            $this->params = ArrayHelper::merge($this->params, $params);
125
        }
126
    }
127
128
    /**
129
     * Implements extra aliases.
130
     * @param array $aliases
131
     */
132 2
    public function setExtraAliases($aliases)
133
    {
134
        if (is_array($aliases) && !empty($aliases)) {
135
            $this->setAliases($aliases);
136
        }
137 2
    }
138
139
    /**
140
     * Implements extra modules.
141
     * @param array $modules
142
     */
143
    public function setExtraModules($modules)
144
    {
145
        if (is_array($modules) && !empty($modules)) {
146
            $this->setModules($modules);
147
        }
148
    }
149
150
    /**
151
     * Implements extra components.
152
     * Does NOT touch already instantiated components.
153
     * @param array $components
154
     */
155 2
    public function setExtraComponents($components)
156
    {
157
        if (is_array($components) && !empty($components)) {
158
            foreach ($components as $id => $component) {
159
                if ($this->has($id, true)) {
160 2
                    unset($components[$id]);
161
                }
162
            }
163
            $this->setComponents($components);
164
        }
165 2
    }
166
167 5
    public function createControllerByID($id)
168
    {
169
        /// skip start for init goal
170 1
        if ($this->_first) {
171 5
            $this->_first = false;
172 5
            static $skips = ['init' => 1, 'clone' => 1, '--version' => 1];
173 5
            if (!isset($skips[$id])) {
174
                $this->runRequest('start');
175
            }
176
        }
177
178
        if ($this->get('config')->hasGoal($id)) {
179
            return $this->get('config')->get($id);
180
        }
181
182
        $controller = parent::createControllerByID($id);
183
        $this->get('config')->set($id, $controller);
184
185 3
        return $controller;
186
    }
187
188
    /**
189
     * Run request.
190
     * @param string|array $query
191
     * @return Response
192
     */
193 3
    public function runRequest($query)
194
    {
195
        $request = Yii::createObject([
196 3
            'class'  => 'hidev\base\Request',
197
            'params' => is_array($query) ? $query : array_filter(explode(' ', $query)),
198
        ]);
199
200
        return $this->handleRequest($request);
201
    }
202
}
203