addInitializer(array($this, 'injectControllerDependencies'), false); } /** * Inject required dependencies into the controller. * * @param DispatchableInterface $controller * @param ServiceLocatorInterface $serviceLocator * @return void */ public function injectControllerDependencies($controller, ServiceLocatorInterface $serviceLocator) { if (!$controller instanceof DispatchableInterface) { return; } $parentLocator = $serviceLocator->getServiceLocator(); if ($controller instanceof ServiceLocatorAwareInterface) { $controller->setServiceLocator($parentLocator->get('Zend\ServiceManager\ServiceLocatorInterface')); } if ($controller instanceof EventManagerAwareInterface) { // If we have an event manager composed already, make sure it gets // injected with the shared event manager. // The AbstractController lazy-instantiates an EM instance, which // is why the shared EM injection needs to happen; the conditional // will always pass. $events = $controller->getEventManager(); if (!$events instanceof EventManagerInterface) { $controller->setEventManager($parentLocator->get('EventManager')); } else { $events->setSharedManager($parentLocator->get('SharedEventManager')); } } if (method_exists($controller, 'setPluginManager')) { $controller->setPluginManager($parentLocator->get('ControllerPluginManager')); } } /** * Validate the plugin * * Ensure we have a dispatchable. * * @param mixed $plugin * @return true * @throws Exception\InvalidControllerException */ public function validatePlugin($plugin) { if ($plugin instanceof DispatchableInterface) { // we're okay return; } throw new Exception\InvalidControllerException(sprintf( 'Controller of type %s is invalid; must implement Zend\Stdlib\DispatchableInterface', (is_object($plugin) ? get_class($plugin) : gettype($plugin)) )); } /** * Override: do not use peering service managers * * @param string|array $name * @param bool $checkAbstractFactories * @param bool $usePeeringServiceManagers * @return bool */ public function has($name, $checkAbstractFactories = true, $usePeeringServiceManagers = false) { return parent::has($name, $checkAbstractFactories, $usePeeringServiceManagers); } /** * Override: do not use peering service managers * * @param string $name * @param array $options * @param bool $usePeeringServiceManagers * @return mixed */ public function get($name, $options = array(), $usePeeringServiceManagers = false) { return parent::get($name, $options, $usePeeringServiceManagers); } }