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

Commit 057d2b8

Browse files
committed
Create delegator factory for registering UrlHelper as observer
This patch does the following: - Creates a UrlHelperFactory for creating the UrlHelper, to register under the service name `Zend\Expressive\ZendView\UrlHelper` in the application container. (This simplifies usage of the delegator factory). - Updates the ZendViewRendererFactory to test for, and pull when present, the `Zend\Expressive\ZendView\UrlHelper` to create the URL helper. - Provides `ApplicationUrlDelegatorFactory` for injecting the `UrlHelper` as a RouteResult observer to the application instance; to do this, it checks to see if the `Zend\Expressive\ZendView\UrlHelper` is present, and, if so, uses that instance. (This minimizes the number of objects present in the dependency graph for cases when no template renderer is required.)
1 parent 7742df7 commit 057d2b8

5 files changed

+126
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace Zend\Expressive\ZendView;
11+
12+
use Zend\ServiceManager\DelegatorFactoryInterface;
13+
use Zend\ServiceManager\ServiceLocatorInterface;
14+
15+
class ApplicationUrlDelegatorFactory implements DelegatorFactoryInterface
16+
{
17+
/**
18+
* Inject the UrlHelper in an Application instance, when available.
19+
*
20+
* @param ServiceLocatorInterface $container
21+
* @param string $name
22+
* @param string $requestedName
23+
* @param callable $callback Callback that returns the Application instance
24+
*/
25+
public function createDelegatorWithName(ServiceLocatorInterface $container, $name, $requestedName, $callback)
26+
{
27+
$application = $callback();
28+
if ($container->has(UrlHelper::class)) {
29+
$application->attachRouteResultObserver($container->get(UrlHelper::class));
30+
}
31+
return $application;
32+
}
33+
}

src/UrlHelperFactory.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace Zend\Expressive\ZendView;
11+
12+
use Interop\Container\ContainerInterface;
13+
use Zend\Expressive\Router\RouterInterface;
14+
15+
class UrlHelperFactory
16+
{
17+
/**
18+
* Create a UrlHelper instance.
19+
*
20+
* @param ContainerInterface $container
21+
* @return UrlHelper
22+
*/
23+
public function __invoke(ContainerInterface $container)
24+
{
25+
return new UrlHelper($container->get(RouterInterface::class));
26+
}
27+
}

src/ZendViewRendererFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ private function injectHelpers(PhpRenderer $renderer, ContainerInterface $contai
103103
: new HelperPluginManager();
104104

105105
$helpers->setFactory('url', function () use ($container) {
106+
if ($container->has(UrlHelper::class)) {
107+
return $container->get(UrlHelper::class);
108+
}
106109
return new UrlHelper($container->get(RouterInterface::class));
107110
});
108111
$helpers->setInvokableClass('serverurl', ServerUrlHelper::class);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace ZendTest\Expressive\ZendView;
11+
12+
use PHPUnit_Framework_TestCase as TestCase;
13+
use Zend\Expressive\Application;
14+
use Zend\Expressive\ZendView\ApplicationUrlDelegatorFactory;
15+
use Zend\Expressive\ZendView\UrlHelper;
16+
use Zend\ServiceManager\ServiceLocatorInterface;
17+
18+
class ApplicationUrlDelegatorFactoryTest extends TestCase
19+
{
20+
public function testDelegatorRegistersUrlHelperAsRouteResultObserverWithApplication()
21+
{
22+
$urlHelper = $this->prophesize(UrlHelper::class);
23+
$application = $this->prophesize(Application::class);
24+
$application->attachRouteResultObserver($urlHelper->reveal())->shouldBeCalled();
25+
$applicationCallback = function () use ($application) {
26+
return $application->reveal();
27+
};
28+
29+
$container = $this->prophesize(ServiceLocatorInterface::class);
30+
$container->has(UrlHelper::class)->willReturn(true);
31+
$container->get(UrlHelper::class)->willReturn($urlHelper->reveal());
32+
33+
$delegator = new ApplicationUrlDelegatorFactory();
34+
$test = $delegator->createDelegatorWithName(
35+
$container->reveal(),
36+
Application::class,
37+
Application::class,
38+
$applicationCallback
39+
);
40+
$this->assertSame($application->reveal(), $test);
41+
}
42+
}

test/ZendViewRendererFactoryTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public function testInjectsCustomHelpersIntoHelperManager()
210210
$router = $this->prophesize(RouterInterface::class)->reveal();
211211
$this->container->has('config')->willReturn(false);
212212
$this->container->has(HelperPluginManager::class)->willReturn(false);
213+
$this->container->has(UrlHelper::class)->willReturn(false);
213214
$this->container->has(RouterInterface::class)->willReturn(true);
214215
$this->container->get(RouterInterface::class)->willReturn($router);
215216
$factory = new ZendViewRendererFactory();
@@ -231,6 +232,7 @@ public function testWillUseHelperManagerFromContainer()
231232
$this->container->has('config')->willReturn(false);
232233
$this->container->has(RouterInterface::class)->willReturn(true);
233234
$this->container->get(RouterInterface::class)->willReturn($router);
235+
$this->container->has(UrlHelper::class)->willReturn(false);
234236

235237
$helpers = new HelperPluginManager();
236238
$this->container->has(HelperPluginManager::class)->willReturn(true);
@@ -254,4 +256,23 @@ public function testInjectsCustomHelpersIntoHelperManagerFromContainer(HelperPlu
254256
$this->assertInstanceOf(UrlHelper::class, $helpers->get('url'));
255257
$this->assertInstanceOf(ServerUrlHelper::class, $helpers->get('serverurl'));
256258
}
259+
260+
public function testWillUseUrlHelperFromContainerWhenAvailable()
261+
{
262+
$urlHelper = $this->prophesize(UrlHelper::class)->reveal();
263+
$router = $this->prophesize(RouterInterface::class)->reveal();
264+
$this->container->has('config')->willReturn(false);
265+
$this->container->has(HelperPluginManager::class)->willReturn(false);
266+
$this->container->has(UrlHelper::class)->willReturn(true);
267+
$this->container->get(UrlHelper::class)->willReturn($urlHelper);
268+
$this->container->has(RouterInterface::class)->willReturn(true);
269+
$this->container->get(RouterInterface::class)->willReturn($router);
270+
$factory = new ZendViewRendererFactory();
271+
$view = $factory($this->container->reveal());
272+
273+
$renderer = $this->fetchPhpRenderer($view);
274+
$helpers = $renderer->getHelperPluginManager();
275+
$this->assertTrue($helpers->has('url'));
276+
$this->assertSame($urlHelper, $helpers->get('url'));
277+
}
257278
}

0 commit comments

Comments
 (0)