Completed
Push — master ( 9777ec...f588f4 )
by Andrii
13:07
created

Application::readExtraConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 6
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) {
54
                Yii::error($e->getMessage());
55
                $exitCode = 1;
56
            } else {
57
                throw $e;
58
            }
59
        }
60
61
        return $exitCode;
62
    }
63
64
    public static function readExtraVendor($dir)
65
    {
66
       return static::readExtraConfig($dir . '/hiqdev/extensions-config.php');
67
    }
68
69
    public static function readExtraConfig($path)
70
    {
71
        return file_exists($path) ? require $path : [];
72
    }
73
74
    public function loadExtraConfig($path)
75
    {
76
        $this->setExtraConfig(static::readExtraConfig($path));
77
    }
78
79
    /**
80
     * Load extra config files.
81
     * @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...
82
     * @return void
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
     * @return void
93
     */
94
    public function setExtraConfig($config)
95
    {
96
        $this->_config = $config = ArrayHelper::merge($config, $this->_config);
97
98
        if (!empty($config['aliases'])) {
99
            $this->setAliases($config['aliases']);
100
        }
101
        if (!empty($config['modules'])) {
102
            $this->setModules($config['modules']);
103
        }
104
        if (!empty($config['components'])) {
105
            foreach ($config['components'] as $id => $component) {
106
                if ($this->has($id, true)) {
107
                    unset($config['components'][$id]);
108
                }
109
            }
110
            $this->setComponents($config['components']);
111
        }
112
    }
113
114
    public function createControllerByID($id)
115
    {
116
        /// skip start for init goal
117
        if ($this->_first) {
118
            $this->_first = false;
119
            static $skips = ['init' => 1, 'clone' => 1, '--version' => 1];
120
            if (!$skips[$id]) {
121
                $this->runRequest('start');
122
            }
123
        }
124
125
        if ($this->get('config')->hasGoal($id)) {
126
            return $this->get('config')->get($id);
127
        }
128
129
        $controller = parent::createControllerByID($id);
130
        $this->get('config')->set($id, $controller);
131
132
        return $controller;
133
    }
134
135
    public function runRequest($string)
136
    {
137
        $request = Yii::createObject([
138
            'class'  => 'hidev\base\Request',
139
            'params' => array_filter(explode(' ', $string)),
140
        ]);
141
142
        return $this->handleRequest($request);
143
    }
144
}
145