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

Commit 6768ba5

Browse files
committed
Added zend-view helper plugin manager injection
The zend-view container factory now also injects the HelperPluginManager instance, adding overrides for the url and serverurl helpers.
1 parent 7e0b635 commit 6768ba5

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

doc/book/container/factories.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,15 @@ the `TwigExtension` instance (assuming the router was found).
275275
- **FactoryName**: `Zend\Expressive\Container\Template\ZendViewFactory`
276276
- **Suggested Name**: `Zend\Expressive\Template\TemplateInterface`
277277
- **Requires**: no additional services are required.
278+
- `Zend\Expressive\Router\RouterInterface`, in order to inject the custom
279+
url helper implementation.
278280
- **Optional**:
279281
- `config`, an array or `ArrayAccess` instance. This will be used to further
280282
configure the `ZendView` instance, specifically with the layout template
281283
name, entries for a `TemplateMapResolver`, and and template paths to
282284
inject.
285+
- `Zend\View\HelperPluginManager`; if present, will be used to inject the
286+
`PhpRenderer` instance.
283287

284288
It consumes the following `config` structure:
285289

@@ -297,3 +301,9 @@ It consumes the following `config` structure:
297301
],
298302
]
299303
```
304+
305+
When creating the `PhpRenderer` instance, it will inject it with a
306+
`Zend\View\HelperPluginManager` instance (either pulled from the container, or
307+
instantiated directly). It injects the helper plugin manager with custom url and
308+
serverurl helpers, `Zend\Expressive\Template\ZendView\UrlHelper` and
309+
`Zend\Expressive\Template\ZendView\ServerUrlHelper`, respetively.

doc/book/template/zend-view.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ $content = $templates->render('blog/entry', [
145145
]);
146146
```
147147

148+
## Helpers
149+
150+
Expressive provides overrides of specific view helpers in order to better
151+
integrate with PSR-7. These include:
152+
153+
- `Zend\Expressive\Template\ZendView\UrlHelper`. This helper consumes the
154+
application's `Zend\Expressive\Router\RouterInterface` instance in order
155+
to generate URIs. It's signature is:
156+
`url($routeName, array $substitutions = [])`
157+
- `Zend\Expressive\Template\ZendView\ServerUrlHelper`. This helper consumes the
158+
URI from the application's request in order to provide fully qualified URIs.
159+
It's signature is: `serverUrl($path = null)`.
160+
161+
To use this particular helper, you will need to inject it with the request URI
162+
somewhere within your application:
163+
164+
```php
165+
$serverUrlHelper->setUri($request->getUri());
166+
```
167+
168+
We recommend doing this within a pre-pipeline middleware.
169+
148170
## Recommendations
149171

150172
We recommend the following practices when using the zend-view adapter:

src/Container/Template/ZendViewFactory.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@
1010
namespace Zend\Expressive\Container\Template;
1111

1212
use Interop\Container\ContainerInterface;
13+
use Zend\Expressive\Router\RouterInterface;
1314
use Zend\Expressive\Template\ZendView;
15+
use Zend\View\HelperPluginManager;
1416
use Zend\View\Renderer\PhpRenderer;
1517
use Zend\View\Resolver;
1618

1719
/**
1820
* Create and return a ZendView template instance.
1921
*
22+
* Requires the Zend\Expressive\Router\RouterInterface service (for creating
23+
* the UrlHelper instance).
24+
*
25+
* Optionally requires the Zend\View\HelperPluginManager service; if present,
26+
* will use the service to inject the PhpRenderer instance.
27+
*
2028
* Optionally uses the service 'config', which should return an array. This
2129
* factory consumes the following structure:
2230
*
@@ -34,6 +42,9 @@
3442
* ],
3543
* ]
3644
* </code>
45+
*
46+
* Injects the HelperPluginManager used by the PhpRenderer with zend-expressive
47+
* overrides of the url and serverurl helpers.
3748
*/
3849
class ZendViewFactory
3950
{
@@ -57,6 +68,9 @@ public function __invoke(ContainerInterface $container)
5768
$renderer = new PhpRenderer();
5869
$renderer->setResolver($resolver);
5970

71+
// Inject helpers
72+
$this->injectHelpers($renderer, $container);
73+
6074
// Inject renderer
6175
$view = new ZendView($renderer, isset($config['layout']) ? $config['layout'] : null);
6276

@@ -71,4 +85,29 @@ public function __invoke(ContainerInterface $container)
7185

7286
return $view;
7387
}
88+
89+
/**
90+
* Inject helpers into the PhpRenderer instance.
91+
*
92+
* If a HelperPluginManager instance is present in the container, uses that;
93+
* otherwise, instantiates one.
94+
*
95+
* In each case, injects with the custom url/serverurl implementations.
96+
*
97+
* @param PhpRenderer $renderer
98+
* @param ContainerInterface $container
99+
*/
100+
private function injectHelpers(PhpRenderer $renderer, ContainerInterface $container)
101+
{
102+
$helpers = $container->has(HelperPluginManager::class)
103+
? $container->get(HelperPluginManager::class)
104+
: new HelperPluginManager();
105+
106+
$helpers->setFactory('url', function () use ($container) {
107+
return new ZendView\UrlHelper($container->get(RouterInterface::class));
108+
});
109+
$helpers->setInvokableClass('serverurl', ZendView\ServerUrlHelper::class);
110+
111+
$renderer->setHelperPluginManager($helpers);
112+
}
74113
}

