Completed
Push — master ( 89f899...621fd3 )
by Andrii
12:26
created

Application   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 4 Features 2
Metric Value
wmc 30
c 9
b 4
f 2
lcom 1
cbo 3
dl 0
loc 161
ccs 0
cts 80
cp 0
rs 10

13 Methods

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