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

Commit 7eb61df

Browse files
committed
Raise exception when creating HelperPluginManager if container is invalid
zend-servicemanager currently typehints against container-interop, which means that if we have a container that does not implement container-interop, we need to raise an exception when attempting to create a `HelperPluginManager` instance.
1 parent e857583 commit 7eb61df

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-expressive-zendviewrenderer for the canonical source repository
4+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-expressive-zendviewrenderer/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Zend\Expressive\ZendView\Exception;
11+
12+
use RuntimeException;
13+
14+
class InvalidContainerException extends RuntimeException implements ExceptionInterface
15+
{
16+
}

src/ZendViewRendererFactory.php

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

1010
namespace Zend\Expressive\ZendView;
1111

12+
use Interop\Container\ContainerInterface as InteropContainerInterface;
1213
use Psr\Container\ContainerInterface;
1314
use Zend\Expressive\Helper\ServerUrlHelper as BaseServerUrlHelper;
1415
use Zend\Expressive\Helper\UrlHelper as BaseUrlHelper;
@@ -92,14 +93,13 @@ public function __invoke(ContainerInterface $container) : ZendViewRenderer
9293
*
9394
* In each case, injects with the custom url/serverurl implementations.
9495
*
96+
* @throws Exception\InvalidContainerException if the $container argument
97+
* does not implement InteropContainerInterface.
9598
* @throws Exception\MissingHelperException
9699
*/
97100
private function injectHelpers(PhpRenderer $renderer, ContainerInterface $container) : void
98101
{
99-
$helpers = $container->has(HelperPluginManager::class)
100-
? $container->get(HelperPluginManager::class)
101-
: new HelperPluginManager($container);
102-
102+
$helpers = $this->retrieveHelperManager($container);
103103
$helpers->setAlias('url', BaseUrlHelper::class);
104104
$helpers->setAlias('Url', BaseUrlHelper::class);
105105
$helpers->setFactory(BaseUrlHelper::class, function () use ($container) {
@@ -127,4 +127,29 @@ private function injectHelpers(PhpRenderer $renderer, ContainerInterface $contai
127127

128128
$renderer->setHelperPluginManager($helpers);
129129
}
130+
131+
/**
132+
* @throws Exception\InvalidContainerException if the $container argument
133+
* does not implement InteropContainerInterface.
134+
*/
135+
private function retrieveHelperManager(ContainerInterface $container) : HelperPluginManager
136+
{
137+
if ($container->has(HelperPluginManager::class)) {
138+
return $container->get(HelperPluginManager::class);
139+
}
140+
141+
if (! $container instanceof InteropContainerInterface) {
142+
throw new Exception\InvalidContainerException(sprintf(
143+
'%s expects a %s instance to its constructor; however, your service'
144+
. ' container is an instance of %s, which does not implement that'
145+
. ' interface. Consider switching to zend-servicemanager for your'
146+
. ' container implementation if you wish to use the zend-view renderer.',
147+
HelperPluginManager::class,
148+
InteropContainerInterface::class,
149+
get_class($container)
150+
));
151+
}
152+
153+
return new HelperPluginManager($container);
154+
}
130155
}

test/ZendViewRendererFactoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
use PHPUnit\Framework\TestCase;
1414
use Prophecy\Prophecy\ObjectProphecy;
1515
use Prophecy\Prophecy\ProphecyInterface;
16+
use Psr\Container\ContainerInterface as PsrContainerInterface;
1617
use ReflectionProperty;
1718
use Zend\Expressive\Helper;
1819
use Zend\Expressive\Template\TemplatePath;
20+
use Zend\Expressive\ZendView\Exception\InvalidContainerException;
1921
use Zend\Expressive\ZendView\ServerUrlHelper;
2022
use Zend\Expressive\ZendView\UrlHelper;
2123
use Zend\Expressive\ZendView\ZendViewRenderer;
@@ -304,4 +306,19 @@ public function testWillUseRendererFromContainer()
304306
$composed = $this->fetchPhpRenderer($view);
305307
$this->assertSame($engine, $composed);
306308
}
309+
310+
public function testWillRaiseExceptionIfContainerDoesNotImplementInteropContainerInterface()
311+
{
312+
$container = $this->prophesize(PsrContainerInterface::class);
313+
$container->has('config')->willReturn(false);
314+
$container->get('config')->shouldNotBeCalled();
315+
$container->has(PhpRenderer::class)->willReturn(false);
316+
$container->get(PhpRenderer::class)->shouldNotBeCalled();
317+
$container->has(HelperPluginManager::class)->willReturn(false);
318+
319+
$factory = new ZendViewRendererFactory();
320+
321+
$this->expectException(InvalidContainerException::class);
322+
$factory($container->reveal());
323+
}
307324
}

0 commit comments

Comments
 (0)