/** * 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')); } }
/** * 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) { $controller->setEventManager($parentLocator->get('EventManager')); } if (method_exists($controller, 'setPluginManager')) { $controller->setPluginManager($parentLocator->get('ControllerPluginBroker')); } }