test/Container/Template/ZendViewFactoryTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
use PHPUnit_Framework_TestCase as TestCase;
1414
use ReflectionProperty;
1515
use Zend\Expressive\Container\Template\ZendViewFactory;
16+
use Zend\Expressive\Router\RouterInterface;
1617
use Zend\Expressive\Template\ZendView;
18+
use Zend\View\HelperPluginManager;
1719
use Zend\View\Model\ModelInterface;
1820
use Zend\View\Resolver\AggregateResolver;
1921
use Zend\View\Resolver\TemplateMapResolver;
@@ -37,6 +39,7 @@ public function fetchPhpRenderer(ZendView $view)
3739
public function testCallingFactoryWithNoConfigReturnsZendViewInstance()
3840
{
3941
$this->container->has('config')->willReturn(false);
42+
$this->container->has(HelperPluginManager::class)->willReturn(false);
4043
$factory = new ZendViewFactory();
4144
$view = $factory($this->container->reveal());
4245
$this->assertInstanceOf(ZendView::class, $view);
@@ -62,6 +65,7 @@ public function testConfiguresLayout()
6265
];
6366
$this->container->has('config')->willReturn(true);
6467
$this->container->get('config')->willReturn($config);
68+
$this->container->has(HelperPluginManager::class)->willReturn(false);
6569
$factory = new ZendViewFactory();
6670
$view = $factory($this->container->reveal());
6771

@@ -81,6 +85,7 @@ public function testConfiguresPaths()
8185
];
8286
$this->container->has('config')->willReturn(true);
8387
$this->container->get('config')->willReturn($config);
88+
$this->container->has(HelperPluginManager::class)->willReturn(false);
8489
$factory = new ZendViewFactory();
8590
$view = $factory($this->container->reveal());
8691

@@ -113,6 +118,7 @@ public function testConfiguresTemplateMap()
113118
];
114119
$this->container->has('config')->willReturn(true);
115120
$this->container->get('config')->willReturn($config);
121+
$this->container->has(HelperPluginManager::class)->willReturn(false);
116122
$factory = new ZendViewFactory();
117123
$view = $factory($this->container->reveal());
118124

@@ -133,4 +139,54 @@ public function testConfiguresTemplateMap()
133139
$this->assertTrue($resolver->has('bar'));
134140
$this->assertEquals('baz', $resolver->get('bar'));
135141
}
142+
143+
public function testInjectsCustomHelpersIntoHelperManager()
144+
{
145+
$router = $this->prophesize(RouterInterface::class)->reveal();
146+
$this->container->has('config')->willReturn(false);
147+
$this->container->has(HelperPluginManager::class)->willReturn(false);
148+
$this->container->has(RouterInterface::class)->willReturn(true);
149+
$this->container->get(RouterInterface::class)->willReturn($router);
150+
$factory = new ZendViewFactory();
151+
$view = $factory($this->container->reveal());
152+
$this->assertInstanceOf(ZendView::class, $view);
153+
154+
$renderer = $this->fetchPhpRenderer($view);
155+
$helpers = $renderer->getHelperPluginManager();
156+
$this->assertInstanceOf(HelperPluginManager::class, $helpers);
157+
$this->assertTrue($helpers->has('url'));
158+
$this->assertTrue($helpers->has('serverurl'));
159+
$this->assertInstanceOf(ZendView\UrlHelper::class, $helpers->get('url'));
160+
$this->assertInstanceOf(ZendView\ServerUrlHelper::class, $helpers->get('serverurl'));
161+
}
162+
163+
public function testWillUseHelperManagerFromContainer()
164+
{
165+
$router = $this->prophesize(RouterInterface::class)->reveal();
166+
$this->container->has('config')->willReturn(false);
167+
$this->container->has(RouterInterface::class)->willReturn(true);
168+
$this->container->get(RouterInterface::class)->willReturn($router);
169+
170+
$helpers = new HelperPluginManager();
171+
$this->container->has(HelperPluginManager::class)->willReturn(true);
172+
$this->container->get(HelperPluginManager::class)->willReturn($helpers);
173+
$factory = new ZendViewFactory();
174+
$view = $factory($this->container->reveal());
175+
$this->assertInstanceOf(ZendView::class, $view);
176+
177+
$renderer = $this->fetchPhpRenderer($view);
178+
$this->assertSame($helpers, $renderer->getHelperPluginManager());
179+
return $helpers;
180+
}
181+
182+
/**
183+
* @depends testWillUseHelperManagerFromContainer
184+
*/
185+
public function testInjectsCustomHelpersIntoHelperManagerFromContainer(HelperPluginManager $helpers)
186+
{
187+
$this->assertTrue($helpers->has('url'));
188+
$this->assertTrue($helpers->has('serverurl'));
189+
$this->assertInstanceOf(ZendView\UrlHelper::class, $helpers->get('url'));
190+
$this->assertInstanceOf(ZendView\ServerUrlHelper::class, $helpers->get('serverurl'));
191+
}
136192
}

0 commit comments

Comments
 (0)