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

Commit 46a4cd2

Browse files
committed
Add service locator injection initializer to ControllerManager
This functionality was removed when we were targeting a v3 release, but needs to be re-added when targeting v2.7.
1 parent e740f89 commit 46a4cd2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/Controller/ControllerManager.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Zend\ServiceManager\AbstractPluginManager;
1717
use Zend\ServiceManager\ConfigInterface;
1818
use Zend\ServiceManager\Exception\InvalidServiceException;
19+
use Zend\ServiceManager\ServiceLocatorAwareInterface;
1920
use Zend\Stdlib\DispatchableInterface;
2021

2122
/**
@@ -53,6 +54,7 @@ public function __construct($configOrContainerInstance, array $v3config = [])
5354
$this->addInitializer([$this, 'injectEventManager']);
5455
$this->addInitializer([$this, 'injectConsole']);
5556
$this->addInitializer([$this, 'injectPluginManager']);
57+
$this->addInitializer([$this, 'injectServiceLocator']);
5658
parent::__construct($configOrContainerInstance, $v3config);
5759
}
5860

@@ -191,4 +193,38 @@ public function injectPluginManager($first, $second)
191193

192194
$controller->setPluginManager($container->get('ControllerPluginManager'));
193195
}
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+
}
194230
}

0 commit comments

Comments
 (0)