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