Completed
Push — master ( 32412c...4a6135 )
by Andrii
03:09
created

Application::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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