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

Commit 82d6868

Browse files
committed
Allow varying ExpressiveUrlGeneratorFactory behavior
This patch adds a new optional constructor argument to the `ExpressiveUrlGeneratorFactory`, a string `$urlHelperServiceName`. By default, it uses `Zend\Expressive\Helper\UrlHelper` for the value. Users can, however, specify an alternate service class. This allows users to vary the `ExpressiveUrlGenerator` per module or path-segregated middleware pipeline: ```php // In configuration: return [ 'dependencies' => [ 'factories' => [ \Api\Router::class => FastRouteRouterFactory::class, \Api\RouteMiddleware::class => new RouteMiddlewareFactory(\Api\Router::class), \Api\UrlHelper::class => new UrlHelperFactory('/api', \Api\Router::class), \Api\UrlHelperMiddleware::class => new UrlHelperMiddlewareFactory(\Api\UrlHelper::class), \Api\UrlGenerator::class => new ExpressiveUrlGeneratorFactory(\Api\UrlHelper::class), ], ], ], ``` The factory implements `__set_state()`, allowing serialization via `var_export()` (the mechanism used by the default expressive configuration caching mechanism).
1 parent 7d5910f commit 82d6868

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/LinkGenerator/ExpressiveUrlGeneratorFactory.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-hal for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-hal/blob/master/LICENSE.md New BSD License
66
*/
77

@@ -16,19 +16,40 @@
1616

1717
class ExpressiveUrlGeneratorFactory
1818
{
19+
/** @var string */
20+
private $urlHelperServiceName;
21+
22+
/**
23+
* Allow serialization
24+
*/
25+
public static function __set_state(array $data) : self
26+
{
27+
return new self(
28+
$data['urlHelperServiceName'] ?? UrlHelper::class
29+
);
30+
}
31+
32+
/**
33+
* Vary behavior based on the URL helper service name.
34+
*/
35+
public function __construct(string $urlHelperServiceName = UrlHelper::class)
36+
{
37+
$this->urlHelperServiceName = $urlHelperServiceName;
38+
}
39+
1940
public function __invoke(ContainerInterface $container) : ExpressiveUrlGenerator
2041
{
21-
if (! $container->has(UrlHelper::class)) {
42+
if (! $container->has($this->urlHelperServiceName)) {
2243
throw new RuntimeException(sprintf(
2344
'%s requires a %s in order to generate a %s instance; none found',
2445
__CLASS__,
25-
UrlHelper::class,
46+
$this->urlHelperServiceName,
2647
ExpressiveUrlGenerator::class
2748
));
2849
}
2950

3051
return new ExpressiveUrlGenerator(
31-
$container->get(UrlHelper::class),
52+
$container->get($this->urlHelperServiceName),
3253
$container->has(ServerUrlHelper::class) ? $container->get(ServerUrlHelper::class) : null
3354
);
3455
}

test/LinkGenerator/ExpressiveUrlGeneratorFactoryTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-hal for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-hal/blob/master/LICENSE.md New BSD License
66
*/
77

@@ -67,4 +67,30 @@ public function testFactoryCanCreateUrlGeneratorWithBothUrlHelperAndServerUrlHel
6767
$this->assertAttributeSame($urlHelper, 'urlHelper', $generator);
6868
$this->assertAttributeSame($serverUrlHelper, 'serverUrlHelper', $generator);
6969
}
70+
71+
public function testFactoryCanAcceptUrlHelperServiceNameToConstructor()
72+
{
73+
$urlHelper = $this->prophesize(UrlHelper::class)->reveal();
74+
75+
$this->container->has(CustomUrlHelper::class)->willReturn(true);
76+
$this->container->get(CustomUrlHelper::class)->willReturn($urlHelper);
77+
$this->container->has(ServerUrlHelper::class)->willReturn(false);
78+
79+
$factory = new ExpressiveUrlGeneratorFactory(CustomUrlHelper::class);
80+
$generator = $factory($this->container->reveal());
81+
82+
$this->assertInstanceOf(ExpressiveUrlGenerator::class, $generator);
83+
$this->assertAttributeSame($urlHelper, 'urlHelper', $generator);
84+
$this->assertAttributeEmpty('serverUrlHelper', $generator);
85+
}
86+
87+
public function testFactoryIsSerializable()
88+
{
89+
$factory = ExpressiveUrlGeneratorFactory::__set_state([
90+
'urlHelperServiceName' => CustomUrlHelper::class,
91+
]);
92+
93+
$this->assertInstanceOf(ExpressiveUrlGeneratorFactory::class, $factory);
94+
$this->assertAttributeSame(CustomUrlHelper::class, 'urlHelperServiceName', $factory);
95+
}
7096
}

0 commit comments

Comments
 (0)