Skip to content

Commit 39188ad

Browse files
committed
增加EventObserverable trait用于事件触发
1 parent 1bb0392 commit 39188ad

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

src/exception/EventException.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006-2021 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: yunwuxin <[email protected]>
10+
// +----------------------------------------------------------------------
11+
declare (strict_types = 1);
12+
13+
namespace think\exception;
14+
15+
/**
16+
* 事件执行异常
17+
*/
18+
class EventException extends \RuntimeException
19+
{
20+
}

src/helper/EventObserverable.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: yunwuxin <[email protected]>
10+
// +----------------------------------------------------------------------
11+
namespace think\helper;
12+
13+
use ReflectionClass;
14+
use think\exception\EventException;
15+
use think\facade\Event;
16+
use think\helper\Str;
17+
18+
trait EventObserverable
19+
{
20+
/**
21+
* 是否需要事件响应.
22+
*
23+
* @var bool
24+
*/
25+
protected $withEvent = true;
26+
27+
/**
28+
* 事件观察者.
29+
*
30+
* @var string
31+
*/
32+
protected $eventObserver;
33+
34+
/**
35+
* 当前操作的事件响应.
36+
*
37+
* @param bool $event 是否需要事件响应
38+
*
39+
* @return $this
40+
*/
41+
public function withEvent(bool $event)
42+
{
43+
$this->withEvent = $event;
44+
45+
return $this;
46+
}
47+
48+
/**
49+
* 设置事件观察者.
50+
*
51+
* @param string $observer 事件观察者
52+
*
53+
* @return $this
54+
*/
55+
public function observer($observer)
56+
{
57+
$this->eventObserver = $observer;
58+
59+
return $this;
60+
}
61+
62+
/**
63+
* 触发事件.
64+
*
65+
* @param string $event 事件名
66+
*
67+
* @return bool
68+
*/
69+
protected function trigger(string $event): bool
70+
{
71+
if (!$this->withEvent) {
72+
return true;
73+
}
74+
75+
$call = 'on' . Str::studly($event);
76+
77+
try {
78+
if ($this->eventObserver) {
79+
$reflect = new ReflectionClass($this->eventObserver);
80+
$observer = $reflect->newinstance();
81+
} else {
82+
$observer = static::class;
83+
}
84+
85+
if (method_exists($observer, $call)) {
86+
$result = call_user_func([$observer, $call], $this);
87+
} else {
88+
$result = Event::trigger($event, $this);
89+
$result = empty($result) ? true : end($result);
90+
}
91+
92+
return !(false === $result);
93+
} catch (EventException $e) {
94+
return false;
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)