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

Commit 825c952

Browse files
committed
Merge branch 'hotfix/routeresult-observer'
Close #9
2 parents 8bd425b + f48f30c commit 825c952

9 files changed

+168
-2
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ All notable changes to this project will be documented in this file, in reverse
88

99
- [#4](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/)
1010
Allow rendering view models via render
11+
- [#9](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/)
12+
updates `UrlHelper` to implement `Zend\Expressive\Template\RouteResultObserverInterface`,
13+
and the `update()` method it defines. This allows it to observer the
14+
application for the `RouteResult` and store it for later URI generation.
15+
To accomplish this, the following additional changes were made:
16+
- `Zend\Expressive\ZendView\UrlHelperFactory` was added, for creating the
17+
`UrlHelper` instance. This should be registered with the application service
18+
container.
19+
- `Zend\Expressive\ZendView\ZendViewRendererFactory` was updated to look for
20+
the `Zend\Expressive\ZendView\UrlHelper` service in the application service
21+
container, and use it to seed the `HelperManager` when available.
22+
- `Zend\Expressive\ZendView\ApplicationUrlDelegatorFactory` was created; when
23+
registered as a delegator factory with the `Zend\Expressive\Application`
24+
service, it will pull the `UrlHelper` and attach it as a route result
25+
observer to the `Application` instance. Documentation was also provided for
26+
creating a Pimple extension for accomplishing this.
1127

1228
### Deprecated
1329

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"require": {
2020
"php": ">=5.5",
2121
"container-interop/container-interop": "^1.1",
22-
"zendframework/zend-expressive": "^1.0@rc",
22+
"zendframework/zend-expressive": "~1.0.0-dev@dev",
2323
"zendframework/zend-filter": "^2.5",
2424
"zendframework/zend-i18n": "^2.5",
2525
"zendframework/zend-servicemanager": "^2.5",
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/UrlHelper.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
use Zend\Expressive\Exception;
1111
use Zend\Expressive\Router\RouterInterface;
1212
use Zend\Expressive\Router\RouteResult;
13+
use Zend\Expressive\Router\RouteResultObserverInterface;
1314
use Zend\View\Helper\AbstractHelper;
1415

15-
class UrlHelper extends AbstractHelper
16+
class UrlHelper extends AbstractHelper implements RouteResultObserverInterface
1617
{
1718
/**
1819
* @var RouteResult
@@ -62,6 +63,14 @@ public function __invoke($route = null, $params = [])
6263
return $this->router->generateUri($route, $params);
6364
}
6465

66+
/**
67+
* {@inheritDoc}
68+
*/
69+
public function update(RouteResult $result)
70+
{
71+
$this->result = $result;
72+
}
73+
6574
/**
6675
* @param RouteResult $result
6776
*/

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/UrlHelperTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Zend\Expressive\Exception;
1515
use Zend\Expressive\Router\RouterInterface;
1616
use Zend\Expressive\Router\RouteResult;
17+
use Zend\Expressive\Router\RouteResultObserverInterface;
1718
use Zend\Expressive\ZendView\UrlHelper;
1819

1920
class UrlHelperTest extends TestCase
@@ -134,4 +135,18 @@ public function testProvidedParametersOverrideAnyPresentInARouteResultWhenGenera
134135

135136
$this->assertEquals('URL', $helper('resource', ['id' => 2]));
136137
}
138+
139+
public function testIsARouteResultObserver()
140+
{
141+
$helper = $this->createHelper();
142+
$this->assertInstanceOf(RouteResultObserverInterface::class, $helper);
143+
}
144+
145+
public function testUpdateMethodSetsRouteResultProperty()
146+
{
147+
$result = $this->prophesize(RouteResult::class);
148+
$helper = $this->createHelper();
149+
$helper->update($result->reveal());
150+
$this->assertAttributeSame($result->reveal(), 'result', $helper);
151+
}
137152
}

test/ZendViewRendererFactoryTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public function testInjectsCustomHelpersIntoHelperManager()
213213
$router = $this->prophesize(RouterInterface::class)->reveal();
214214
$this->container->has('config')->willReturn(false);
215215
$this->container->has(HelperPluginManager::class)->willReturn(false);
216+
$this->container->has(UrlHelper::class)->willReturn(false);
216217
$this->container->has(RouterInterface::class)->willReturn(true);
217218
$this->container->get(RouterInterface::class)->willReturn($router);
218219
$factory = new ZendViewRendererFactory();
@@ -234,6 +235,7 @@ public function testWillUseHelperManagerFromContainer()
234235
$this->container->has('config')->willReturn(false);
235236
$this->container->has(RouterInterface::class)->willReturn(true);
236237
$this->container->get(RouterInterface::class)->willReturn($router);
238+
$this->container->has(UrlHelper::class)->willReturn(false);
237239

238240
$helpers = new HelperPluginManager();
239241
$this->container->has(HelperPluginManager::class)->willReturn(true);
@@ -257,4 +259,23 @@ public function testInjectsCustomHelpersIntoHelperManagerFromContainer(HelperPlu
257259
$this->assertInstanceOf(UrlHelper::class, $helpers->get('url'));
258260
$this->assertInstanceOf(ServerUrlHelper::class, $helpers->get('serverurl'));
259261
}
262+
263+
public function testWillUseUrlHelperFromContainerWhenAvailable()
264+
{
265+
$urlHelper = $this->prophesize(UrlHelper::class)->reveal();
266+
$router = $this->prophesize(RouterInterface::class)->reveal();
267+
$this->container->has('config')->willReturn(false);
268+
$this->container->has(HelperPluginManager::class)->willReturn(false);
269+
$this->container->has(UrlHelper::class)->willReturn(true);
270+
$this->container->get(UrlHelper::class)->willReturn($urlHelper);
271+
$this->container->has(RouterInterface::class)->willReturn(true);
272+
$this->container->get(RouterInterface::class)->willReturn($router);
273+
$factory = new ZendViewRendererFactory();
274+
$view = $factory($this->container->reveal());
275+
276+
$renderer = $this->fetchPhpRenderer($view);
277+
$helpers = $renderer->getHelperPluginManager();
278+
$this->assertTrue($helpers->has('url'));
279+
$this->assertSame($urlHelper, $helpers->get('url'));
280+
}
260281
}

0 commit comments

Comments
 (0)