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

Commit ba23b41

Browse files
committed
Make all factories, plugin managers both BC and FC
Updated all factories to ensure they work with both v2 and v3 of zend-servicemanager. Primarily, this consisted of re-adding the `createService()` method as a proxy to `__invoke()`. Updated all abstract factories to ensure they implement both the v2 and v3 methods. Updated all plugin managers to ensure they implement both the v2 and v3 methods, define appropriate default services, and utilize the v2 properties for "auto add invokables" and "share by default".
1 parent dbda72d commit ba23b41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+806
-85
lines changed

src/Controller/ControllerManager.php

Lines changed: 94 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
use Interop\Container\ContainerInterface;
1313
use Zend\EventManager\EventManagerAwareInterface;
1414
use Zend\EventManager\SharedEventManagerInterface;
15+
use Zend\Mvc\Exception;
1516
use Zend\ServiceManager\AbstractPluginManager;
17+
use Zend\ServiceManager\ConfigInterface;
18+
use Zend\ServiceManager\Exception\InvalidServiceException;
1619
use Zend\Stdlib\DispatchableInterface;
1720

1821
/**
@@ -22,6 +25,18 @@
2225
*/
2326
class ControllerManager extends AbstractPluginManager
2427
{
28+
/**
29+
* We do not want arbitrary classes instantiated as controllers.
30+
*
31+
* @var bool
32+
*/
33+
protected $autoAddInvokableClass = false;
34+
35+
/**
36+
* Controllers must be of this type.
37+
*
38+
* @var string
39+
*/
2540
protected $instanceOf = DispatchableInterface::class;
2641

2742
/**
@@ -30,15 +45,51 @@ class ControllerManager extends AbstractPluginManager
3045
* Injects an initializer for injecting controllers with an
3146
* event manager and plugin manager.
3247
*
33-
* @param ContainerInterface $container
34-
* @param array $configuration
48+
* @param ConfigInterface|ContainerInterface $container
49+
* @param array $v3config
50+
*/
51+
public function __construct($configOrContainerInstance, array $v3config = [])
52+
{
53+
$this->addInitializer([$this, 'injectEventManager']);
54+
$this->addInitializer([$this, 'injectConsole']);
55+
$this->addInitializer([$this, 'injectPluginManager']);
56+
parent::__construct($configOrContainerInstance, $v3config);
57+
}
58+
59+
/**
60+
* Validate a plugin (v3)
61+
*
62+
* {@inheritDoc}
63+
*/
64+
public function validate($plugin)
65+
{
66+
if (! $plugin instanceof $this->instanceOf) {
67+
throw new InvalidServiceException(sprintf(
68+
'Plugin of type "%s" is invalid; must implement %s',
69+
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
70+
$this->instanceOf
71+
));
72+
}
73+
}
74+
75+
/**
76+
* Validate a plugin (v2)
77+
*
78+
* {@inheritDoc}
79+
*
80+
* @throws Exception\InvalidControllerException
3581
*/
36-
public function __construct(ContainerInterface $container, array $configuration = [])
82+
public function validatePlugin($plugin)
3783
{
38-
$this->initializers[] = [$this, 'injectEventManager'];
39-
$this->initializers[] = [$this, 'injectConsole'];
40-
$this->initializers[] = [$this, 'injectPluginManager'];
41-
parent::__construct($container, $configuration);
84+
try {
85+
$this->validate($plugin);
86+
} catch (InvalidServiceException $e) {
87+
throw new Exception\InvalidControllerException(
88+
$e->getMessage(),
89+
$e->getCode(),
90+
$e
91+
);
92+
}
4293
}
4394

4495
/**
@@ -51,11 +102,21 @@ public function __construct(ContainerInterface $container, array $configuration
51102
* the shared EM injection needs to happen; the conditional will always
52103
* pass.
53104
*
54-
* @param ContainerInterface $container
55-
* @param DispatchableInterface $controller
105+
* @param ContainerInterface|DispatchableInterface $first Container when
106+
* using zend-servicemanager v3; controller under v2.
107+
* @param DispatchableInterface|ContainerInterface $second Controller when
108+
* using zend-servicemanager v3; container under v2.
56109
*/
57110
public function injectEventManager(ContainerInterface $container, $controller)
58111
{
112+
if ($first instanceof ContainerInterface) {
113+
$container = $first;
114+
$controller = $second;
115+
} else {
116+
$container = $second;
117+
$controller = $first;
118+
}
119+
59120
if (! $controller instanceof EventManagerAwareInterface) {
60121
return;
61122
}
@@ -69,11 +130,21 @@ public function injectEventManager(ContainerInterface $container, $controller)
69130
/**
70131
* Initializer: inject Console adapter instance
71132
*
72-
* @param ContainerInterface $container
73-
* @param DispatchableInterface $controller
133+
* @param ContainerInterface|DispatchableInterface $first Container when
134+
* using zend-servicemanager v3; controller under v2.
135+
* @param DispatchableInterface|ContainerInterface $second Controller when
136+
* using zend-servicemanager v3; container under v2.
74137
*/
75138
public function injectConsole(ContainerInterface $container, $controller)
76139
{
140+
if ($first instanceof ContainerInterface) {
141+
$container = $first;
142+
$controller = $second;
143+
} else {
144+
$container = $second;
145+
$controller = $first;
146+
}
147+
77148
if (! $controller instanceof AbstractConsoleController) {
78149
return;
79150
}
@@ -84,11 +155,21 @@ public function injectConsole(ContainerInterface $container, $controller)
84155
/**
85156
* Initializer: inject plugin manager
86157
*
87-
* @param ContainerInterface $container
88-
* @param DispatchableInterface $controller
158+
* @param ContainerInterface|DispatchableInterface $first Container when
159+
* using zend-servicemanager v3; controller under v2.
160+
* @param DispatchableInterface|ContainerInterface $second Controller when
161+
* using zend-servicemanager v3; container under v2.
89162
*/
90163
public function injectPluginManager(ContainerInterface $container, $controller)
91164
{
165+
if ($first instanceof ContainerInterface) {
166+
$container = $first;
167+
$controller = $second;
168+
} else {
169+
$container = $second;
170+
$controller = $first;
171+
}
172+
92173
if (! method_exists($controller, 'setPluginManager')) {
93174
return;
94175
}

src/Controller/Plugin/Service/ForwardFactory.php

Lines changed: 15 additions & 1 deletion
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\FactoryInterface;
14+
use Zend\ServiceManager\FactoryInterface;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
1516
use Zend\Mvc\Controller\Plugin\Forward;
1617

1718
class ForwardFactory implements FactoryInterface
@@ -35,4 +36,17 @@ public function __invoke(ContainerInterface $container, $name, array $options =
3536

3637
return new Forward($controllers);
3738
}
39+
40+
/**
41+
* Create and return Forward instance
42+
*
43+
* For use with zend-servicemanager v2; proxies to __invoke().
44+
*
45+
* @param ServiceLocatorInterface $container
46+
* @return Forward
47+
*/
48+
public function createService(ServiceLocatorInterface $container)
49+
{
50+
return $this($container, Forward::class);
51+
}
3852
}

src/Controller/Plugin/Service/IdentityFactory.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
use Interop\Container\ContainerInterface;
1313
use Zend\Authentication\AuthenticationService;
1414
use Zend\Mvc\Controller\Plugin\Identity;
15-
use Zend\ServiceManager\Factory\FactoryInterface;
15+
use Zend\ServiceManager\FactoryInterface;
16+
use Zend\ServiceManager\ServiceLocatorInterface;
1617

1718
class IdentityFactory implements FactoryInterface
1819
{
1920
/**
2021
* {@inheritDoc}
2122
*
22-
* @return \Zend\Mvc\Controller\Plugin\Identity
23+
* @return Identity
2324
*/
2425
public function __invoke(ContainerInterface $container, $name, array $options = null)
2526
{
@@ -29,4 +30,17 @@ public function __invoke(ContainerInterface $container, $name, array $options =
2930
}
3031
return $helper;
3132
}
33+
34+
/**
35+
* Create and return Identity instance
36+
*
37+
* For use with zend-servicemanager v2; proxies to __invoke().
38+
*
39+
* @param ServiceLocatorInterface $container
40+
* @return Identity
41+
*/
42+
public function createService(ServiceLocatorInterface $container)
43+
{
44+
return $this($container, Identity::class);
45+
}
3246
}

src/Controller/PluginManager.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
namespace Zend\Mvc\Controller;
1111

12+
use Zend\Mvc\Exception;
1213
use Zend\ServiceManager\AbstractPluginManager;
14+
use Zend\ServiceManager\Exception\InvalidServiceException;
1315
use Zend\ServiceManager\Factory\InvokableFactory;
1416
use Zend\Stdlib\DispatchableInterface;
1517

@@ -21,6 +23,11 @@
2123
*/
2224
class PluginManager extends AbstractPluginManager
2325
{
26+
/**
27+
* Plugins must be of this type.
28+
*
29+
* @var string
30+
*/
2431
protected $instanceOf = Plugin\PluginInterface::class;
2532

2633
/**
@@ -77,6 +84,21 @@ class PluginManager extends AbstractPluginManager
7784
Plugin\Url::class => InvokableFactory::class,
7885
Plugin\CreateHttpNotFoundModel::class => InvokableFactory::class,
7986
Plugin\CreateConsoleNotFoundModel::class => InvokableFactory::class,
87+
88+
// v2 normalized names
89+
90+
'zendmvccontrollerpluginforward' => Plugin\Service\ForwardFactory::class,
91+
'zendmvccontrollerpluginidentity' => Plugin\Service\IdentityFactory::class,
92+
'zendmvccontrollerpluginacceptableviewmodelselector' => InvokableFactory::class,
93+
'zendmvccontrollerpluginfilepostredirectget' => InvokableFactory::class,
94+
'zendmvccontrollerpluginflashmessenger' => InvokableFactory::class,
95+
'zendmvccontrollerpluginlayout' => InvokableFactory::class,
96+
'zendmvccontrollerpluginparams' => InvokableFactory::class,
97+
'zendmvccontrollerpluginpostredirectget' => InvokableFactory::class,
98+
'zendmvccontrollerpluginredirect' => InvokableFactory::class,
99+
'zendmvccontrollerpluginurl' => InvokableFactory::class,
100+
'zendmvccontrollerplugincreatehttpnotfoundmodel' => InvokableFactory::class,
101+
'zendmvccontrollerplugincreateconsolenotfoundmodel' => InvokableFactory::class,
80102
];
81103

82104
/**
@@ -150,4 +172,40 @@ public function injectController($plugin)
150172

151173
$plugin->setController($controller);
152174
}
175+
176+
/**
177+
* Validate a plugin (v3)
178+
*
179+
* {@inheritDoc}
180+
*/
181+
public function validate($plugin)
182+
{
183+
if (! $plugin instanceof $this->instanceOf) {
184+
throw new InvalidServiceException(sprintf(
185+
'Plugin of type "%s" is invalid; must implement %s',
186+
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
187+
$this->instanceOf
188+
));
189+
}
190+
}
191+
192+
/**
193+
* Validate a plugin (v2)
194+
*
195+
* {@inheritDoc}
196+
*
197+
* @throws Exception\InvalidPluginException
198+
*/
199+
public function validatePlugin($plugin)
200+
{
201+
try {
202+
$this->validate($plugin);
203+
} catch (InvalidServiceException $e) {
204+
throw new Exception\InvalidPluginException(
205+
$e->getMessage(),
206+
$e->getCode(),
207+
$e
208+
);
209+
}
210+
}
153211
}

src/Service/AbstractPluginManagerFactory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
use Interop\Container\ContainerInterface;
1313
use Zend\ServiceManager\AbstractPluginManager;
14-
use Zend\ServiceManager\Factory\FactoryInterface;
14+
use Zend\ServiceManager\FactoryInterface;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
1516

1617
abstract class AbstractPluginManagerFactory implements FactoryInterface
1718
{
@@ -34,4 +35,17 @@ public function __invoke(ContainerInterface $container, $name, array $options =
3435
$pluginManagerClass = static::PLUGIN_MANAGER_CLASS;
3536
return new $pluginManagerClass($container, $options);
3637
}
38+
39+
/**
40+
* Create and return AbstractPluginManager instance
41+
*
42+
* For use with zend-servicemanager v2; proxies to __invoke().
43+
*
44+
* @param ServiceLocatorInterface $container
45+
* @return AbstractPluginManager
46+
*/
47+
public function createService(ServiceLocatorInterface $container)
48+
{
49+
return $this($container, AbstractPluginManager::class);
50+
}
3751
}

src/Service/ApplicationFactory.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212
use Interop\Container\ContainerInterface;
1313
use Zend\Mvc\Application;
14-
use Zend\ServiceManager\Factory\FactoryInterface;
14+
use Zend\ServiceManager\FactoryInterface;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
1516

1617
class ApplicationFactory implements FactoryInterface
1718
{
1819
/**
19-
* Create the Application service
20+
* Create the Application service (v3)
2021
*
2122
* Creates a Zend\Mvc\Application service, passing it the configuration
2223
* service and the service manager instance.
@@ -36,4 +37,17 @@ public function __invoke(ContainerInterface $container, $name, array $options =
3637
$container->get('Response')
3738
);
3839
}
40+
41+
/**
42+
* Create the Application service (v2)
43+
*
44+
* Proxies to __invoke().
45+
*
46+
* @param ServiceLocatorInterface $container
47+
* @return Application
48+
*/
49+
public function createService(ServiceLocatorInterface $container)
50+
{
51+
return $this($container, Application::class);
52+
}
3953
}

src/Service/ConsoleAdapterFactory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
use stdClass;
1414
use Zend\Console\Adapter\AdapterInterface;
1515
use Zend\Console\Console;
16-
use Zend\ServiceManager\Factory\FactoryInterface;
16+
use Zend\ServiceManager\FactoryInterface;
17+
use Zend\ServiceManager\ServiceLocatorInterface;
1718

1819
class ConsoleAdapterFactory implements FactoryInterface
1920
{
@@ -74,4 +75,17 @@ public function __invoke(ContainerInterface $container, $name, array $options =
7475

7576
return $adapter;
7677
}
78+
79+
/**
80+
* Create and return AdapterInterface instance
81+
*
82+
* For use with zend-servicemanager v2; proxies to __invoke().
83+
*
84+
* @param ServiceLocatorInterface $container
85+
* @return AdapterInterface|stdClass
86+
*/
87+
public function createService(ServiceLocatorInterface $container)
88+
{
89+
return $this($container, AdapterInterface::class);
90+
}
7791
}

0 commit comments

Comments
 (0)