-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventDispatcher.php
96 lines (82 loc) · 2.66 KB
/
EventDispatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* This file is part of the Vection package.
*
* (c) David M. Lung <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Vection\Component\Event;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Throwable;
use Vection\Contracts\Event\EventDispatcherInterface;
use Vection\Contracts\Event\EventInterface;
use Vection\Contracts\Event\EventListenerProviderInterface;
/**
* Class EventDispatcher
*
* @package Vection\Component\Event
*
* @author David Lung <[email protected]>
*/
class EventDispatcher implements EventDispatcherInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
protected bool $catchExceptions;
protected EventListenerProviderInterface $listenerProvider;
/**
* EventDispatcher constructor.
*
* @param EventListenerProviderInterface $listenerProvider
* @param bool $catchExceptions
*/
public function __construct(EventListenerProviderInterface $listenerProvider, bool $catchExceptions = true)
{
$this->listenerProvider = $listenerProvider;
$this->catchExceptions = $catchExceptions;
$this->logger = new NullLogger();
}
/**
* Provide all relevant listeners with an event to process.
*
* @param object $event
* The object to process.
*
* @return object
* The Event that was passed, now modified by listeners.
*/
public function dispatch(object $event): object
{
$listeners = $this->listenerProvider->getListenersForEvent($event);
if ($c = count($listeners)) {
$this->logger->debug(
sprintf('Execute %d listeners for event %s', $c, str_replace('\\', '.', get_class($event)))
);
}
foreach ($listeners as $listener) {
if ($event instanceof EventInterface && $event->isPropagationStopped()) {
break;
}
if (!$this->catchExceptions) {
$listener($event);
}
else{
try{
$listener($event);
}
catch (Throwable $e) {
$this->logger->critical('Unexpected exception during execution of event listener.', [
'event' => get_class($event),
'listener' => is_object($listener) ? get_class($listener) : $listener,
'exception' => $e
]);
}
}
}
return $event;
}
}