15
15
use Zend \Mvc \InjectApplicationEventInterface ;
16
16
use Zend \Mvc \MvcEvent ;
17
17
use Zend \Mvc \Router \RouteMatch ;
18
+ use Zend \Stdlib \CallbackHandler ;
18
19
19
20
class Forward extends AbstractPlugin
20
21
{
@@ -178,9 +179,15 @@ protected function detachProblemListeners(SharedEvents $sharedEvents)
178
179
foreach ($ eventArray as $ eventName => $ classArray ) {
179
180
$ results [$ id ][$ eventName ] = [];
180
181
$ events = $ this ->getSharedListenersById ($ id , $ eventName , $ sharedEvents );
181
- foreach ($ events as $ currentEvent ) {
182
+ foreach ($ events as $ priority => $ currentEvent ) {
182
183
$ currentCallback = $ currentEvent ;
183
184
185
+ // zend-eventmanager v2 compatibility:
186
+ if ($ currentCallback instanceof CallbackHandler) {
187
+ $ currentCallback = $ currentEvent ->getCallback ();
188
+ $ priority = $ currentEvent ->getMetadatum ('priority ' );
189
+ }
190
+
184
191
// If we have an array, grab the object
185
192
if (is_array ($ currentCallback )) {
186
193
$ currentCallback = array_shift ($ currentCallback );
@@ -193,8 +200,11 @@ protected function detachProblemListeners(SharedEvents $sharedEvents)
193
200
194
201
foreach ($ classArray as $ class ) {
195
202
if ($ currentCallback instanceof $ class ) {
196
- $ sharedEvents ->detach ($ currentEvent , $ id );
197
- $ results [$ id ][$ eventName ][] = $ currentEvent ;
203
+ // Pass $currentEvent; when using zend-eventmanager v2,
204
+ // this is the CallbackHandler, while in v3 it's
205
+ // the actual listener.
206
+ $ this ->detachSharedListener ($ id , $ currentEvent , $ sharedEvents );
207
+ $ results [$ id ][$ eventName ][$ priority ] = $ currentEvent ;
198
208
}
199
209
}
200
210
}
@@ -215,8 +225,16 @@ protected function reattachProblemListeners(SharedEvents $sharedEvents, array $l
215
225
{
216
226
foreach ($ listeners as $ id => $ eventArray ) {
217
227
foreach ($ eventArray as $ eventName => $ callbacks ) {
218
- foreach ($ callbacks as $ current ) {
219
- $ sharedEvents ->attach ($ id , $ eventName , $ current ->getCallback (), $ current ->getMetadatum ('priority ' ));
228
+ foreach ($ callbacks as $ priority => $ current ) {
229
+ $ callback = $ current ;
230
+
231
+ // zend-eventmanager v2 compatibility:
232
+ if ($ current instanceof CallbackHandler) {
233
+ $ callback = $ current ->getCallback ();
234
+ $ priority = $ current ->getMetadatum ('priority ' );
235
+ }
236
+
237
+ $ sharedEvents ->attach ($ id , $ eventName , $ callback , $ priority );
220
238
}
221
239
}
222
240
}
@@ -276,4 +294,26 @@ private function getSharedListenersById($id, $event, SharedEvents $sharedEvents)
276
294
// v3
277
295
return $ sharedEvents ->getListeners ([$ id ], $ event );
278
296
}
297
+
298
+ /**
299
+ * Detach a shared listener by identifier.
300
+ *
301
+ * Varies detachment based on zend-eventmanager version.
302
+ *
303
+ * @param string|int $id
304
+ * @param callable|CallbackHandler $listener
305
+ * @param SharedEvents $sharedEvents
306
+ * @return void
307
+ */
308
+ private function detachSharedListener ($ id , $ listener , SharedEvents $ sharedEvents )
309
+ {
310
+ if (method_exists ($ sharedEvents , 'attachAggregate ' )) {
311
+ // v2
312
+ $ sharedEvents ->detach ($ id , $ listener );
313
+ return ;
314
+ }
315
+
316
+ // v3
317
+ $ sharedEvents ->detach ($ listener , $ id );
318
+ }
279
319
}
0 commit comments