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

Commit 2e63552

Browse files
committed
Merge branch 'hotfix/49'
Close #50 Fixes #49
2 parents df50e15 + dd615aa commit 2e63552

File tree

9 files changed

+108
-15
lines changed

9 files changed

+108
-15
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ before_install:
5353
- if [[ $ZEND_EVENTMANAGER_VERSION == '' ]]; then composer require --no-update "zendframework/zend-eventmanager:^3.0" ; fi
5454
- if [[ $ZEND_SERVICEMANAGER_VERSION != '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:$ZEND_SERVICEMANAGER_VERSION" ; fi
5555
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:^3.0.3" ; fi
56-
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer remove --dev --no-update zendframework/zend-mvc zendframework/zend-session ; fi
56+
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer remove --dev --no-update zendframework/zend-modulemanager zendframework/zend-mvc zendframework/zend-session ; fi
5757

5858
install:
5959
- travis_retry composer install --no-interaction --ignore-platform-reqs

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.6.3 - TBD
5+
## 2.6.3 - 2016-02-22
66

77
### Added
88

@@ -18,7 +18,13 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#50](https://github.com/zendframework/zend-view/pull/50) fixes
22+
the initializer defined and registered in
23+
`Navigation\PluginManager::__construct()` to ensure it properly pulls and
24+
injects the application container into navigation helpers, under both
25+
zend-servicemanager v2 and v3. Additionally, when lazy-instantiating the
26+
`Navigation\PluginManager`, the `Navigation` helper now passes the composed
27+
service manager instance to its constructor.
2228

2329
## 2.6.2 - 2016-02-18
2430

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"zendframework/zend-i18n": "^2.6",
3131
"zendframework/zend-json": "^2.6.1",
3232
"zendframework/zend-log": "^2.7",
33+
"zendframework/zend-modulemanager": "^2.5",
3334
"zendframework/zend-mvc": "^2.6.1",
3435
"zendframework/zend-navigation": "^2.5",
3536
"zendframework/zend-paginator": "^2.5",

src/Helper/Navigation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public function setPluginManager(Navigation\PluginManager $plugins)
322322
public function getPluginManager()
323323
{
324324
if (null === $this->plugins) {
325-
$this->setPluginManager(new Navigation\PluginManager());
325+
$this->setPluginManager(new Navigation\PluginManager($this->getServiceLocator()));
326326
}
327327

328328
return $this->plugins;

src/Helper/Navigation/AbstractHelper.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Interop\Container\ContainerInterface;
1313
use RecursiveIteratorIterator;
14+
use ReflectionProperty;
1415
use Zend\EventManager\EventManager;
1516
use Zend\EventManager\EventManagerAwareInterface;
1617
use Zend\EventManager\EventManagerInterface;
@@ -19,6 +20,7 @@
1920
use Zend\Navigation;
2021
use Zend\Navigation\Page\AbstractPage;
2122
use Zend\Permissions\Acl;
23+
use Zend\ServiceManager\AbstractPluginManager;
2224
use Zend\View;
2325
use Zend\View\Exception;
2426

@@ -753,6 +755,26 @@ public function hasRole()
753755
*/
754756
public function setServiceLocator(ContainerInterface $serviceLocator)
755757
{
758+
// If we are provided a plugin manager, we should pull the parent
759+
// context from it.
760+
// @todo We should update tests and code to ensure that this situation
761+
// doesn't happen in the future.
762+
if ($serviceLocator instanceof AbstractPluginManager
763+
&& ! method_exists($serviceLocator, 'configure')
764+
&& $serviceLocator->getServiceLocator()
765+
) {
766+
$serviceLocator = $serviceLocator->getServiceLocator();
767+
}
768+
769+
// v3 variant; likely won't be needed.
770+
if ($serviceLocator instanceof AbstractPluginManager
771+
&& method_exists($serviceLocator, 'configure')
772+
) {
773+
$r = new ReflectionProperty($serviceLocator, 'creationContext');
774+
$r->setAccessible(true);
775+
$serviceLocator = $r->getValue($serviceLocator);
776+
}
777+
756778
$this->serviceLocator = $serviceLocator;
757779
return $this;
758780
}

src/Helper/Navigation/PluginManager.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,31 @@ class PluginManager extends HelperPluginManager
6565
*/
6666
public function __construct($configOrContainerInstance = null, array $v3config = [])
6767
{
68-
$this->initializers[] = function ($container, $instance) {
68+
$this->initializers[] = function ($first, $second) {
69+
// v2 vs v3 argument order
70+
if ($first instanceof ContainerInterface) {
71+
// v3
72+
$container = $first;
73+
$instance = $second;
74+
} else {
75+
// v2
76+
$container = $second;
77+
$instance = $first;
78+
}
79+
6980
if (! $instance instanceof AbstractHelper) {
7081
return;
7182
}
7283

84+
// This initializer was written with v2 functionality in mind; as such,
85+
// we need to test and see if we're called in a v2 context, and, if so,
86+
// set the service locator to the parent locator.
87+
//
88+
// Under v3, the parent locator is what is passed to the method already.
89+
if (! method_exists($container, 'configure') && $container->getServiceLocator()) {
90+
$container = $container->getServiceLocator();
91+
}
92+
7393
$instance->setServiceLocator($container);
7494
};
7595

test/Helper/Navigation/AbstractTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ abstract class AbstractTest extends \PHPUnit_Framework_TestCase
8686
*/
8787
protected function setUp()
8888
{
89-
if (! class_exists(PluginFlashMessenger::class)) {
89+
if (! class_exists(ServiceManagerConfig::class)) {
9090
$this->markTestSkipped(
9191
'Skipping zend-mvc-related tests until that component is updated '
9292
. 'to be forwards-compatible with zend-eventmanager, zend-stdlib, '
@@ -126,7 +126,7 @@ protected function setUp()
126126
'extra_config' => [
127127
'service_manager' => [
128128
'factories' => [
129-
'Config' => function () use ($config) {
129+
'config' => function () use ($config) {
130130
return [
131131
'navigation' => [
132132
'default' => $config->get('nav_test1'),
@@ -139,7 +139,10 @@ protected function setUp()
139139
],
140140
];
141141

142-
$sm = $this->serviceManager = new ServiceManager(new ServiceManagerConfig);
142+
$sm = $this->serviceManager = new ServiceManager();
143+
$sm->setAllowOverride(true);
144+
145+
(new ServiceManagerConfig())->configureServiceManager($sm);
143146
$sm->setService('ApplicationConfig', $smConfig);
144147
$sm->get('ModuleManager')->loadModules();
145148
$sm->get('Application')->bootstrap();
@@ -148,6 +151,8 @@ protected function setUp()
148151
$sm->setService('nav1', $this->_nav1);
149152
$sm->setService('nav2', $this->_nav2);
150153

154+
$sm->setAllowOverride(false);
155+
151156
$app = $this->serviceManager->get('Application');
152157
$app->getMvcEvent()->setRouteMatch(new RouteMatch([
153158
'controller' => 'post',

test/Helper/Navigation/NavigationTest.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
namespace ZendTest\View\Helper\Navigation;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\Navigation\Navigation as Container;
14+
use Zend\Navigation\Page;
1315
use Zend\Permissions\Acl;
1416
use Zend\Permissions\Acl\Role;
1517
use Zend\ServiceManager\ServiceManager;
@@ -61,11 +63,10 @@ public function testAcceptAclShouldReturnGracefullyWithUnknownResource()
6163
$this->_helper->setRole($acl['role']);
6264

6365
$accepted = $this->_helper->accept(
64-
new \Zend\Navigation\Page\Uri([
66+
new Page\Uri([
6567
'resource' => 'unknownresource',
6668
'privilege' => 'someprivilege'
67-
],
68-
false)
69+
], false)
6970
);
7071

7172
$this->assertEquals($accepted, false);
@@ -144,10 +145,9 @@ public function testServiceManagerIsUsedToRetrieveContainer()
144145
$serviceManager = new ServiceManager;
145146
$serviceManager->setService('navigation', $container);
146147

147-
$pluginManager = new View\HelperPluginManager;
148-
$pluginManager->setServiceLocator($serviceManager);
148+
$pluginManager = new View\HelperPluginManager($serviceManager);
149149

150-
$this->_helper->setServiceLocator($pluginManager);
150+
$this->_helper->setServiceLocator($serviceManager);
151151
$this->_helper->setContainer('navigation');
152152

153153
$expected = $this->_helper->getContainer();
@@ -558,7 +558,7 @@ public function testMultipleNavigationsWithSameHelperAndSameContainer()
558558

559559
public function testSetPluginManagerAndView()
560560
{
561-
$pluginManager = new \Zend\View\Helper\Navigation\PluginManager();
561+
$pluginManager = new Navigation\PluginManager();
562562
$view = new PhpRenderer();
563563

564564
$helper = new $this->_helperName;
@@ -568,6 +568,27 @@ public function testSetPluginManagerAndView()
568568
$this->assertEquals($view, $pluginManager->getRenderer());
569569
}
570570

571+
/**
572+
* @group 49
573+
*/
574+
public function testInjectsLazyInstantiatedPluginManagerWithCurrentServiceLocator()
575+
{
576+
$services = $this->prophesize(ContainerInterface::class)->reveal();
577+
$helper = new $this->_helperName;
578+
$helper->setServiceLocator($services);
579+
580+
$plugins = $helper->getPluginManager();
581+
$this->assertInstanceOf(Navigation\PluginManager::class, $plugins);
582+
583+
if (method_exists($plugins, 'configure')) {
584+
// v3
585+
$this->assertAttributeSame($services, 'creationContext', $plugins);
586+
} else {
587+
// v2
588+
$this->assertSame($services, $plugins->getServiceLocator());
589+
}
590+
}
591+
571592
/**
572593
* Returns the contens of the expected $file, normalizes newlines
573594
* @param string $file

test/Helper/Navigation/PluginManagerCompatibilityTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Zend\ServiceManager\Test\CommonPluginManagerTrait;
1515
use Zend\View\Exception\InvalidHelperException;
1616
use Zend\View\Helper\Navigation\AbstractHelper;
17+
use Zend\View\Helper\Navigation\Breadcrumbs;
1718
use Zend\View\Helper\Navigation\PluginManager;
1819

1920
/**
@@ -65,4 +66,21 @@ public function testConstructorAllowsConfigInstanceAsFirstArgumentUnderV2()
6566
$helpers = new PluginManager(new Config([]));
6667
$this->assertInstanceOf(PluginManager::class, $helpers);
6768
}
69+
70+
public function testInjectsParentContainerIntoHelpers()
71+
{
72+
$config = new Config([
73+
'navigation' => [
74+
'default' => [],
75+
],
76+
]);
77+
78+
$services = new ServiceManager();
79+
$config->configureServiceManager($services);
80+
$helpers = new PluginManager($services);
81+
82+
$helper = $helpers->get('breadcrumbs');
83+
$this->assertInstanceOf(Breadcrumbs::class, $helper);
84+
$this->assertSame($services, $helper->getServiceLocator());
85+
}
6886
}

0 commit comments

Comments
 (0)