|
9 | 9 |
|
10 | 10 | namespace Zend\ServiceManager;
|
11 | 11 |
|
| 12 | +use Interop\Container\ContainerInterface; |
12 | 13 | use Exception as BaseException;
|
13 | 14 |
|
14 | 15 | /**
|
@@ -57,11 +58,56 @@ abstract class AbstractPluginManager extends ServiceManager implements ServiceLo
|
57 | 58 | * Add a default initializer to ensure the plugin is valid after instance
|
58 | 59 | * creation.
|
59 | 60 | *
|
60 |
| - * @param null|ConfigInterface $configuration |
| 61 | + * Additionally, the constructor provides forwards compatibility with v3 by |
| 62 | + * overloading the initial argument. v2 usage expects either null or a |
| 63 | + * ConfigInterface instance, and will ignore any other arguments. v3 expects |
| 64 | + * a ContainerInterface instance, and will use an array of configuration to |
| 65 | + * seed the current instance with services. In most cases, you can ignore the |
| 66 | + * constructor unless you are writing a specialized factory for your plugin |
| 67 | + * manager or overriding it. |
| 68 | + * |
| 69 | + * @param null|ConfigInterface|ContainerInterface $configOrContainerInstance |
| 70 | + * @param array $v3config If $configOrContainerInstance is a container, this |
| 71 | + * value will be passed to the parent constructor. |
| 72 | + * @throws Exception\InvalidArgumentException if $configOrContainerInstance |
| 73 | + * is neither null, nor a ConfigInterface, nor a ContainerInterface. |
61 | 74 | */
|
62 |
| - public function __construct(ConfigInterface $configuration = null) |
| 75 | + public function __construct($configOrContainerInstance = null, array $v3config = []) |
63 | 76 | {
|
64 |
| - parent::__construct($configuration); |
| 77 | + if (null !== $configOrContainerInstance |
| 78 | + && ! $configOrContainerInstance instanceof ConfigInterface |
| 79 | + && ! $configOrContainerInstance instanceof ContainerInterface |
| 80 | + ) { |
| 81 | + throw new Exception\InvalidArgumentException(sprintf( |
| 82 | + '%s expects a ConfigInterface instance or ContainerInterface instance; received %s', |
| 83 | + get_class($this), |
| 84 | + (is_object($configOrContainerInstance) |
| 85 | + ? get_class($configOrContainerInstance) |
| 86 | + : gettype($configOrContainerInstance) |
| 87 | + ) |
| 88 | + )); |
| 89 | + } |
| 90 | + |
| 91 | + if ($configOrContainerInstance instanceof ContainerInterface) { |
| 92 | + if (property_exists($this, 'serviceLocator')) { |
| 93 | + if (! empty($v3config)) { |
| 94 | + parent::__construct(new Config($v3config)); |
| 95 | + } |
| 96 | + $this->serviceLocator = $configOrContainerInstance; |
| 97 | + } |
| 98 | + |
| 99 | + if (property_exists($this, 'creationContext')) { |
| 100 | + if (! empty($v3config)) { |
| 101 | + parent::__construct($v3config); |
| 102 | + } |
| 103 | + $this->creationContext = $configOrContainerInstance; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if ($configOrContainerInstance instanceof ConfigInterface) { |
| 108 | + parent::__construct($configOrContainerInstance); |
| 109 | + } |
| 110 | + |
65 | 111 | $this->addInitializer(function ($instance) {
|
66 | 112 | if ($instance instanceof ServiceLocatorAwareInterface) {
|
67 | 113 | $instance->setServiceLocator($this);
|
|
0 commit comments