|
9 | 9 |
|
10 | 10 | namespace Zend\Expressive\ZendView;
|
11 | 11 |
|
| 12 | +use Interop\Container\ContainerInterface as InteropContainerInterface; |
12 | 13 | use Psr\Container\ContainerInterface;
|
13 | 14 | use Zend\Expressive\Helper\ServerUrlHelper as BaseServerUrlHelper;
|
14 | 15 | use Zend\Expressive\Helper\UrlHelper as BaseUrlHelper;
|
@@ -92,14 +93,13 @@ public function __invoke(ContainerInterface $container) : ZendViewRenderer
|
92 | 93 | *
|
93 | 94 | * In each case, injects with the custom url/serverurl implementations.
|
94 | 95 | *
|
| 96 | + * @throws Exception\InvalidContainerException if the $container argument |
| 97 | + * does not implement InteropContainerInterface. |
95 | 98 | * @throws Exception\MissingHelperException
|
96 | 99 | */
|
97 | 100 | private function injectHelpers(PhpRenderer $renderer, ContainerInterface $container) : void
|
98 | 101 | {
|
99 |
| - $helpers = $container->has(HelperPluginManager::class) |
100 |
| - ? $container->get(HelperPluginManager::class) |
101 |
| - : new HelperPluginManager($container); |
102 |
| - |
| 102 | + $helpers = $this->retrieveHelperManager($container); |
103 | 103 | $helpers->setAlias('url', BaseUrlHelper::class);
|
104 | 104 | $helpers->setAlias('Url', BaseUrlHelper::class);
|
105 | 105 | $helpers->setFactory(BaseUrlHelper::class, function () use ($container) {
|
@@ -127,4 +127,29 @@ private function injectHelpers(PhpRenderer $renderer, ContainerInterface $contai
|
127 | 127 |
|
128 | 128 | $renderer->setHelperPluginManager($helpers);
|
129 | 129 | }
|
| 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 | + } |
130 | 155 | }
|
0 commit comments