Skip to content

Commit

Permalink
增加EventObserverable trait用于事件触发
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 5, 2024
1 parent 1bb0392 commit 39188ad
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/exception/EventException.php
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
{
}
97 changes: 97 additions & 0 deletions src/helper/EventObserverable.php
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;
}
}
}

0 comments on commit 39188ad

Please sign in to comment.