Completed
Push — master ( 08a139...a5004e )
by Andrii
03:35
created

Application::runRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 9
rs 9.6666
ccs 0
cts 0
cp 0
cc 1
eloc 5
nc 1
nop 1
crap 2
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) 2014-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\Module;
18
use yii\base\ViewContextInterface;
19
use yii\console\Exception as ConsoleException;
20
use yii\helpers\ArrayHelper;
21
22
/**
23
 * The Application.
24
 */
25
class Application extends \yii\console\Application implements ViewContextInterface
26
{
27
    protected $_viewPath;
28
29
    protected $_config;
30
31
    protected $_first = true;
32
33
    public function __construct($config = [])
34
    {
35
        $this->_config = $config;
36
        parent::__construct($config);
37
    }
38
39
    /**
40
     * Creates application with given config and runs it.
41
     * @param array $config
42
     * @return int exit code
43
     */
44
    public static function main(array $config)
45
    {
46
        try {
47
            Yii::setLogger(Yii::createObject('hidev\base\Logger'));
48
            $config = ArrayHelper::merge(
49
                static::readExtraVendor($config['vendorPath']),
50
                $config
51
            );
52
            $exitCode = (new static($config))->run();
53
        } catch (Exception $e) {
54
            if ($e instanceof InvalidParamException || $e instanceof ConsoleException) {
55
                Yii::error($e->getMessage());
56
                $exitCode = 1;
57
            } else {
58
                throw $e;
59
            }
60
        }
61
62
        return $exitCode;
63
    }
64
65
    public static function readExtraVendor($dir)
66
    {
67
        return require $dir . '/yiisoft/yii2-extraconfig.php';
68
    }
69
70
    /**
71
     * Load extra config files.
72
     * @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...
73
     * @return void
74
     */
75
    public function loadExtraVendor($dir)
76
    {
77
        $this->setExtraConfig(static::readExtraVendor($dir));
78
    }
79
80
    /**
81
     * Implements extra configuration.
82
     * @param array $config
83
     * @return void
84
     */
85
    public function setExtraConfig($config)
86
    {
87
        $this->_config = $config = ArrayHelper::merge($config, $this->_config);
88
89
        if (!empty($config['aliases'])) {
90
            $this->setAliases($config['aliases']);
91
        }
92
        if (!empty($config['modules'])) {
93
            $this->setModules($config['modules']);
94
            /*$this->setModules(ArrayHelper::merge(
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
95
                $config['modules'],
96
                ArrayHelper::getItems($this->modules, array_keys($config['modules']))
97
            ));*/
98
        }
99
        if (!empty($config['components'])) {
100
            foreach ($config['components'] as $id => $component) {
101
                if ($this->has($id, true)) {
102
                    unset($config['components'][$id]);
103
                }
104
            }
105
            $this->setComponents($config['components']);
106
        }
107
    }
108
109
    /*public function getViewPath()
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
110
    {
111
        if ($this->_viewPath === null) {
112
            $this->_viewPath = Yii::getAlias('@app/views');
113
        }
114
115
        return $this->_viewPath;
116
    }*/
117
118
    public function createControllerByID($id)
119
    {
120
        /// skip start for init goal
121
        if ($this->_first) {
122
            $this->_first = false;
123
            if ($id != 'init') {
124
                $this->runRequest('start');
125
            }
126
        }
127
128
        if ($this->get('config')->hasGoal($id)) {
129
            return $this->get('config')->get($id);
130
        }
131
132
        $controller = parent::createControllerByID($id);
133
        $this->get('config')->set($id, $controller);
134
135
        return $controller;
136
    }
137
138
    public function runRequest($string)
139
    {
140
        $request = Yii::createObject([
141
            'class'  => 'hidev\base\Request',
142
            'params' => array_filter(explode(' ', $string)),
143
        ]);
144
145
        return $this->handleRequest($request);
146
    }
147
148
    public function runAction($route, $params = [])
149
    {
150
        try {
151
            return Module::runAction($route, $params);
152
        } catch (InvalidRouteException $e) {
0 ignored issues
show
Bug introduced by
The class hidev\base\InvalidRouteException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
153
            throw new Exception("Unknown command \"$route\".", 0, $e);
154
        }
155
    }
156
}
157