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

Commit 5ada030

Browse files
committed
Merge branch 'hotfix/navigation-plugin-manager'
Close #47 Fixes #46 Fixes #45 Fixes zendframework/zend-navigation#19
2 parents 490d5d8 + a1228db commit 5ada030

File tree

6 files changed

+134
-17
lines changed

6 files changed

+134
-17
lines changed

CHANGELOG.md

Lines changed: 10 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.2 - TBD
5+
## 2.6.2 - 2016-02-18
66

77
### Added
88

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

1919
### Fixed
2020

21-
- Nothing.
21+
- [#47](https://github.com/zendframework/zend-view/pull/47) fixes
22+
`Navigation\PluginManager` to ensure it is backwards compatible
23+
with zend-servicemanager v2, including:
24+
- fixing the constructor to be BC with v2 and forwards-compatible with v3.
25+
- adding additional, normalized alias/factory pairs.
26+
- [#47](https://github.com/zendframework/zend-view/pull/47) fixes
27+
the behavior of `HelperPluginManager::injectTranslator()` to return
28+
early if no container is provided (fixing an issue with navigation
29+
helpers introduced in 2.6.0).
2230

2331
## 2.6.1 - 2016-02-18
2432

src/Helper/Navigation/PluginManager.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,30 @@ class PluginManager extends HelperPluginManager
4949
Links::class => InvokableFactory::class,
5050
Menu::class => InvokableFactory::class,
5151
Sitemap::class => InvokableFactory::class,
52+
53+
// v2 canonical FQCNs
54+
55+
'zendviewhelpernavigationbreadcrumbs' => InvokableFactory::class,
56+
'zendviewhelpernavigationlinks' => InvokableFactory::class,
57+
'zendviewhelpernavigationmenu' => InvokableFactory::class,
58+
'zendviewhelpernavigationsitemap' => InvokableFactory::class,
5259
];
5360

5461
/**
55-
* @param ContainerInterface $container
56-
* @param array $config
62+
* @param null|ConfigInterface|ContainerInterface $configOrContainerInstance
63+
* @param array $v3config If $configOrContainerInstance is a container, this
64+
* value will be passed to the parent constructor.
5765
*/
58-
public function __construct(ContainerInterface $container, array $config = [])
66+
public function __construct($configOrContainerInstance = null, array $v3config = [])
5967
{
6068
$this->initializers[] = function ($container, $instance) {
6169
if (! $instance instanceof AbstractHelper) {
62-
continue;
70+
return;
6371
}
6472

6573
$instance->setServiceLocator($container);
6674
};
6775

68-
parent::__construct($container, $config);
76+
parent::__construct($configOrContainerInstance, $v3config);
6977
}
7078
}

