28 private array $listeners = [];
35 private array $pendingSubscribers = [];
37 private ObjectFactory $objectFactory;
40 $this->objectFactory = $objectFactory;
50 foreach ( $event->getEventTypeChain() as $type ) {
51 $this->dispatchAs( $type, $event, $dbProvider );
56 $this->resolveSubscribers( $type );
57 $listeners = $this->listeners[ $type ] ?? [];
65 foreach ( $listeners ?? [] as $callback ) {
66 $this->push( $callback, $event, $dbProvider );
81 array $options = self::DEFAULT_LISTENER_OPTIONS
83 if ( !is_callable( $listener ) ) {
84 throw new InvalidArgumentException(
'$listener must be callable' );
87 $this->listeners[$eventType][] = $listener;
99 $subscriber->registerListeners( $this );
103 if ( !is_array( $subscriber ) ) {
104 throw new InvalidArgumentException(
105 '$subscriber must be a DomainEventSubscriber or an array'
114 if ( !isset( $subscriber[
'events'] ) ) {
115 throw new InvalidArgumentException(
116 '$subscriber must contain the key "events" to specify which ' .
117 'events will trigger instantiation of the subscriber'
123 foreach ( $subscriber[
'events'] as $eventType ) {
126 $this->pendingSubscribers[$eventType][] =& $subscriber;
134 private function resolveSubscribers(
string $eventType ) {
137 $pending = $this->pendingSubscribers[$eventType] ?? [];
142 $this->pendingSubscribers[$eventType] = [];
145 foreach ( $pending as &$spec ) {
152 $this->applySubscriberSpec( $spec );
159 if ( $this->pendingSubscribers[$eventType] ) {
162 $this->resolveSubscribers( $eventType );
166 private function applySubscriberSpec( array $spec ) {
168 $subscriber = $this->objectFactory->createObject(
170 [
'assertClass' => DomainEventSubscriber::class, ]
173 if ( $subscriber instanceof InitializableDomainEventSubscriber ) {
174 $subscriber->initSubscriber( $spec );
177 $subscriber->registerListeners( $this );
184 private function push(
187 IConnectionProvider $dbProvider
191 $dbw = $dbProvider->getPrimaryDatabase();
192 DeferredUpdates::addUpdate(
new MWCallableUpdate(
193 function () use ( $callback, $event ) {
194 $this->invoke( $callback, $event );
204 private function invoke( callable $callback, DomainEvent $event ) {