|
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\controllers; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\console\Request; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Common controller behavior. |
|
18
|
|
|
*/ |
|
19
|
|
|
class CommonBehavior extends \yii\base\Behavior |
|
20
|
|
|
{ |
|
21
|
|
|
public function events() |
|
22
|
|
|
{ |
|
23
|
|
|
return [ |
|
24
|
|
|
CommonController::EVENT_BEFORE_ACTION => 'onBeforeAction', |
|
25
|
|
|
CommonController::EVENT_AFTER_ACTION => 'onAfterAction', |
|
26
|
|
|
]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function onBeforeAction($event) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->runRequests($event->sender->before); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function onAfterAction($event) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->runRequests($event->sender->after); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
View Code Duplication |
public function runRequests($requests) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
foreach ($this->normalizeTasks($requests) as $request => $enabled) { |
|
42
|
|
|
if ($enabled) { |
|
43
|
|
|
$response = $this->runRequest($request); |
|
44
|
|
|
if ($this->isNotOk($response)) { |
|
45
|
|
|
return $response; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
public function normalizeTasks($tasks) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
if (!$tasks) { |
|
54
|
|
|
return []; |
|
55
|
|
|
} elseif (!is_array($tasks)) { |
|
56
|
|
|
$tasks = [(string) $tasks => 1]; |
|
57
|
|
|
} |
|
58
|
|
|
$res = []; |
|
59
|
|
|
foreach ($tasks as $dep => $enabled) { |
|
60
|
|
|
$res[(string) (is_int($dep) ? $enabled : $dep)] = (bool) (is_int($dep) ? 1 : $enabled); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $res; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Is response NOT Ok. |
|
68
|
|
|
* @param Response|int $response |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public function isNotOk($response) |
|
72
|
|
|
{ |
|
73
|
|
|
return is_object($response) ? $response->exitStatus : $response; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Run request. |
|
78
|
|
|
* @param string|array $query |
|
79
|
|
|
* @return Response |
|
80
|
|
|
*/ |
|
81
|
|
|
public function runRequest($query) |
|
82
|
|
|
{ |
|
83
|
|
|
$request = Yii::createObject([ |
|
84
|
|
|
'class' => Request::class, |
|
85
|
|
|
'params' => is_array($query) ? $query : array_filter(explode(' ', $query)), |
|
86
|
|
|
]); |
|
87
|
|
|
|
|
88
|
|
|
return Yii::$app->handleRequest($request); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
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.