|
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 |
|
|
|
|
|
|
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( |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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
|
|
|
if ($this->_first) { |
|
121
|
|
|
$this->_first = false; |
|
122
|
|
|
if ($id && !$this->get('config')->hasGoal($id)) { |
|
123
|
|
|
$this->runRequest('start'); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if ($this->get('config')->hasGoal($id)) { |
|
128
|
|
|
return $this->get('config')->get($id); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$controller = parent::createControllerByID($id); |
|
132
|
|
|
$this->get('config')->set($id, $controller); |
|
133
|
|
|
|
|
134
|
|
|
return $controller; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function runRequest($string) |
|
138
|
|
|
{ |
|
139
|
|
|
$request = Yii::createObject([ |
|
140
|
|
|
'class' => 'hidev\base\Request', |
|
141
|
|
|
'params' => array_filter(explode(' ', $string)), |
|
142
|
|
|
]); |
|
143
|
|
|
|
|
144
|
|
|
return $this->handleRequest($request); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function runAction($route, $params = []) |
|
148
|
|
|
{ |
|
149
|
|
|
try { |
|
150
|
|
|
return Module::runAction($route, $params); |
|
151
|
|
|
} catch (InvalidRouteException $e) { |
|
|
|
|
|
|
152
|
|
|
throw new Exception("Unknown command \"$route\".", 0, $e); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.