Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit b20fd60

Browse files
committed
Merge branch 'hotfix/90'
Close #96 Fixes #90
2 parents 5b253ce + 53d5dc4 commit b20fd60

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- [#90](https://github.com/zendframework/zend-mvc/pull/90) re-introduces the
21+
- [#95](https://github.com/zendframework/zend-mvc/pull/95) re-introduces the
2222
various zend-di aliases and factories in `Zend\Mvc\Service\ServiceListenerFactory`,
2323
which were accidently removed in the 2.7.0 release.
24+
- [#96](https://github.com/zendframework/zend-mvc/pull/96) fixes shared event
25+
detachment/attachment within the `Forward` plugin to work with both v2 and v3
26+
of zend-eventmanager.
2427

2528
## 2.7.1 - 2016-03-02
2629

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"zendframework/zend-version": "^2.5",
4343
"zendframework/zend-view": "^2.6.3",
4444
"fabpot/php-cs-fixer": "1.7.*",
45-
"phpunit/PHPUnit": "~4.0"
45+
"phpunit/PHPUnit": "^4.5",
46+
"sebastian/version": "^1.0.4"
4647
},
4748
"suggest": {
4849
"zendframework/zend-authentication": "Zend\\Authentication component for Identity plugin",

src/Controller/Plugin/Forward.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Zend\Mvc\InjectApplicationEventInterface;
1616
use Zend\Mvc\MvcEvent;
1717
use Zend\Mvc\Router\RouteMatch;
18+
use Zend\Stdlib\CallbackHandler;
1819

1920
class Forward extends AbstractPlugin
2021
{
@@ -178,9 +179,15 @@ protected function detachProblemListeners(SharedEvents $sharedEvents)
178179
foreach ($eventArray as $eventName => $classArray) {
179180
$results[$id][$eventName] = [];
180181
$events = $this->getSharedListenersById($id, $eventName, $sharedEvents);
181-
foreach ($events as $currentEvent) {
182+
foreach ($events as $priority => $currentEvent) {
182183
$currentCallback = $currentEvent;
183184

185+
// zend-eventmanager v2 compatibility:
186+
if ($currentCallback instanceof CallbackHandler) {
187+
$currentCallback = $currentEvent->getCallback();
188+
$priority = $currentEvent->getMetadatum('priority');
189+
}
190+
184191
// If we have an array, grab the object
185192
if (is_array($currentCallback)) {
186193
$currentCallback = array_shift($currentCallback);
@@ -193,8 +200,11 @@ protected function detachProblemListeners(SharedEvents $sharedEvents)
193200

194201
foreach ($classArray as $class) {
195202
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;
198208
}
199209
}
200210
}
@@ -215,8 +225,16 @@ protected function reattachProblemListeners(SharedEvents $sharedEvents, array $l
215225
{
216226
foreach ($listeners as $id => $eventArray) {
217227
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);
220238
}
221239
}
222240
}
@@ -276,4 +294,26 @@ private function getSharedListenersById($id, $event, SharedEvents $sharedEvents)
276294
// v3
277295
return $sharedEvents->getListeners([$id], $event);
278296
}
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+
}
279319
}

0 commit comments

Comments
 (0)