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

Commit fed4b7a

Browse files
committed
Running tests of zend-navigation against all v2 components (no v3 components) revealed a circular dependency condition in the navigation helpers related to `getEventManager()`. In v2, `getEventManager()` has lazy-loaded an EM instance, and an initializer was checking the returned instance to see if its `SharedEventManager` instance was present and/or the same as the one in the container; if not, it was re-injecting the EM instance from the container. Unfortunately, this fails now, as the call to `setEventManager()` now attaches the default listeners, and requires that a shared manager is present. This patch changes the behavior to the following: - `getEventManager()` now *never* lazy-loads an instance. This ensures that the initializer doesn't lead to lazy-population of the shared manager. - `setEventManager()` was updated to check that we have a shared manager before attempting to call `setDefaultListeners()`. - Internally, if an EM instance is needed, it now lazy-creates one, *with a shared manager*, and calls `setEventManager()` with the new EM instance. - The EM initializer in the helper plugin amanger was updated to first check that we have an `EventManager` instance before attempting to inject one.
1 parent 2ef915e commit fed4b7a

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

src/Helper/Navigation/AbstractHelper.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ public function accept(AbstractPage $page, $recursive = true)
342342
*/
343343
protected function isAllowed($params)
344344
{
345-
$results = $this->getEventManager()->trigger(__FUNCTION__, $this, $params);
345+
$events = $this->getEventManager() ?: $this->createEventManager();
346+
$results = $events->trigger(__FUNCTION__, $this, $params);
346347
return $results->last();
347348
}
348349

@@ -513,22 +514,23 @@ public function setEventManager(EventManagerInterface $events)
513514

514515
$this->events = $events;
515516

516-
$this->setDefaultListeners();
517+
if ($events->getSharedManager()) {
518+
$this->setDefaultListeners();
519+
}
517520

518521
return $this;
519522
}
520523

521524
/**
522-
* Get the event manager.
525+
* Get the event manager, if present.
526+
*
527+
* Internally, the helper will lazy-load an EM instance the first time it
528+
* requires one, but ideally it should be injected during instantiation.
523529
*
524-
* @return EventManagerInterface
530+
* @return null|EventManagerInterface
525531
*/
526532
public function getEventManager()
527533
{
528-
if (null === $this->events) {
529-
$this->setEventManager($this->createEventManager());
530-
}
531-
532534
return $this->events;
533535
}
534536

@@ -956,7 +958,13 @@ protected function setDefaultListeners()
956958
return;
957959
}
958960

959-
$this->getEventManager()->getSharedManager()->attach(
961+
$events = $this->getEventManager() ?: $this->createEventManager();
962+
963+
if (! $events->getSharedManager()) {
964+
return;
965+
}
966+
967+
$events->getSharedManager()->attach(
960968
'Zend\View\Helper\Navigation\AbstractHelper',
961969
'isAllowed',
962970
['Zend\View\Helper\Navigation\Listener\AclListener', 'accept']
@@ -975,9 +983,13 @@ private function createEventManager()
975983
{
976984
$r = new ReflectionClass(EventManager::class);
977985
if ($r->hasMethod('setSharedManager')) {
978-
return new EventManager();
986+
$events = new EventManager();
987+
$events->setSharedManager(new SharedEventManager());
988+
} else {
989+
$events = new EventManager(new SharedEventManager());
979990
}
980991

981-
return new EventManager(new SharedEventManager());
992+
$this->setEventManager($events);
993+
return $events;
982994
}
983995
}

src/HelperPluginManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ public function injectEventManager($first, $second)
375375
return;
376376
}
377377

378+
if (! $container->has('EventManager')) {
379+
// If the container doesn't have an EM service, do nothing.
380+
return;
381+
}
382+
378383
$events = $helper->getEventManager();
379384
if (! $events || ! $events->getSharedManager() instanceof SharedEventManagerInterface) {
380385
$helper->setEventManager($container->get('EventManager'));

test/Helper/Navigation/AbstractHelperTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ public function testHasRoleChecksMemberVariable()
7676
$this->_helper->setRole($role);
7777
$this->assertEquals(true, $this->_helper->hasRole());
7878
}
79+
80+
public function testEventManagerIsNullByDefault()
81+
{
82+
$this->assertNull($this->_helper->getEventManager());
83+
}
7984
}

test/Helper/Navigation/AbstractTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@
2323

2424
/**
2525
* Base class for navigation view helper tests
26-
*
27-
* @group Zend_View
28-
* @group Zend_View_Helper
2926
*/
3027
abstract class AbstractTest extends \PHPUnit_Framework_TestCase
3128
{
32-
const REGISTRY_KEY = 'Zend_Navigation';
33-
3429
/**
3530
* @var
3631
*/

0 commit comments

Comments
 (0)