1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think\model\concern; |
14
|
|
|
|
15
|
|
|
use think\App; |
16
|
|
|
use think\Container; |
17
|
|
|
use think\exception\ModelEventException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* 模型事件处理 |
21
|
|
|
*/ |
|
|
|
|
22
|
|
|
trait ModelEvent |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* 模型事件观察 |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected static $observe = ['after_read', 'before_write', 'after_write', 'before_insert', 'after_insert', 'before_update', 'after_update', 'before_delete', 'after_delete', 'before_restore', 'after_restore']; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* 是否需要事件响应 |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $withEvent = true; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* 注册一个模型观察者 |
38
|
|
|
* |
39
|
|
|
* @param string $class |
|
|
|
|
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
protected static function observe(string $class): void |
43
|
|
|
{ |
44
|
|
|
foreach (static::$observe as $event) { |
45
|
|
|
$call = 'on' . App::parseName($event, 1, false); |
46
|
|
|
|
47
|
|
|
if (method_exists($class, $call)) { |
48
|
|
|
$instance = Container::getInstance()->invokeClass($class); |
49
|
|
|
|
50
|
|
|
Container::pull('event')->listen(static::class . '.' . $event, [$instance, $call]); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* 当前操作的事件响应 |
57
|
|
|
* @access protected |
58
|
|
|
* @param bool $event 是否需要事件响应 |
|
|
|
|
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function withEvent(bool $event) |
62
|
|
|
{ |
63
|
|
|
$this->withEvent = $event; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* 触发事件 |
69
|
|
|
* @access protected |
70
|
|
|
* @param string $event 事件名 |
|
|
|
|
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
protected function trigger(string $event): bool |
74
|
|
|
{ |
75
|
|
|
if (!$this->withEvent) { |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$call = 'on' . App::parseName($event, 1, false); |
80
|
|
|
|
81
|
|
|
if (method_exists($this, $call)) { |
82
|
|
|
$result = Container::getInstance()->invoke($call, [$this]); |
83
|
|
|
|
84
|
|
|
if (false === $result) { |
85
|
|
|
return false; |
86
|
|
|
} else { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
Container::pull('event')->trigger(static::class . '.' . $event, $this); |
93
|
|
|
return true; |
94
|
|
|
} catch (ModelEventException $e) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|