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

Commit

Permalink
Merge branch 'hotfix/routeresult-observer'
Browse files Browse the repository at this point in the history
Close #9
  • Loading branch information
weierophinney committed Dec 2, 2015
2 parents 8bd425b + f48f30c commit 825c952
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 2 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ All notable changes to this project will be documented in this file, in reverse

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

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"require": {
"php": ">=5.5",
"container-interop/container-interop": "^1.1",
"zendframework/zend-expressive": "^1.0@rc",
"zendframework/zend-expressive": "~1.0.0-dev@dev",
"zendframework/zend-filter": "^2.5",
"zendframework/zend-i18n": "^2.5",
"zendframework/zend-servicemanager": "^2.5",
Expand Down
33 changes: 33 additions & 0 deletions src/ApplicationUrlDelegatorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\ZendView;

use Zend\ServiceManager\DelegatorFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ApplicationUrlDelegatorFactory implements DelegatorFactoryInterface
{
/**
* Inject the UrlHelper in an Application instance, when available.
*
* @param ServiceLocatorInterface $container
* @param string $name
* @param string $requestedName
* @param callable $callback Callback that returns the Application instance
*/
public function createDelegatorWithName(ServiceLocatorInterface $container, $name, $requestedName, $callback)
{
$application = $callback();
if ($container->has(UrlHelper::class)) {
$application->attachRouteResultObserver($container->get(UrlHelper::class));
}
return $application;
}
}
11 changes: 10 additions & 1 deletion src/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
use Zend\Expressive\Exception;
use Zend\Expressive\Router\RouterInterface;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\Router\RouteResultObserverInterface;
use Zend\View\Helper\AbstractHelper;

class UrlHelper extends AbstractHelper
class UrlHelper extends AbstractHelper implements RouteResultObserverInterface
{
/**
* @var RouteResult
Expand Down Expand Up @@ -62,6 +63,14 @@ public function __invoke($route = null, $params = [])
return $this->router->generateUri($route, $params);
}

/**
* {@inheritDoc}
*/
public function update(RouteResult $result)
{
$this->result = $result;
}

/**
* @param RouteResult $result
*/
Expand Down
27 changes: 27 additions & 0 deletions src/UrlHelperFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\ZendView;

use Interop\Container\ContainerInterface;
use Zend\Expressive\Router\RouterInterface;

class UrlHelperFactory
{
/**
* Create a UrlHelper instance.
*
* @param ContainerInterface $container
* @return UrlHelper
*/
public function __invoke(ContainerInterface $container)
{
return new UrlHelper($container->get(RouterInterface::class));
}
}
3 changes: 3 additions & 0 deletions src/ZendViewRendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ private function injectHelpers(PhpRenderer $renderer, ContainerInterface $contai
: new HelperPluginManager();

$helpers->setFactory('url', function () use ($container) {
if ($container->has(UrlHelper::class)) {
return $container->get(UrlHelper::class);
}
return new UrlHelper($container->get(RouterInterface::class));
});
$helpers->setInvokableClass('serverurl', ServerUrlHelper::class);
Expand Down
42 changes: 42 additions & 0 deletions test/ApplicationUrlDelegatorFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Expressive\ZendView;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Expressive\Application;
use Zend\Expressive\ZendView\ApplicationUrlDelegatorFactory;
use Zend\Expressive\ZendView\UrlHelper;
use Zend\ServiceManager\ServiceLocatorInterface;

class ApplicationUrlDelegatorFactoryTest extends TestCase
{
public function testDelegatorRegistersUrlHelperAsRouteResultObserverWithApplication()
{
$urlHelper = $this->prophesize(UrlHelper::class);
$application = $this->prophesize(Application::class);
$application->attachRouteResultObserver($urlHelper->reveal())->shouldBeCalled();
$applicationCallback = function () use ($application) {
return $application->reveal();
};

$container = $this->prophesize(ServiceLocatorInterface::class);
$container->has(UrlHelper::class)->willReturn(true);
$container->get(UrlHelper::class)->willReturn($urlHelper->reveal());

$delegator = new ApplicationUrlDelegatorFactory();
$test = $delegator->createDelegatorWithName(
$container->reveal(),
Application::class,
Application::class,
$applicationCallback
);
$this->assertSame($application->reveal(), $test);
}
}
15 changes: 15 additions & 0 deletions test/UrlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Zend\Expressive\Exception;
use Zend\Expressive\Router\RouterInterface;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\Router\RouteResultObserverInterface;
use Zend\Expressive\ZendView\UrlHelper;

class UrlHelperTest extends TestCase
Expand Down Expand Up @@ -134,4 +135,18 @@ public function testProvidedParametersOverrideAnyPresentInARouteResultWhenGenera

$this->assertEquals('URL', $helper('resource', ['id' => 2]));
}

public function testIsARouteResultObserver()
{
$helper = $this->createHelper();
$this->assertInstanceOf(RouteResultObserverInterface::class, $helper);
}

public function testUpdateMethodSetsRouteResultProperty()
{
$result = $this->prophesize(RouteResult::class);
$helper = $this->createHelper();
$helper->update($result->reveal());
$this->assertAttributeSame($result->reveal(), 'result', $helper);
}
}
21 changes: 21 additions & 0 deletions test/ZendViewRendererFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public function testInjectsCustomHelpersIntoHelperManager()
$router = $this->prophesize(RouterInterface::class)->reveal();
$this->container->has('config')->willReturn(false);
$this->container->has(HelperPluginManager::class)->willReturn(false);
$this->container->has(UrlHelper::class)->willReturn(false);
$this->container->has(RouterInterface::class)->willReturn(true);
$this->container->get(RouterInterface::class)->willReturn($router);
$factory = new ZendViewRendererFactory();
Expand All @@ -234,6 +235,7 @@ public function testWillUseHelperManagerFromContainer()
$this->container->has('config')->willReturn(false);
$this->container->has(RouterInterface::class)->willReturn(true);
$this->container->get(RouterInterface::class)->willReturn($router);
$this->container->has(UrlHelper::class)->willReturn(false);

$helpers = new HelperPluginManager();
$this->container->has(HelperPluginManager::class)->willReturn(true);
Expand All @@ -257,4 +259,23 @@ public function testInjectsCustomHelpersIntoHelperManagerFromContainer(HelperPlu
$this->assertInstanceOf(UrlHelper::class, $helpers->get('url'));
$this->assertInstanceOf(ServerUrlHelper::class, $helpers->get('serverurl'));
}

public function testWillUseUrlHelperFromContainerWhenAvailable()
{
$urlHelper = $this->prophesize(UrlHelper::class)->reveal();
$router = $this->prophesize(RouterInterface::class)->reveal();
$this->container->has('config')->willReturn(false);
$this->container->has(HelperPluginManager::class)->willReturn(false);
$this->container->has(UrlHelper::class)->willReturn(true);
$this->container->get(UrlHelper::class)->willReturn($urlHelper);
$this->container->has(RouterInterface::class)->willReturn(true);
$this->container->get(RouterInterface::class)->willReturn($router);
$factory = new ZendViewRendererFactory();
$view = $factory($this->container->reveal());

$renderer = $this->fetchPhpRenderer($view);
$helpers = $renderer->getHelperPluginManager();
$this->assertTrue($helpers->has('url'));
$this->assertSame($urlHelper, $helpers->get('url'));
}
}

0 comments on commit 825c952

Please sign in to comment.