|
28 | 28 | */
|
29 | 29 | class EventDataCollector extends DataCollector implements LateDataCollectorInterface
|
30 | 30 | {
|
31 |
| - private ?EventDispatcherInterface $dispatcher; |
| 31 | + /** @var iterable<EventDispatcherInterface> */ |
| 32 | + private iterable $dispatchers; |
32 | 33 | private ?RequestStack $requestStack;
|
33 | 34 | private ?Request $currentRequest = null;
|
34 | 35 |
|
35 |
| - public function __construct(EventDispatcherInterface $dispatcher = null, RequestStack $requestStack = null) |
| 36 | + /** |
| 37 | + * @param iterable<EventDispatcherInterface>|EventDispatcherInterface|null $dispatchers |
| 38 | + */ |
| 39 | + public function __construct(iterable|EventDispatcherInterface|null $dispatchers = [], RequestStack $requestStack = null) |
36 | 40 | {
|
37 |
| - $this->dispatcher = $dispatcher; |
| 41 | + if (!is_iterable($dispatchers)) { |
| 42 | + $dispatchers = $dispatchers ? ['event_dispatcher' => $dispatchers] : []; |
| 43 | + } |
| 44 | + $this->dispatchers = $dispatchers; |
38 | 45 | $this->requestStack = $requestStack;
|
39 | 46 | }
|
40 | 47 |
|
41 | 48 | public function collect(Request $request, Response $response, \Throwable $exception = null): void
|
42 | 49 | {
|
43 | 50 | $this->currentRequest = $this->requestStack && $this->requestStack->getMainRequest() !== $request ? $request : null;
|
44 |
| - $this->data = [ |
45 |
| - 'called_listeners' => [], |
46 |
| - 'not_called_listeners' => [], |
47 |
| - 'orphaned_events' => [], |
48 |
| - ]; |
| 51 | + $this->data = []; |
49 | 52 | }
|
50 | 53 |
|
51 | 54 | public function reset(): void
|
52 | 55 | {
|
53 | 56 | $this->data = [];
|
54 | 57 |
|
55 |
| - if ($this->dispatcher instanceof ResetInterface) { |
56 |
| - $this->dispatcher->reset(); |
| 58 | + foreach ($this->dispatchers as $dispatcher) { |
| 59 | + if ($dispatcher instanceof ResetInterface) { |
| 60 | + $dispatcher->reset(); |
| 61 | + } |
57 | 62 | }
|
58 | 63 | }
|
59 | 64 |
|
60 | 65 | public function lateCollect(): void
|
61 | 66 | {
|
62 |
| - if ($this->dispatcher instanceof TraceableEventDispatcher) { |
63 |
| - $this->setCalledListeners($this->dispatcher->getCalledListeners($this->currentRequest)); |
64 |
| - $this->setNotCalledListeners($this->dispatcher->getNotCalledListeners($this->currentRequest)); |
65 |
| - $this->setOrphanedEvents($this->dispatcher->getOrphanedEvents($this->currentRequest)); |
| 67 | + foreach ($this->dispatchers as $name => $dispatcher) { |
| 68 | + if (!$dispatcher instanceof TraceableEventDispatcher) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + $this->setCalledListeners($dispatcher->getCalledListeners($this->currentRequest), $name); |
| 73 | + $this->setNotCalledListeners($dispatcher->getNotCalledListeners($this->currentRequest), $name); |
| 74 | + $this->setOrphanedEvents($dispatcher->getOrphanedEvents($this->currentRequest), $name); |
66 | 75 | }
|
67 | 76 |
|
68 | 77 | $this->data = $this->cloneVar($this->data);
|
69 | 78 | }
|
70 | 79 |
|
| 80 | + public function getData(): array|Data |
| 81 | + { |
| 82 | + return $this->data; |
| 83 | + } |
| 84 | + |
71 | 85 | /**
|
72 | 86 | * @see TraceableEventDispatcher
|
73 | 87 | */
|
74 |
| - public function setCalledListeners(array $listeners): void |
| 88 | + public function setCalledListeners(array $listeners, string $dispatcher = 'event_dispatcher'): void |
75 | 89 | {
|
76 |
| - $this->data['called_listeners'] = $listeners; |
| 90 | + $this->data[$dispatcher]['called_listeners'] = $listeners; |
77 | 91 | }
|
78 | 92 |
|
79 | 93 | /**
|
80 | 94 | * @see TraceableEventDispatcher
|
81 | 95 | */
|
82 |
| - public function getCalledListeners(): array|Data |
| 96 | + public function getCalledListeners(string $dispatcher = 'event_dispatcher'): array|Data |
83 | 97 | {
|
84 |
| - return $this->data['called_listeners']; |
| 98 | + return $this->data[$dispatcher]['called_listeners'] ?? []; |
85 | 99 | }
|
86 | 100 |
|
87 | 101 | /**
|
88 | 102 | * @see TraceableEventDispatcher
|
89 | 103 | */
|
90 |
| - public function setNotCalledListeners(array $listeners): void |
| 104 | + public function setNotCalledListeners(array $listeners, string $dispatcher = 'event_dispatcher'): void |
91 | 105 | {
|
92 |
| - $this->data['not_called_listeners'] = $listeners; |
| 106 | + $this->data[$dispatcher]['not_called_listeners'] = $listeners; |
93 | 107 | }
|
94 | 108 |
|
95 | 109 | /**
|
96 | 110 | * @see TraceableEventDispatcher
|
97 | 111 | */
|
98 |
| - public function getNotCalledListeners(): array|Data |
| 112 | + public function getNotCalledListeners(string $dispatcher = 'event_dispatcher'): array|Data |
99 | 113 | {
|
100 |
| - return $this->data['not_called_listeners']; |
| 114 | + return $this->data[$dispatcher]['not_called_listeners'] ?? []; |
101 | 115 | }
|
102 | 116 |
|
103 | 117 | /**
|
104 | 118 | * @param array $events An array of orphaned events
|
105 | 119 | *
|
106 | 120 | * @see TraceableEventDispatcher
|
107 | 121 | */
|
108 |
| - public function setOrphanedEvents(array $events): void |
| 122 | + public function setOrphanedEvents(array $events, string $dispatcher = 'event_dispatcher'): void |
109 | 123 | {
|
110 |
| - $this->data['orphaned_events'] = $events; |
| 124 | + $this->data[$dispatcher]['orphaned_events'] = $events; |
111 | 125 | }
|
112 | 126 |
|
113 | 127 | /**
|
114 | 128 | * @see TraceableEventDispatcher
|
115 | 129 | */
|
116 |
| - public function getOrphanedEvents(): array|Data |
| 130 | + public function getOrphanedEvents(string $dispatcher = 'event_dispatcher'): array|Data |
117 | 131 | {
|
118 |
| - return $this->data['orphaned_events']; |
| 132 | + return $this->data[$dispatcher]['orphaned_events'] ?? []; |
119 | 133 | }
|
120 | 134 |
|
121 | 135 | public function getName(): string
|
|
0 commit comments