Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Commit cb65273

Browse files
author
Charlotte Dunois
committed
Add new error throwing trait
1 parent bd91738 commit cb65273

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/EventEmitterErrorTrait.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* EventEmitter
4+
* Copyright 2018 Charlotte Dunois, All Rights Reserved
5+
*
6+
* Website: https://charuru.moe
7+
* License: https://github.com/CharlotteDunois/EventEmitter/blob/master/LICENSE
8+
*/
9+
10+
namespace CharlotteDunois\Events;
11+
12+
/**
13+
* Our Event Emitter Error Trait. This one throws an exception if there is not an error event listener when an error event gets emitted.
14+
*/
15+
trait EventEmitterErrorTrait {
16+
use EventEmitterTrait;
17+
18+
/**
19+
* Emits an event, catching all exceptions and emitting an error event for these exceptions.
20+
* @param string $event
21+
* @param mixed ...$arguments
22+
* @return void
23+
* @throws \Throwable Any Throwable, Exception, Error or ErrorException by the listener.
24+
* @throws \Exception Any Throwable, Exception, Error or ErrorException by the listener.
25+
* @throws \Error Any Throwable, Exception, Error or ErrorException by the listener.
26+
* @throws \CharlotteDunois\Events\UnhandledErrorException Thrown when an error event goes unhandled.
27+
*/
28+
function emit(string $event, ...$arguments) {
29+
$errorEmpty = ($event === 'error');
30+
31+
if(!empty($this->listeners[$event])) {
32+
$errorEmpty = false;
33+
34+
foreach($this->listeners[$event] as $listener) {
35+
$listener(...$arguments);
36+
}
37+
}
38+
39+
if(!empty($this->onceListeners[$event])) {
40+
$errorEmpty = false;
41+
42+
$listeners = $this->onceListeners[$event];
43+
unset($this->onceListeners[$event]);
44+
45+
foreach($listeners as $listener) {
46+
$listener(...$arguments);
47+
}
48+
}
49+
50+
if($errorEmpty) {
51+
throw new \CharlotteDunois\Events\UnhandledErrorException('Unhandled error event', 0, (($arguments[0] ?? null) instanceof \Throwable ? $arguments[0] : null));
52+
}
53+
}
54+
}

src/EventEmitterTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ function listeners(?string $event = null) {
144144
* @throws \Error Any Throwable, Exception, Error or ErrorException by the listener.
145145
*/
146146
function emit(string $event, ...$arguments) {
147-
if(isset($this->listeners[$event])) {
147+
if(!empty($this->listeners[$event])) {
148148
foreach($this->listeners[$event] as $listener) {
149149
$listener(...$arguments);
150150
}
151151
}
152152

153-
if(isset($this->onceListeners[$event])) {
153+
if(!empty($this->onceListeners[$event])) {
154154
$listeners = $this->onceListeners[$event];
155155
unset($this->onceListeners[$event]);
156156

src/UnhandledErrorException.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* EventEmitter
4+
* Copyright 2018 Charlotte Dunois, All Rights Reserved
5+
*
6+
* Website: https://charuru.moe
7+
* License: https://github.com/CharlotteDunois/EventEmitter/blob/master/LICENSE
8+
*/
9+
10+
namespace CharlotteDunois\Events;
11+
12+
/**
13+
* Thrown when an error event gets emitted, but not handled (aka no listeners).
14+
*/
15+
class UnhandledErrorException extends \RuntimeException {
16+
17+
}

0 commit comments

Comments
 (0)