1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Automation tool mixed with code generator for easier continuous development |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hidev |
6
|
|
|
* @package hidev |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hidev\base; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\InvalidParamException; |
16
|
|
|
use yii\base\ViewContextInterface; |
17
|
|
|
use yii\console\Exception as ConsoleException; |
18
|
|
|
use yii\helpers\ArrayHelper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The Application. |
22
|
|
|
*/ |
23
|
|
|
class Application extends \yii\console\Application implements ViewContextInterface |
24
|
|
|
{ |
25
|
|
|
protected $_viewPath; |
26
|
|
|
|
27
|
|
|
protected $_config; |
28
|
|
|
|
29
|
|
|
protected $_first = true; |
30
|
|
|
|
31
|
5 |
|
public function __construct($config = []) |
32
|
|
|
{ |
33
|
5 |
|
$this->_config = $config; |
34
|
|
|
parent::__construct($config); |
35
|
5 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates application with given config and runs it. |
39
|
|
|
* @param array $config |
40
|
|
|
* @return int exit code |
41
|
|
|
*/ |
42
|
|
|
public static function main(array $config) |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
$app = static::create($config); |
46
|
|
|
$exitCode = $app->run(); |
47
|
|
|
} catch (Exception $e) { |
48
|
|
|
/*if ($e instanceof InvalidParamException || $e instanceof ConsoleException) { |
|
|
|
|
49
|
|
|
Yii::error($e->getMessage()); |
50
|
|
|
$exitCode = 1; |
51
|
|
|
} else { |
52
|
|
|
throw $e; |
53
|
|
|
}*/ |
54
|
|
|
throw $e; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $exitCode; |
58
|
|
|
} |
59
|
|
|
|
60
|
5 |
|
public static function create(array $config) |
61
|
|
|
{ |
62
|
|
|
Yii::setLogger(Yii::createObject('hidev\base\Logger')); |
63
|
|
|
$config = ArrayHelper::merge( |
64
|
5 |
|
static::readExtraVendor($config['vendorPath']), |
65
|
|
|
$config |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
return new static($config); |
69
|
5 |
|
} |
70
|
|
|
|
71
|
|
|
public static function readExtraVendor($dir) |
72
|
|
|
{ |
73
|
|
|
return static::readExtraConfig($dir . '/hiqdev/composer-config-plugin-output/hidev.php'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function readExtraConfig($path) |
77
|
|
|
{ |
78
|
|
|
$path = Yii::getAlias($path); |
79
|
|
|
|
80
|
|
|
return file_exists($path) ? require $path : []; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function loadExtraConfig($path) |
84
|
|
|
{ |
85
|
|
|
$this->setExtraConfig(static::readExtraConfig($path)); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Load extra config files. |
90
|
|
|
* @param array $config |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
public function loadExtraVendor($dir) |
93
|
|
|
{ |
94
|
|
|
$this->setExtraConfig(static::readExtraVendor($dir)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Implements extra configuration. |
99
|
|
|
* @param array $config |
100
|
|
|
*/ |
101
|
2 |
|
public function setExtraConfig($config) |
102
|
|
|
{ |
103
|
|
|
$this->_config = $config = ArrayHelper::merge($config, $this->_config); |
104
|
|
|
$backup = $this->get('config')->getItems(); |
105
|
|
|
$this->clear('config'); |
106
|
|
|
|
107
|
2 |
|
foreach (['params', 'aliases', 'modules', 'components'] as $key) { |
108
|
|
|
if (isset($config[$key])) { |
109
|
|
|
$this->{'setExtra' . ucfirst($key)}($config[$key]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->get('config')->mergeItems($backup); |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Implements extra params. |
118
|
|
|
* @param array $params |
119
|
|
|
*/ |
120
|
|
|
public function setExtraParams($params) |
121
|
|
|
{ |
122
|
|
|
if (is_array($params) && !empty($params)) { |
123
|
|
|
$this->params = ArrayHelper::merge($this->params, $params); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Implements extra aliases. |
129
|
|
|
* @param array $aliases |
130
|
|
|
*/ |
131
|
2 |
|
public function setExtraAliases($aliases) |
132
|
|
|
{ |
133
|
|
|
if (is_array($aliases) && !empty($aliases)) { |
134
|
|
|
$this->setAliases($aliases); |
135
|
|
|
} |
136
|
2 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Implements extra modules. |
140
|
|
|
* @param array $modules |
141
|
|
|
*/ |
142
|
|
|
public function setExtraModules($modules) |
143
|
|
|
{ |
144
|
|
|
if (is_array($modules) && !empty($modules)) { |
145
|
|
|
$this->setModules($modules); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Implements extra components. |
151
|
|
|
* Does NOT touch already instantiated components. |
152
|
|
|
* @param array $components |
153
|
|
|
*/ |
154
|
2 |
|
public function setExtraComponents($components) |
155
|
|
|
{ |
156
|
|
|
if (is_array($components) && !empty($components)) { |
157
|
|
|
foreach ($components as $id => $component) { |
158
|
|
|
if ($this->has($id, true)) { |
159
|
2 |
|
unset($components[$id]); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
$this->setComponents($components); |
163
|
|
|
} |
164
|
2 |
|
} |
165
|
|
|
|
166
|
5 |
|
public function createControllerByID($id) |
167
|
|
|
{ |
168
|
|
|
/// skip start for init goal |
169
|
1 |
|
if ($this->_first) { |
170
|
5 |
|
$this->_first = false; |
171
|
5 |
|
static $skips = ['init' => 1, 'clone' => 1, '--version' => 1]; |
172
|
5 |
|
if (!isset($skips[$id])) { |
173
|
|
|
$this->runRequest('start'); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($this->get('config')->hasGoal($id)) { |
178
|
|
|
return $this->get('config')->get($id); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$controller = parent::createControllerByID($id); |
182
|
|
|
$this->get('config')->set($id, $controller); |
183
|
|
|
|
184
|
3 |
|
return $controller; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Run request. |
189
|
|
|
* @param string|array $query |
190
|
|
|
* @return Response |
191
|
|
|
*/ |
192
|
3 |
|
public function runRequest($query) |
193
|
|
|
{ |
194
|
|
|
$request = Yii::createObject([ |
195
|
3 |
|
'class' => 'hidev\base\Request', |
196
|
|
|
'params' => is_array($query) ? $query : array_filter(explode(' ', $query)), |
197
|
|
|
]); |
198
|
|
|
|
199
|
|
|
return $this->handleRequest($request); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
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.