|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\commands; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\InvalidConfigException; |
|
7
|
|
|
use yii\console\Controller; |
|
8
|
|
|
use yii\helpers\Console; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class BaseController |
|
12
|
|
|
* |
|
13
|
|
|
* @author Igor Chepurnoy <[email protected]> |
|
14
|
|
|
* |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
class BaseController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array list of disabled actions |
|
21
|
|
|
*/ |
|
22
|
|
|
public $disabledActions = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string the running command name |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $command; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritdoc |
|
31
|
|
|
*/ |
|
32
|
|
|
public function behaviors(): array |
|
33
|
|
|
{ |
|
34
|
|
|
return [ |
|
35
|
|
|
'cronLogger' => [ |
|
36
|
|
|
'class' => 'yii2mod\cron\behaviors\CronLoggerBehavior', |
|
37
|
|
|
'actions' => ['*'], |
|
38
|
|
|
], |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Before action event |
|
44
|
|
|
* |
|
45
|
|
|
* @param \yii\base\Action $action |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
public function beforeAction($action): bool |
|
50
|
|
|
{ |
|
51
|
|
|
$this->command = $action->controller->id . '/' . $action->id; |
|
52
|
|
|
|
|
53
|
|
|
if ($this->isDisabledAction($action->id)) { |
|
54
|
|
|
$this->stdout("Command '{$this->command}' is disabled.\n", Console::FG_RED); |
|
55
|
|
|
|
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!parent::beforeAction($action)) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->stdout("Running the command `{$this->command}` at the " . Yii::$app->formatter->asDatetime(time()) . "\n", Console::FG_GREEN); |
|
64
|
|
|
|
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* After action event |
|
70
|
|
|
* |
|
71
|
|
|
* @param \yii\base\Action $action |
|
72
|
|
|
* @param mixed $result |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
public function afterAction($action, $result) |
|
77
|
|
|
{ |
|
78
|
|
|
$result = parent::afterAction($action, $result); |
|
79
|
|
|
|
|
80
|
|
|
$this->stdout("Command `{$this->command}` finished at the " . Yii::$app->formatter->asDatetime(time()) . "\n", Console::FG_GREEN); |
|
81
|
|
|
|
|
82
|
|
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Check whether the current action is disabled |
|
87
|
|
|
* |
|
88
|
|
|
* @param $id string action id |
|
89
|
|
|
* |
|
90
|
|
|
* @throws InvalidConfigException |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function isDisabledAction($id): bool |
|
95
|
|
|
{ |
|
96
|
|
|
if (!is_array($this->disabledActions)) { |
|
97
|
|
|
throw new InvalidConfigException('The "disabledActions" property must be an array.'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (in_array('*', $this->disabledActions) || in_array($id, $this->disabledActions)) { |
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|