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