|
16 | 16 | use Zend\ServiceManager\AbstractPluginManager;
|
17 | 17 | use Zend\ServiceManager\ConfigInterface;
|
18 | 18 | use Zend\ServiceManager\Exception\InvalidServiceException;
|
| 19 | +use Zend\ServiceManager\ServiceLocatorAwareInterface; |
19 | 20 | use Zend\Stdlib\DispatchableInterface;
|
20 | 21 |
|
21 | 22 | /**
|
@@ -53,6 +54,7 @@ public function __construct($configOrContainerInstance, array $v3config = [])
|
53 | 54 | $this->addInitializer([$this, 'injectEventManager']);
|
54 | 55 | $this->addInitializer([$this, 'injectConsole']);
|
55 | 56 | $this->addInitializer([$this, 'injectPluginManager']);
|
| 57 | + $this->addInitializer([$this, 'injectServiceLocator']); |
56 | 58 | parent::__construct($configOrContainerInstance, $v3config);
|
57 | 59 | }
|
58 | 60 |
|
@@ -191,4 +193,38 @@ public function injectPluginManager($first, $second)
|
191 | 193 |
|
192 | 194 | $controller->setPluginManager($container->get('ControllerPluginManager'));
|
193 | 195 | }
|
| 196 | + |
| 197 | + /** |
| 198 | + * Initializer: inject service locator |
| 199 | + * |
| 200 | + * @param ContainerInterface|DispatchableInterface $first Container when |
| 201 | + * using zend-servicemanager v3; controller under v2. |
| 202 | + * @param DispatchableInterface|ContainerInterface $second Controller when |
| 203 | + * using zend-servicemanager v3; container under v2. |
| 204 | + */ |
| 205 | + public function injectServiceLocator($first, $second) |
| 206 | + { |
| 207 | + if ($first instanceof ContainerInterface) { |
| 208 | + $container = $first; |
| 209 | + $controller = $second; |
| 210 | + } else { |
| 211 | + $container = $second; |
| 212 | + $controller = $first; |
| 213 | + } |
| 214 | + |
| 215 | + // For v2, we need to pull the parent service locator |
| 216 | + if (! method_exists($container, 'configure')) { |
| 217 | + $container = $container->getServiceLocator() ?: $container; |
| 218 | + } |
| 219 | + |
| 220 | + if ($controller instanceof ServiceLocatorAwareInterface) { |
| 221 | + trigger_error(sprintf( |
| 222 | + 'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along ' |
| 223 | + . 'with the ServiceLocatorAwareInitializer. Please update your class %s to remove ' |
| 224 | + . 'the implementation, and start injecting your dependencies via factory instead.', |
| 225 | + get_class($controller) |
| 226 | + ), E_USER_DEPRECATED); |
| 227 | + $controller->setServiceLocator($container); |
| 228 | + } |
| 229 | + } |
194 | 230 | }
|
0 commit comments