1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jellyfish\Event; |
6
|
|
|
|
7
|
|
|
use Throwable; |
8
|
|
|
|
9
|
|
|
use function count; |
10
|
|
|
|
11
|
|
|
abstract class AbstractEventListener implements EventListenerInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Jellyfish\Event\EventErrorHandlerInterface[] |
15
|
|
|
*/ |
16
|
|
|
protected $errorHandlers; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param \Jellyfish\Event\EventErrorHandlerInterface[] $errorHandlers |
20
|
|
|
*/ |
21
|
|
|
public function __construct(array $errorHandlers = []) |
22
|
|
|
{ |
23
|
|
|
$this->errorHandlers = $errorHandlers; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return \Jellyfish\Event\EventErrorHandlerInterface[] |
28
|
|
|
*/ |
29
|
|
|
public function getErrorHandlers(): array |
30
|
|
|
{ |
31
|
|
|
return $this->errorHandlers; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param \Jellyfish\Event\EventErrorHandlerInterface[] $errorHandlers |
36
|
|
|
* |
37
|
|
|
* @return \Jellyfish\Event\EventListenerInterface |
38
|
|
|
*/ |
39
|
|
|
public function setErrorHandlers(array $errorHandlers): EventListenerInterface |
40
|
|
|
{ |
41
|
|
|
$this->errorHandlers = $errorHandlers; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \Jellyfish\Event\EventErrorHandlerInterface $errorHandler |
48
|
|
|
* |
49
|
|
|
* @return \Jellyfish\Event\EventListenerInterface |
50
|
|
|
*/ |
51
|
|
|
public function addErrorHandler(EventErrorHandlerInterface $errorHandler): EventListenerInterface |
52
|
|
|
{ |
53
|
|
|
$this->errorHandlers[] = $errorHandler; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param \Jellyfish\Event\EventInterface $event |
60
|
|
|
* |
61
|
|
|
* @return \Jellyfish\Event\EventListenerInterface |
62
|
|
|
* |
63
|
|
|
* @throws \Throwable |
64
|
|
|
*/ |
65
|
|
|
public function handle(EventInterface $event): EventListenerInterface |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
|
|
$this->doHandle($event); |
69
|
|
|
} catch (Throwable $error) { |
70
|
|
|
$this->handleError($error, $event); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param \Jellyfish\Event\EventInterface $event |
78
|
|
|
* |
79
|
|
|
* @return \Jellyfish\Event\EventListenerInterface |
80
|
|
|
*/ |
81
|
|
|
abstract protected function doHandle(EventInterface $event): EventListenerInterface; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param \Throwable $error |
85
|
|
|
* @param \Jellyfish\Event\EventInterface $event |
86
|
|
|
* |
87
|
|
|
* @return \Jellyfish\Event\EventListenerInterface |
88
|
|
|
* |
89
|
|
|
* @throws \Throwable |
90
|
|
|
*/ |
91
|
|
|
protected function handleError(Throwable $error, EventInterface $event): EventListenerInterface |
92
|
|
|
{ |
93
|
|
|
if ($this->errorHandlers === null || count($this->errorHandlers) === 0) { |
94
|
|
|
throw $error; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($this->errorHandlers as $errorHandler) { |
98
|
|
|
$errorHandler->handle($error, $this->getIdentifier(), $event); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|