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

Implement RouteResultObserverInterface #9

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my assumption is this may be problematic when zend-expressive need to download zendviewrenderer. But I am not exactly sure if this will work without minimum-stability flag in expressive. How about you add minimum-stability dev to the composer.json and keep the rest in tact ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will essentially grab dev-master always. I think instead I'm going to tag the zend-expressive repo, and then update this one to the latest tag.

I understand why everyone wanted to separate out the implementations, but while we're not stable, this is a nightmare to maintain the dependencies!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will essentially grab dev-master always

Yes only for this repo. But you can change later :) .

and I can understand the pain. There should be some sort of scripts to do automatic releases and tagging when we have many repos.

"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 @@ -210,6 +210,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 @@ -231,6 +232,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 @@ -254,4 +256,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'));
}
}