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

Commit

Permalink
Merge branch 'hotfix/navigation-plugin-manager'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Feb 18, 2016
2 parents 490d5d8 + a1228db commit 5ada030
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 17 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## 2.6.2 - TBD
## 2.6.2 - 2016-02-18

### Added

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

### Fixed

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

## 2.6.1 - 2016-02-18

Expand Down
18 changes: 13 additions & 5 deletions src/Helper/Navigation/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,30 @@ class PluginManager extends HelperPluginManager
Links::class => InvokableFactory::class,
Menu::class => InvokableFactory::class,
Sitemap::class => InvokableFactory::class,

// v2 canonical FQCNs

'zendviewhelpernavigationbreadcrumbs' => InvokableFactory::class,
'zendviewhelpernavigationlinks' => InvokableFactory::class,
'zendviewhelpernavigationmenu' => InvokableFactory::class,
'zendviewhelpernavigationsitemap' => InvokableFactory::class,
];

/**
* @param ContainerInterface $container
* @param array $config
* @param null|ConfigInterface|ContainerInterface $configOrContainerInstance
* @param array $v3config If $configOrContainerInstance is a container, this
* value will be passed to the parent constructor.
*/
public function __construct(ContainerInterface $container, array $config = [])
public function __construct($configOrContainerInstance = null, array $v3config = [])
{
$this->initializers[] = function ($container, $instance) {
if (! $instance instanceof AbstractHelper) {
continue;
return;
}

$instance->setServiceLocator($container);
};

parent::__construct($container, $config);
parent::__construct($configOrContainerInstance, $v3config);
}
}
33 changes: 24 additions & 9 deletions src/HelperPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,18 @@ public function getRenderer()
/**
* Inject a helper instance with the registered renderer
*
* @param $first
* @param $second
* @param ContainerInterface|Helper\HelperInterface $first helper instance
* under zend-servicemanager v2, ContainerInterface under v3.
* @param ContainerInterface|Helper\HelperInterface $second
* ContainerInterface under zend-servicemanager v3, helper instance
* under v2. Ignored regardless.
*/
public function injectRenderer($first, $second)
{
if ($first instanceof ContainerInterface) {
$helper = $second;
} else {
$helper = $first;
}
$helper = ($first instanceof ContainerInterface)
? $second
: $first;

$renderer = $this->getRenderer();
if (null === $renderer) {
return;
Expand All @@ -293,22 +295,35 @@ public function injectRenderer($first, $second)
/**
* Inject a helper instance with the registered translator
*
* @param $first
* @param $second
* @param ContainerInterface|Helper\HelperInterface $first helper instance
* under zend-servicemanager v2, ContainerInterface under v3.
* @param ContainerInterface|Helper\HelperInterface $second
* ContainerInterface under zend-servicemanager v3, helper instance
* under v2. Ignored regardless.
*/
public function injectTranslator($first, $second)
{
if ($first instanceof ContainerInterface) {
// v3 usage
$container = $first;
$helper = $second;
} else {
// v2 usage; grab the parent container
$container = $second->getServiceLocator();
$helper = $first;
}

if (! $helper instanceof TranslatorAwareInterface) {
return;
}

if (! $container) {
// Under zend-navigation v2.5, the navigation PluginManager is
// always lazy-loaded, which means it never has a parent
// container.
return;
}

if ($container->has('MvcTranslator')) {
$helper->setTranslator($container->get('MvcTranslator'));
return;
Expand Down
68 changes: 68 additions & 0 deletions test/Helper/Navigation/PluginManagerCompatibilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\View\Helper\Navigation;

use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\Test\CommonPluginManagerTrait;
use Zend\View\Exception\InvalidHelperException;
use Zend\View\Helper\Navigation\AbstractHelper;
use Zend\View\Helper\Navigation\PluginManager;

/**
* @group Zend_View
*/
class PluginManagerCompatibilityTest extends \PHPUnit_Framework_TestCase
{
use CommonPluginManagerTrait;

protected function getPluginManager()
{
return new PluginManager(new ServiceManager());
}

protected function getV2InvalidPluginException()
{
return InvalidHelperException::class;
}

protected function getInstanceOf()
{
return AbstractHelper::class;
}

/**
* @group 43
*/
public function testConstructorArgumentsAreOptionalUnderV2()
{
$helpers = $this->getPluginManager();
if (method_exists($helpers, 'configure')) {
$this->markTestSkipped('zend-servicemanager v3 plugin managers require a container argument');
}

$helpers = new PluginManager();
$this->assertInstanceOf(PluginManager::class, $helpers);
}

/**
* @group 43
*/
public function testConstructorAllowsConfigInstanceAsFirstArgumentUnderV2()
{
$helpers = $this->getPluginManager();
if (method_exists($helpers, 'configure')) {
$this->markTestSkipped('zend-servicemanager v3 plugin managers require a container argument');
}

$helpers = new PluginManager(new Config([]));
$this->assertInstanceOf(PluginManager::class, $helpers);
}
}
2 changes: 1 addition & 1 deletion test/HelperPluginManagerCompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\Test\CommonPluginManagerTrait;

class PluginManagerCompatibilityTest extends TestCase
class HelperPluginManagerCompatibilityTest extends TestCase
{
use CommonPluginManagerTrait;

Expand Down
18 changes: 18 additions & 0 deletions test/HelperPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Zend\ServiceManager\ServiceManager;
use Zend\View\Exception\InvalidHelperException;
use Zend\View\HelperPluginManager;
use Zend\View\Helper\HeadTitle;
use Zend\View\Helper\HelperInterface;
use Zend\View\Helper\Url;
use Zend\View\Renderer\PhpRenderer;
Expand Down Expand Up @@ -175,6 +176,23 @@ public function testIfHelperIsTranslatorAwareAndBothMvcTranslatorAndTranslatorAr
$this->assertSame($translator, $helper->getTranslator());
}

/**
* @group 47
*/
public function testInjectTranslatorWillReturnEarlyIfThePluginManagerDoesNotHaveAParentContainer()
{
if (method_exists($this->helpers, 'configure')) {
$this->markTestSkipped(
'Skip test when testing against zend-servicemanager v3, as that implementation '
. 'guarantees a parent container in plugin managers'
);
}
$helpers = new HelperPluginManager();
$helper = new HeadTitle();
$this->assertNull($helpers->injectTranslator($helper, $helpers));
$this->assertNull($helper->getTranslator());
}

public function testCanOverrideAFactoryViaConfigurationPassedToConstructor()
{
$helper = $this->prophesize(HelperInterface::class)->reveal();
Expand Down

0 comments on commit 5ada030

Please sign in to comment.