-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskEventTrait.php
More file actions
90 lines (81 loc) · 2.07 KB
/
TaskEventTrait.php
File metadata and controls
90 lines (81 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace WebmanTech\CrontabTask\Traits;
use Closure;
use WebmanTech\CrontabTask\Helper\ConfigHelper;
trait TaskEventTrait
{
/**
* @deprecated
* @var null|Closure
*/
protected ?Closure $eventBeforeExec = null;
/**
* @deprecated
* @var null|Closure
*/
protected ?Closure $eventAfterExec = null;
protected bool $disableGlobalBeforeEvent = false;
protected bool $disableGlobalAfterEvent = false;
/**
* @var Closure[]
*/
protected array $eventsBeforeExec = [];
/**
* @var Closure[]
*/
protected array $eventsAfterExec = [];
/**
* @return void
*/
protected function initEvents()
{
// 旧版本单独事件的模式,暂时兼容
if ($this->eventBeforeExec === null) {
/** @phpstan-ignore-next-line */
$this->eventBeforeExec = $this->disableGlobalBeforeEvent ? null : ConfigHelper::get('app.event.before_exec');
}
if ($this->eventAfterExec === null) {
/** @phpstan-ignore-next-line */
$this->eventAfterExec = $this->disableGlobalAfterEvent ? null : ConfigHelper::get('app.event.after_exec');
}
// 支持多事件
if ($this->eventBeforeExec !== null) {
$this->addBeforeEvent($this->eventBeforeExec);
}
if ($this->eventAfterExec !== null) {
$this->addAfterEvent($this->eventAfterExec);
}
}
/**
* @return void
*/
protected function addBeforeEvent(Closure $closure)
{
$this->eventsBeforeExec[] = $closure;
}
/**
* @return void
*/
protected function addAfterEvent(Closure $closure)
{
$this->eventsAfterExec[] = $closure;
}
/**
* @return void
*/
protected function fireBeforeEvent()
{
foreach ($this->eventsBeforeExec as $event) {
$event($this);
}
}
/**
* @return void
*/
protected function fireAfterEvent()
{
foreach ($this->eventsAfterExec as $event) {
$event($this);
}
}
}