Passed
Push — 6.0 ( e525de...48766b )
by liu
02:47
created

ModelEvent   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A trigger() 0 23 5
A withEvent() 0 4 1
A observe() 0 9 3
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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  是否需要事件响应
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
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  事件名
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
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