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

Commit a2249d2

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

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/ResourceGeneratorFactory.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,33 @@
1717

1818
class ResourceGeneratorFactory
1919
{
20+
/** @var string */
21+
private $linkGeneratorServiceName;
22+
23+
/**
24+
* Allow serialization
25+
*/
26+
public static function __set_state(array $data) : self
27+
{
28+
return new self(
29+
$data['linkGeneratorServiceName'] ?? LinkGenerator::class
30+
);
31+
}
32+
33+
/**
34+
* Allow varying behavior based on link generator service name.
35+
*/
36+
public function __construct(string $linkGeneratorServiceName = LinkGenerator::class)
37+
{
38+
$this->linkGeneratorServiceName = $linkGeneratorServiceName;
39+
}
40+
2041
public function __invoke(ContainerInterface $container) : ResourceGenerator
2142
{
2243
$generator = new ResourceGenerator(
2344
$container->get(Metadata\MetadataMap::class),
2445
$container->get(HydratorPluginManager::class),
25-
$container->get(LinkGenerator::class)
46+
$container->get($this->linkGeneratorServiceName)
2647
);
2748

2849
$this->injectStrategies($container, $generator);

test/ResourceGeneratorFactoryTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function testFactoryWithRouteBasedCollectionStrategy()
146146
);
147147

148148
$this->container->get(RouteBasedCollectionStrategy::class)->willReturn(
149-
$this->prophesize(RouteBasedCollectionStrategy::class)
149+
$this->prophesize(RouteBasedCollectionStrategy::class)->reveal()
150150
);
151151

152152
$object = new ResourceGeneratorFactory();
@@ -162,4 +162,41 @@ public function testFactoryWithRouteBasedCollectionStrategy()
162162
$registeredStrategies[RouteBasedCollectionMetadata::class]
163163
);
164164
}
165+
166+
public function testConstructorAllowsSpecifyingLinkGeneratorServiceName()
167+
{
168+
$container = $this->prophesize(ContainerInterface::class);
169+
170+
$container
171+
->get(Metadata\MetadataMap::class)
172+
->willReturn($this->prophesize(Metadata\MetadataMap::class)->reveal());
173+
174+
$container
175+
->get(HydratorPluginManager::class)
176+
->willReturn($this->prophesize(ContainerInterface::class)->reveal());
177+
178+
$linkGenerator = $this->prophesize(LinkGenerator::class)->reveal();
179+
$container
180+
->get(CustomLinkGenerator::class)
181+
->willReturn($linkGenerator);
182+
183+
$container->has('config')->willReturn(false);
184+
185+
$factory = new ResourceGeneratorFactory(CustomLinkGenerator::class);
186+
187+
$generator = $factory($container->reveal());
188+
189+
$this->assertInstanceOf(ResourceGenerator::class, $generator);
190+
$this->assertAttributeSame($linkGenerator, 'linkGenerator', $generator);
191+
}
192+
193+
public function testFactoryIsSerializable()
194+
{
195+
$factory = ResourceGeneratorFactory::__set_state([
196+
'linkGeneratorServiceName' => CustomLinkGenerator::class,
197+
]);
198+
199+
$this->assertInstanceOf(ResourceGeneratorFactory::class, $factory);
200+
$this->assertAttributeSame(CustomLinkGenerator::class, 'linkGeneratorServiceName', $factory);
201+
}
165202
}

0 commit comments

Comments
 (0)