Completed
Push — 6.0 ( be4b12...d71786 )
by liu
06:57
created

ModelEvent::withEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
     * 是否需要事件响应
27
     * @var bool
28
     */
29
    protected $withEvent = true;
30
31
    /**
32
     * 当前操作的事件响应
33
     * @access protected
34
     * @param  bool $event  是否需要事件响应
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
35
     * @return $this
36
     */
37
    public function withEvent(bool $event)
38
    {
39
        $this->withEvent = $event;
40
        return $this;
41
    }
42
43
    /**
44
     * 触发事件
45
     * @access protected
46
     * @param  string $event 事件名
47
     * @return bool
48
     */
49
    protected function trigger(string $event): bool
50
    {
51
        if (!$this->withEvent) {
52
            return true;
53
        }
54
55
        $call = 'on' . App::parseName($event, 1);
56
57
        try {
58
            if (method_exists(static::class, $call)) {
59
                $result = Container::getInstance()->invoke([static::class, $call], [$this]);
60
            } else {
61
                $result = true;
62
            }
63
64
            return false === $result ? false : true;
65
        } catch (ModelEventException $e) {
66
            return false;
67
        }
68
    }
69
}
70