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

Commit ef4f781

Browse files
committed
Allow varying LinkGeneratorFactory behavior
This patch adds a new optional constructor argument to the `LinkGeneratorFactory`, a string `$urlGeneratorServiceName`. By default, it uses `Zend\Expressive\Hal\LinkGenerator\UrlGeneratorInterface` for the value. Users can, however, specify an alternate service class. This allows users to vary the `LinkGenerator` 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), \Api\LinkGenerator::class => new LinkGeneratorFactory(\Api\UrlGenerator::class), ], ], ], ``` The factory implements `__set_state()`, allowing serialization via `var_export()` (the mechanism used by the default expressive configuration caching mechanism).
1 parent 82d6868 commit ef4f781

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/LinkGeneratorFactory.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,31 @@
1111

1212
class LinkGeneratorFactory
1313
{
14+
/** @var string */
15+
private $urlGeneratorServiceName;
16+
17+
/**
18+
* Allow serialization
19+
*/
20+
public static function __set_state(array $data) : self
21+
{
22+
return new self(
23+
$data['urlGeneratorServiceName'] ?? LinkGenerator\UrlGeneratorInterface::class
24+
);
25+
}
26+
27+
/**
28+
* Allow varying behavior based on URL generator service name.
29+
*/
30+
public function __construct(string $urlGeneratorServiceName = LinkGenerator\UrlGeneratorInterface::class)
31+
{
32+
$this->urlGeneratorServiceName = $urlGeneratorServiceName;
33+
}
34+
1435
public function __invoke(ContainerInterface $container) : LinkGenerator
1536
{
1637
return new LinkGenerator(
17-
$container->get(LinkGenerator\UrlGeneratorInterface::class)
38+
$container->get($this->urlGeneratorServiceName)
1839
);
1940
}
2041
}

test/LinkGeneratorFactoryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,26 @@ public function testReturnsLinkGeneratorInstance() : void
2727
self::assertInstanceOf(LinkGenerator::class, $instance);
2828
self::assertAttributeSame($urlGenerator, 'urlGenerator', $instance);
2929
}
30+
31+
public function testConstructorAllowsSpecifyingUrlGeneratorServiceName()
32+
{
33+
$urlGenerator = $this->prophesize(LinkGenerator\UrlGeneratorInterface::class)->reveal();
34+
35+
$container = $this->prophesize(ContainerInterface::class);
36+
$container->get(UrlGenerator::class)->willReturn($urlGenerator);
37+
38+
$instance = (new LinkGeneratorFactory(UrlGenerator::class))($container->reveal());
39+
self::assertInstanceOf(LinkGenerator::class, $instance);
40+
self::assertAttributeSame($urlGenerator, 'urlGenerator', $instance);
41+
}
42+
43+
public function testFactoryIsSerializable()
44+
{
45+
$factory = LinkGeneratorFactory::__set_state([
46+
'urlGeneratorServiceName' => UrlGenerator::class,
47+
]);
48+
49+
$this->assertInstanceOf(LinkGeneratorFactory::class, $factory);
50+
$this->assertAttributeSame(UrlGenerator::class, 'urlGeneratorServiceName', $factory);
51+
}
3052
}

0 commit comments

Comments
 (0)