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

Commit b9af1d2

Browse files
committed
Merge branch 'hotfix/zend-navigation-23'
Close #55
2 parents 2ef915e + 67ee19c commit b9af1d2

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

CHANGELOG.md

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

1919
### Fixed
2020

21-
- Nothing.
21+
- [#55](https://github.com/zendframework/zend-view/pull/55) fixes a circular
22+
dependency issue in the navigation helpers with regards to event manager
23+
resolution.
2224

2325
## 2.6.4 - 2016-03-02
2426

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)