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

Commit dbda72d

Browse files
committed
Make RouteInvokableFactory + RoutePluginManager v2/v3 compatible
- Ensured each extend the appropriate v2 interface/class, and implement the required methods for both v2 and v3. - Added `setInvokableClass()` implementation to `RoutePluginManager` to mimic work in `configure()` for v2.
1 parent b2a17be commit dbda72d

File tree

2 files changed

+106
-12
lines changed

2 files changed

+106
-12
lines changed

src/Router/RouteInvokableFactory.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
use Interop\Container\ContainerInterface;
1313
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
14-
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
14+
use Zend\ServiceManager\AbstractFactoryInterface;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
1516

1617
/**
1718
* Specialized invokable/abstract factory for use with RoutePluginManager.
@@ -22,15 +23,15 @@
2223
class RouteInvokableFactory implements AbstractFactoryInterface
2324
{
2425
/**
25-
* Can we create a route instance with the given name?
26+
* Can we create a route instance with the given name? (v3)
2627
*
2728
* Only works for FQCN $routeName values, for classes that implement RouteInterface.
2829
*
2930
* @param ContainerInterface $container
3031
* @param string $routeName
3132
* @return bool
3233
*/
33-
public function canCreateServiceWithName(ContainerInterface $container, $routeName)
34+
public function canCreate(ContainerInterface $container, $routeName)
3435
{
3536
if (! class_exists($routeName)) {
3637
return false;
@@ -43,6 +44,21 @@ public function canCreateServiceWithName(ContainerInterface $container, $routeNa
4344
return true;
4445
}
4546

47+
/**
48+
* Can we create a route instance with the given name? (v2)
49+
*
50+
* Proxies to canCreate().
51+
*
52+
* @param ServiceLocatorInterface $container
53+
* @param string $normalizedName
54+
* @param string $routeName
55+
* @return bool
56+
*/
57+
public function canCreateServiceWithName(ServiceLocatorInterface $container, $normalizedName, $routeName)
58+
{
59+
return $this->canCreate($container, $routeName);
60+
}
61+
4662
/**
4763
* Create and return a RouteInterface instance.
4864
*
@@ -80,4 +96,19 @@ public function __invoke(ContainerInterface $container, $routeName, array $optio
8096

8197
return $routeName::factory($options);
8298
}
99+
100+
/**
101+
* Create a route instance with the given name. (v2)
102+
*
103+
* Proxies to __invoke().
104+
*
105+
* @param ServiceLocatorInterface $container
106+
* @param string $normalizedName
107+
* @param string $routeName
108+
* @return RouteInterface
109+
*/
110+
public function createServiceWithName(ServiceLocatorInterface $container, $normalizedName, $routeName)
111+
{
112+
return $this($container, $routeName);
113+
}
83114
}

src/Router/RoutePluginManager.php

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Interop\Container\ContainerInterface;
1313
use Zend\ServiceManager\AbstractPluginManager;
14+
use Zend\ServiceManager\Exception\InvalidServiceException;
1415
use Zend\Stdlib\ArrayUtils;
1516

1617
/**
@@ -33,32 +34,94 @@ class RoutePluginManager extends AbstractPluginManager
3334
protected $instanceOf = RouteInterface::class;
3435

3536
/**
36-
* Do not share instances.
37+
* Do not share instances. (v3)
3738
*
3839
* @var bool
3940
*/
4041
protected $shareByDefault = false;
4142

43+
/**
44+
* Do not share instances. (v2)
45+
*
46+
* @var bool
47+
*/
48+
protected $sharedByDefault = false;
49+
4250
/**
4351
* Constructor
4452
*
4553
* Ensure that the instance is seeded with the RouteInvokableFactory as an
4654
* abstract factory.
4755
*
48-
* @param ContainerInterface $container
49-
* @param array $config
56+
* @param ContainerInterface|\Zend\ServiceManager\ConfigInterface $configOrContainerInstance
57+
* @param array $v3config
58+
*/
59+
public function __construct($configOrContainerInstance, array $v3config = [])
60+
{
61+
$this->addAbstractFactory(RouteInvokableFactory::class);
62+
parent::__construct($configOrContainerInstance, $v3config);
63+
}
64+
65+
/**
66+
* Validate a route plugin. (v2)
67+
*
68+
* @param object $plugin
69+
* @throws InvalidServiceException
70+
*/
71+
public function validate($plugin)
72+
{
73+
if (! $plugin instanceof $this->instanceOf) {
74+
throw new InvalidServiceException(sprintf(
75+
'Plugin of type %s is invalid; must implement %s',
76+
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
77+
RouteInterface::class
78+
));
79+
}
80+
}
81+
82+
/**
83+
* Validate a route plugin. (v2)
84+
*
85+
* @param object $plugin
86+
* @throws Exception\RuntimeException
5087
*/
51-
public function __construct(ContainerInterface $container, array $config = [])
88+
public function validatePlugin($plugin)
5289
{
53-
$config = ArrayUtils::merge(['abstract_factories' => [
54-
RouteInvokableFactory::class,
55-
]], $config);
90+
try {
91+
$this->validate($plugin);
92+
} catch (InvalidServiceException $e) {
93+
throw new Exception\RuntimeException(
94+
$e->getMessage(),
95+
$e->getCode(),
96+
$e
97+
);
98+
}
99+
}
100+
101+
/**
102+
* Register an invokable class. (v2)
103+
*
104+
* Create invokable factories + optional aliases for an invokable class.
105+
*
106+
* @param string $name
107+
* @param string $class
108+
* @return self
109+
*/
110+
public function setInvokableClass($name, $class)
111+
{
112+
foreach ($this->createAliasesForInvokables([$name => $class]) as $name => $class) {
113+
$this->setAlias($name, $class);
114+
}
115+
116+
foreach ($this->createFactoriesForInvokables([$name => $class]) as $name => $factory) {
117+
$this->setFactory($name, $factory);
118+
}
56119

57-
parent::__construct($container, $config);
120+
return $this;
58121
}
59122

60123
/**
61-
* Pre-process configuration.
124+
* Pre-process configuration. (v3)
62125
*
63126
* Checks for invokables, and, if found, maps them to the
64127
* component-specific RouteInvokableFactory; removes the invokables entry

0 commit comments

Comments
 (0)