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

Commit 50ccd44

Browse files
committed
Ensure injectTranslator returns early if no parent container found
Adds a test for a condition reported by @webimpress, detailing expected behavior if the plugin manager does not have a parent locator when `injectTranslator()` is called (should return null, not error), and adds a conditional into `injectTranslator()` to return early if no container is available.
1 parent 263aba6 commit 50ccd44

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

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;

test/HelperPluginManagerTest.php

Lines changed: 12 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,17 @@ public function testIfHelperIsTranslatorAwareAndBothMvcTranslatorAndTranslatorAr
175176
$this->assertSame($translator, $helper->getTranslator());
176177
}
177178

179+
/**
180+
* @group 47
181+
*/
182+
public function testInjectTranslatorWillReturnEarlyIfThePluginManagerDoesNotHaveAParentContainer()
183+
{
184+
$helpers = new HelperPluginManager();
185+
$helper = new HeadTitle();
186+
$this->assertNull($helpers->injectTranslator($helper, $helpers));
187+
$this->assertNull($helper->getTranslator());
188+
}
189+
178190
public function testCanOverrideAFactoryViaConfigurationPassedToConstructor()
179191
{
180192
$helper = $this->prophesize(HelperInterface::class)->reveal();

0 commit comments

Comments
 (0)