MediaWiki master
EventDispatchEngine.php
Go to the documentation of this file.
1<?php
2namespace MediaWiki\DomainEvent;
3
4use InvalidArgumentException;
5use MediaWiki\Deferred\DeferredUpdates;
6use MediaWiki\Deferred\MWCallableUpdate;
7use Wikimedia\ObjectFactory\ObjectFactory;
8use Wikimedia\Rdbms\IConnectionProvider;
9
23
28 private array $listeners = [];
29
35 private array $pendingSubscribers = [];
36
37 private ObjectFactory $objectFactory;
38
39 public function __construct( ObjectFactory $objectFactory ) {
40 $this->objectFactory = $objectFactory;
41 }
42
49 public function dispatch( DomainEvent $event, IConnectionProvider $dbProvider ): void {
50 foreach ( $event->getEventTypeChain() as $type ) {
51 $this->dispatchAs( $type, $event, $dbProvider );
52 }
53 }
54
55 private function dispatchAs( string $type, DomainEvent $event, IConnectionProvider $dbProvider ): void {
56 $this->resolveSubscribers( $type );
57 $listeners = $this->listeners[ $type ] ?? [];
58
59 // NOTE: If we want to introduce a synchronous or pre-commit invocation mode
60 // here, it should only be used if the emitter opts into this.
61 // The contract of the dispatch() method doesn't allow listeners
62 // to be invoked within the current transaction.
63
64 // Push a DeferredUpdate for listeners registered for handling AFTER_COMMIT.
65 foreach ( $listeners ?? [] as $callback ) {
66 $this->push( $callback, $event, $dbProvider );
67 }
68 }
69
78 public function registerListener(
79 string $eventType,
80 $listener,
81 array $options = self::DEFAULT_LISTENER_OPTIONS
82 ): void {
83 if ( !is_callable( $listener ) ) {
84 throw new InvalidArgumentException( '$listener must be callable' );
85 }
86
87 $this->listeners[$eventType][] = $listener;
88 }
89
93 public function registerSubscriber( $subscriber ): void {
94 if ( $subscriber instanceof DomainEventSubscriber ) {
95 // If we have a DomainEventSubscriber, apply it immediately.
96 // We can't wait until later, because we don't know what events
97 // it wants to register for, so we don't know on what event to
98 // call registerListeners().
99 $subscriber->registerListeners( $this );
100 return;
101 }
102
103 if ( !is_array( $subscriber ) ) {
104 throw new InvalidArgumentException(
105 '$subscriber must be a DomainEventSubscriber or an array'
106 );
107 }
108
109 // NOTE: we could make the 'events' key optional, and just call
110 // applySubscriberSpec() immediately if it's not given. But that would
111 // make it too easy to just forget about providing it. Callers that want
112 // to apply the subscriber immediately can just create the object instead
113 // of passing a spec 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'
118 );
119 }
120
121 // Register the spec for lazy instantiation when any of the relevant
122 // events is triggered.
123 foreach ( $subscriber['events'] as $eventType ) {
124 // NOTE: must be by reference, so the spec can be resolved for all
125 // events that trigger instantiation at once.
126 $this->pendingSubscribers[$eventType][] =& $subscriber;
127 }
128 }
129
134 private function resolveSubscribers( string $eventType ) {
135 // Copy the list of pending subscribers, since applying subscribers
136 // may modify $this->pendingSubscribers again.
137 $pending = $this->pendingSubscribers[$eventType] ?? [];
138
139 // Blank subscribers for this event. Once the subscribers have been
140 // applied, all their listeners are registered, and the subscribers
141 // are no longer relevant.
142 $this->pendingSubscribers[$eventType] = [];
143
144 // NOTE: entries in $this->subscribers are by reference!
145 foreach ( $pending as &$spec ) {
146 // NOTE: $spec may be empty if it was previously resolved through
147 // a different event type.
148 if ( !$spec ) {
149 continue;
150 }
151
152 $this->applySubscriberSpec( $spec );
153
154 // Empty the spec, so it's not re-triggered though another event
155 // that also references it.
156 $spec = [];
157 }
158
159 if ( $this->pendingSubscribers[$eventType] ) {
160 // If more pending subscribers got added, recurse!
161 // @phan-suppress-next-line PhanPossiblyInfiniteRecursionSameParams We've changed the data
162 $this->resolveSubscribers( $eventType );
163 }
164 }
165
166 private function applySubscriberSpec( array $spec ) {
168 $subscriber = $this->objectFactory->createObject(
169 $spec,
170 [ 'assertClass' => DomainEventSubscriber::class, ]
171 );
172
173 if ( $subscriber instanceof InitializableDomainEventSubscriber ) {
174 $subscriber->initSubscriber( $spec );
175 }
176
177 $subscriber->registerListeners( $this );
178 }
179
184 private function push(
185 callable $callback,
186 DomainEvent $event,
187 IConnectionProvider $dbProvider
188 ) {
189 // TODO: DeferredUpdates should take a more abstract representation of
190 // the current transactional context!
191 $dbw = $dbProvider->getPrimaryDatabase();
192 DeferredUpdates::addUpdate( new MWCallableUpdate(
193 function () use ( $callback, $event ) {
194 $this->invoke( $callback, $event );
195 },
196 __METHOD__,
197 [ $dbw ]
198 ) );
199 }
200
204 private function invoke( callable $callback, DomainEvent $event ) {
205 $callback( $event );
206 }
207
208}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:72
Base class for domain event objects to be used with DomainEventDispatcher.
Implementation of DomainEventDispatcher and DomainEventSource based on HookContainer and DeferredUpda...
dispatch(DomainEvent $event, IConnectionProvider $dbProvider)
Emit the given event to any listeners that have been registered for the respective event type.
registerListener(string $eventType, $listener, array $options=self::DEFAULT_LISTENER_OPTIONS)
Add a listener that will be notified on events of the given type.
Service for sending domain events to registered listeners.
Service object for registering listeners for domain events.
Objects implementing DomainEventSubscriber represent a collection of related event listeners.
Provide primary and replica IDatabase connections.
if(!isset( $specs[$class])) $spec