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\controllers; |
13
|
|
|
|
14
|
|
|
use hidev\helpers\Helper; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\console\Response; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Abstract controller. |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractController extends \hidev\base\Controller |
22
|
|
|
{ |
23
|
|
|
protected $_before = []; |
24
|
|
|
protected $_after = []; |
25
|
|
|
protected $_make = ['load', 'save']; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array list of performed actions |
29
|
|
|
*/ |
30
|
|
|
protected $_done = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function options($actionId) |
36
|
|
|
{ |
37
|
|
|
return array_merge(parent::options($actionId), array_keys(Helper::getPublicVars(get_called_class()))); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function perform() |
41
|
|
|
{ |
42
|
|
|
return $this->runActions(['before', 'make', 'after']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function actionBefore() |
46
|
|
|
{ |
47
|
|
|
return $this->runRequests($this->getBefore()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return int|Response exit code |
52
|
|
|
*/ |
53
|
|
|
public function actionMake() |
54
|
|
|
{ |
55
|
|
|
return $this->runActions($this->getMake()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function actionAfter() |
59
|
|
|
{ |
60
|
|
|
return $this->runRequests($this->getAfter()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function actionLoad() |
64
|
|
|
{ |
65
|
|
|
Yii::trace("Loading nothing for '$this->id'"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function actionSave() |
69
|
|
|
{ |
70
|
|
|
Yii::trace("Saving nothing for '$this->id'"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setBefore($requests) |
74
|
|
|
{ |
75
|
|
|
$this->_before = array_merge($this->getBefore(), $this->normalizeTasks($requests)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getBefore() |
79
|
|
|
{ |
80
|
|
|
return $this->_before; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function setMake($requests) |
84
|
|
|
{ |
85
|
|
|
$this->_make = array_merge($this->getMake(), $this->normalizeTasks($requests)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getMake() |
89
|
|
|
{ |
90
|
|
|
return $this->_make; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function setAfter($requests) |
94
|
|
|
{ |
95
|
|
|
$this->_after = array_merge($this->getAfter(), $this->normalizeTasks($requests)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getAfter() |
99
|
|
|
{ |
100
|
|
|
return $this->_after; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function normalizeTasks($tasks) |
104
|
|
|
{ |
105
|
|
|
if (!$tasks) { |
106
|
|
|
return []; |
107
|
|
|
} elseif (!is_array($tasks)) { |
108
|
|
|
$tasks = [(string) $tasks => 1]; |
109
|
|
|
} |
110
|
|
|
$res = []; |
111
|
|
|
foreach ($tasks as $dep => $enabled) { |
112
|
|
|
$res[(string) (is_int($dep) ? $enabled : $dep)] = (bool) (is_int($dep) ? 1 : $enabled); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $res; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Runs array of requests. Stops on failure and returns exit code. |
120
|
|
|
* @param null|string|array $requests |
121
|
|
|
* @return int|Response exit code |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function runRequests($requests) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
foreach ($this->normalizeTasks($requests) as $request => $enabled) { |
126
|
|
|
if ($enabled) { |
127
|
|
|
$res = $this->runRequest($request); |
128
|
|
|
if (static::isNotOk($res)) { |
129
|
|
|
return $res; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return 0; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function runRequest($request) |
138
|
|
|
{ |
139
|
|
|
return $request === null ? null : $this->module->runRequest($request); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Is response NOT Ok. |
144
|
|
|
* @param Response|int $res |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public static function isNotOk($res) |
148
|
|
|
{ |
149
|
|
|
return is_object($res) ? $res->exitStatus : $res; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Is response Ok. |
154
|
|
|
* @param Response|int $res |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public static function isOk($res) |
158
|
|
|
{ |
159
|
|
|
return !static::isNotOk($res); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Runs list of actions. |
164
|
|
|
* TODO: think to redo with runRequests. |
165
|
|
|
* @param null|string|array $actions |
166
|
|
|
* @return int|Response exit code |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
public function runActions($actions) |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
foreach ($this->normalizeTasks($actions) as $action => $enabled) { |
171
|
|
|
if ($enabled) { |
172
|
|
|
$res = $this->runAction($action); |
173
|
|
|
if (static::isNotOk($res)) { |
174
|
|
|
return $res; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return 0; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function runAction($id, $params = []) |
183
|
|
|
{ |
184
|
|
|
if ($this->isDone($id)) { |
185
|
|
|
return; |
186
|
|
|
} |
187
|
|
|
$result = parent::runAction($id, $params); |
188
|
|
|
$this->markDone($id); |
189
|
|
|
|
190
|
|
|
return $result; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function isDone($action, $timestamp = null) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
if ($this->_done[$action]) { |
196
|
|
|
Yii::trace("Already done: '$this->id/$action'"); |
197
|
|
|
|
198
|
|
|
return true; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Mark action as already done. |
206
|
|
|
* |
207
|
|
|
* @param string $action action id |
208
|
|
|
* @param int $time microtime when action was done, false for action was not done |
209
|
|
|
*/ |
210
|
|
|
public function markDone($action, $time = null) |
211
|
|
|
{ |
212
|
|
|
$this->_done[$action] = ($time === null || $time === true) ? microtime(1) : $time; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Runs given binary with given arguments. Returns exit code. |
217
|
|
|
* @param string $name |
218
|
|
|
* @param array|string $args |
219
|
|
|
* @return int exit code |
220
|
|
|
*/ |
221
|
|
|
public static function passthru($name, $args = []) |
222
|
|
|
{ |
223
|
|
|
return static::takeGoal('binaries')->passthruBinary($name, $args); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Runs given binary with given arguments. Returns stdout array. |
228
|
|
|
* @param string $name |
229
|
|
|
* @param string $args |
230
|
|
|
* @param bool $returnExitCode, default false |
|
|
|
|
231
|
|
|
* @return array|int stdout or exitcode |
232
|
|
|
*/ |
233
|
|
|
public static function exec($name, $args = '', $returnExitCode = false) |
234
|
|
|
{ |
235
|
|
|
return static::takeGoal('binaries')->execBinary($name, $args, $returnExitCode); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function readline($prompt) |
239
|
|
|
{ |
240
|
|
|
return readline($prompt); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function readpassword($prompt) |
244
|
|
|
{ |
245
|
|
|
echo $prompt; |
246
|
|
|
system('stty -echo'); |
247
|
|
|
$password = rtrim(fgets(STDIN), PHP_EOL); |
248
|
|
|
system('stty echo'); |
249
|
|
|
echo "\n"; |
250
|
|
|
return $password; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public static function takeGoal($id) |
254
|
|
|
{ |
255
|
|
|
return Yii::$app->get('config')->getGoal($id); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public static function takeConfig() |
259
|
|
|
{ |
260
|
|
|
return Yii::$app->get('config'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public static function takeVendor() |
264
|
|
|
{ |
265
|
|
|
return static::takeGoal('vendor'); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function takePackage() |
269
|
|
|
{ |
270
|
|
|
return static::takeGoal('package'); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function takeVcs() |
274
|
|
|
{ |
275
|
|
|
return static::takeConfig()->getVcs(); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.