src/HelperPluginManager.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,18 @@ public function getRenderer()
273273
/**
274274
* Inject a helper instance with the registered renderer
275275
*
276-
* @param $first
277-
* @param $second
276+
* @param ContainerInterface|Helper\HelperInterface $first helper instance
277+
* under zend-servicemanager v2, ContainerInterface under v3.
278+
* @param ContainerInterface|Helper\HelperInterface $second
279+
* ContainerInterface under zend-servicemanager v3, helper instance
280+
* under v2. Ignored regardless.
278281
*/
279282
public function injectRenderer($first, $second)
280283
{
281-
if ($first instanceof ContainerInterface) {
282-
$helper = $second;
283-
} else {
284-
$helper = $first;
285-
}
284+
$helper = ($first instanceof ContainerInterface)
285+
? $second
286+
: $first;
287+
286288
$renderer = $this->getRenderer();
287289
if (null === $renderer) {
288290
return;
@@ -293,22 +295,35 @@ public function injectRenderer($first, $second)
293295
/**
294296
* Inject a helper instance with the registered translator
295297
*
296-
* @param $first
297-
* @param $second
298+
* @param ContainerInterface|Helper\HelperInterface $first helper instance
299+
* under zend-servicemanager v2, ContainerInterface under v3.
300+
* @param ContainerInterface|Helper\HelperInterface $second
301+
* ContainerInterface under zend-servicemanager v3, helper instance
302+
* under v2. Ignored regardless.
298303
*/
299304
public function injectTranslator($first, $second)
300305
{
301306
if ($first instanceof ContainerInterface) {
307+
// v3 usage
302308
$container = $first;
303309
$helper = $second;
304310
} else {
311+
// v2 usage; grab the parent container
305312
$container = $second->getServiceLocator();
306313
$helper = $first;
307314
}
315+
308316
if (! $helper instanceof TranslatorAwareInterface) {
309317
return;
310318
}
311319

320+
if (! $container) {
321+
// Under zend-navigation v2.5, the navigation PluginManager is
322+
// always lazy-loaded, which means it never has a parent
323+
// container.
324+
return;
325+
}
326+
312327
if ($container->has('MvcTranslator')) {
313328
$helper->setTranslator($container->get('MvcTranslator'));
314329
return;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\View\Helper\Navigation;
11+
12+
use Zend\ServiceManager\Config;
13+
use Zend\ServiceManager\ServiceManager;
14+
use Zend\ServiceManager\Test\CommonPluginManagerTrait;
15+
use Zend\View\Exception\InvalidHelperException;
16+
use Zend\View\Helper\Navigation\AbstractHelper;
17+
use Zend\View\Helper\Navigation\PluginManager;
18+
19+
/**
20+
* @group Zend_View
21+
*/
22+
class PluginManagerCompatibilityTest extends \PHPUnit_Framework_TestCase
23+
{
24+
use CommonPluginManagerTrait;
25+
26+
protected function getPluginManager()
27+
{
28+
return new PluginManager(new ServiceManager());
29+
}
30+
31+
protected function getV2InvalidPluginException()
32+
{
33+
return InvalidHelperException::class;
34+
}
35+
36+
protected function getInstanceOf()
37+
{
38+
return AbstractHelper::class;
39+
}
40+
41+
/**
42+
* @group 43
43+
*/
44+
public function testConstructorArgumentsAreOptionalUnderV2()
45+
{
46+
$helpers = $this->getPluginManager();
47+
if (method_exists($helpers, 'configure')) {
48+
$this->markTestSkipped('zend-servicemanager v3 plugin managers require a container argument');
49+
}
50+
51+
$helpers = new PluginManager();
52+
$this->assertInstanceOf(PluginManager::class, $helpers);
53+
}
54+
55+
/**
56+
* @group 43
57+
*/
58+
public function testConstructorAllowsConfigInstanceAsFirstArgumentUnderV2()
59+
{
60+
$helpers = $this->getPluginManager();
61+
if (method_exists($helpers, 'configure')) {
62+
$this->markTestSkipped('zend-servicemanager v3 plugin managers require a container argument');
63+
}
64+
65+
$helpers = new PluginManager(new Config([]));
66+
$this->assertInstanceOf(PluginManager::class, $helpers);
67+
}
68+
}

test/HelperPluginManagerCompatibilityTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Zend\ServiceManager\ServiceManager;
2121
use Zend\ServiceManager\Test\CommonPluginManagerTrait;
2222

23-
class PluginManagerCompatibilityTest extends TestCase
23+
class HelperPluginManagerCompatibilityTest extends TestCase
2424
{
2525
use CommonPluginManagerTrait;
2626

test/HelperPluginManagerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Zend\ServiceManager\ServiceManager;
1717
use Zend\View\Exception\InvalidHelperException;
1818
use Zend\View\HelperPluginManager;
19+
use Zend\View\Helper\HeadTitle;
1920
use Zend\View\Helper\HelperInterface;
2021
use Zend\View\Helper\Url;
2122
use Zend\View\Renderer\PhpRenderer;
@@ -175,6 +176,23 @@ public function testIfHelperIsTranslatorAwareAndBothMvcTranslatorAndTranslatorAr
175176
$this->assertSame($translator, $helper->getTranslator());
176177
}
177178

179+
/**
180+
* @group 47
181+
*/
182+
public function testInjectTranslatorWillReturnEarlyIfThePluginManagerDoesNotHaveAParentContainer()
183+
{
184+
if (method_exists($this->helpers, 'configure')) {
185+
$this->markTestSkipped(
186+
'Skip test when testing against zend-servicemanager v3, as that implementation '
187+
. 'guarantees a parent container in plugin managers'
188+
);
189+
}
190+
$helpers = new HelperPluginManager();
191+
$helper = new HeadTitle();
192+
$this->assertNull($helpers->injectTranslator($helper, $helpers));
193+
$this->assertNull($helper->getTranslator());
194+
}
195+
178196
public function testCanOverrideAFactoryViaConfigurationPassedToConstructor()
179197
{
180198
$helper = $this->prophesize(HelperInterface::class)->reveal();

0 commit comments

Comments
 (0)