-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
// +---------------------------------------------------------------------- | ||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
// +---------------------------------------------------------------------- | ||
// | Copyright (c) 2006-2021 http://thinkphp.cn All rights reserved. | ||
// +---------------------------------------------------------------------- | ||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
// +---------------------------------------------------------------------- | ||
// | Author: yunwuxin <448901948@qq.com> | ||
// +---------------------------------------------------------------------- | ||
declare (strict_types = 1); | ||
|
||
namespace think\exception; | ||
|
||
/** | ||
* 事件执行异常 | ||
*/ | ||
class EventException extends \RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
// +---------------------------------------------------------------------- | ||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
// +---------------------------------------------------------------------- | ||
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
// +---------------------------------------------------------------------- | ||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
// +---------------------------------------------------------------------- | ||
// | Author: yunwuxin <448901948@qq.com> | ||
// +---------------------------------------------------------------------- | ||
namespace think\helper; | ||
|
||
use ReflectionClass; | ||
use think\exception\EventException; | ||
use think\facade\Event; | ||
use think\helper\Str; | ||
|
||
trait EventObserverable | ||
{ | ||
/** | ||
* 是否需要事件响应. | ||
* | ||
* @var bool | ||
*/ | ||
protected $withEvent = true; | ||
|
||
/** | ||
* 事件观察者. | ||
* | ||
* @var string | ||
*/ | ||
protected $eventObserver; | ||
|
||
/** | ||
* 当前操作的事件响应. | ||
* | ||
* @param bool $event 是否需要事件响应 | ||
* | ||
* @return $this | ||
*/ | ||
public function withEvent(bool $event) | ||
{ | ||
$this->withEvent = $event; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* 设置事件观察者. | ||
* | ||
* @param string $observer 事件观察者 | ||
* | ||
* @return $this | ||
*/ | ||
public function observer($observer) | ||
{ | ||
$this->eventObserver = $observer; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* 触发事件. | ||
* | ||
* @param string $event 事件名 | ||
* | ||
* @return bool | ||
*/ | ||
protected function trigger(string $event): bool | ||
{ | ||
if (!$this->withEvent) { | ||
return true; | ||
} | ||
|
||
$call = 'on' . Str::studly($event); | ||
|
||
try { | ||
if ($this->eventObserver) { | ||
$reflect = new ReflectionClass($this->eventObserver); | ||
$observer = $reflect->newinstance(); | ||
} else { | ||
$observer = static::class; | ||
} | ||
|
||
if (method_exists($observer, $call)) { | ||
$result = call_user_func([$observer, $call], $this); | ||
} else { | ||
$result = Event::trigger($event, $this); | ||
$result = empty($result) ? true : end($result); | ||
} | ||
|
||
return !(false === $result); | ||
} catch (EventException $e) { | ||
return false; | ||
} | ||
} | ||
} |