6 Commits

Author SHA1 Message Date
as2252258 a44cfb70b1 modify mysql result 2022-04-29 16:43:54 +08:00
as2252258 100ce506ea modify plugin name 2022-03-03 18:30:59 +08:00
as2252258 a4c50f975e modify plugin name 2022-02-23 16:32:08 +08:00
as2252258 f7630a4c2a Revert "改名"
This reverts commit fdf58326
2022-01-20 19:04:16 +08:00
as2252258 bf3182660b Revert "改名"
This reverts commit fdf58326
2022-01-14 11:29:16 +08:00
as2252258 df7b50563f Revert "改名"
This reverts commit fdf58326
2022-01-12 14:10:33 +08:00
2 changed files with 36 additions and 3 deletions
+21 -3
View File
@@ -2,8 +2,10 @@
namespace Kiri\Events;
use Kiri;
use Kiri\Abstracts\Component;
use Kiri\Kiri;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface;
@@ -15,17 +17,33 @@ class EventDispatch extends Component implements EventDispatcherInterface
{
/**
* @param EventProvider $eventProvider
* @param array $config
* @throws \Exception
*/
public function __construct(public EventProvider $eventProvider, array $config = [])
{
parent::__construct($config);
}
/**
* @param object $event
* @return object
* @throws \ReflectionException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function dispatch(object $event): object
{
$lists = $this->eventProvider->getListenersForEvent($event);
foreach ($lists as $listener) {
/** @var Struct $list */
$listener($event);
try {
$listener($event);
}catch (\Throwable $exception) {
$this->logger->addError($exception);
}
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
break;
}
+15
View File
@@ -41,4 +41,19 @@ class EventProvider implements ListenerProviderInterface
}
/**
* @param string $event
* @param callable $handler
* @return void
*/
public function off(string $event, callable $handler)
{
$events = $this->_listeners[$event] ?? [];
$this->_listeners[$event] = array_filter($events, function ($value) use ($handler) {
return $value->listener !== $handler;
});
}
}