Skip to main content
deleted 1496 characters in body
Source Link

This is a very old post but since no answer has been accepted and I recently needed to achieve the same thing, I thought I'd share my solution(s).

I discovered two possible ways of achieving what you want, theThe reason I needed to access the ServiceManager before the Bootstrap event is triggered, was so I could manipulate the merged configuration with values retrieved from the database.

Solution 1:

public function onLoadModulesPost(ModuleEvent $event) { /* @var $serviceManager \Zend\ServiceManager\ServiceManager */ $serviceManager = $event->getParam('ServiceManager'); $configListener = $event->getConfigListener(); $configuration = $configListener->getMergedConfig(false); $someService = $serviceManager->get('Your/Custom/Service'); $information = $someService->fetchSomeInformation(); $configuration = array_merge($configuration, $information); $configListener->setMergedConfig($configuration); $event->setConfigListener($configListener); $serviceManager->setAllowOverride(true); $serviceManager->setService('Config', $configuration); $serviceManager->setAllowOverride(false); } 

Solution 2:

If manipulating the merged configuration is your end goal (while having access to the ServiceManager) then there might be a simpler way.

In your module configuration file, override the Config factory definition.

return [ ... 'service_manager' => [ 'factories' => [ ... 'Config' => 'Your\Mvc\Service\ConfigFactory', ], ], ]; 

By doing so, every time the configuration is retrieved from the ServiceManager your instance will be instantiated and the cached value will be returned.

Your factory class may look something like:

<?php namespace Your\Mvc\Service; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class ConfigFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { /* @var $moduleManager \Zend\ModuleManager\ModuleManager */ $moduleManager = $serviceLocator->get('ModuleManager'); $moduleManager->loadModules(); $event = $moduleManager->getEvent(); $configListener = $event->getConfigListener(); $configuration = $configListener->getMergedConfig(false); $someService = $serviceLocator->get('Your/Custom/Service'); $information = $someService->fetchSomeInformation(); $configuration = array_merge($configuration, $information); return $configuration; } } 

This is a very old post but since no answer has been accepted and I recently needed to achieve the same thing, I thought I'd share my solution(s).

I discovered two possible ways of achieving what you want, the reason I needed to access the ServiceManager before the Bootstrap event is triggered, was so I could manipulate the merged configuration with values retrieved from the database.

Solution 1:

public function onLoadModulesPost(ModuleEvent $event) { /* @var $serviceManager \Zend\ServiceManager\ServiceManager */ $serviceManager = $event->getParam('ServiceManager'); $configListener = $event->getConfigListener(); $configuration = $configListener->getMergedConfig(false); $someService = $serviceManager->get('Your/Custom/Service'); $information = $someService->fetchSomeInformation(); $configuration = array_merge($configuration, $information); $configListener->setMergedConfig($configuration); $event->setConfigListener($configListener); $serviceManager->setAllowOverride(true); $serviceManager->setService('Config', $configuration); $serviceManager->setAllowOverride(false); } 

Solution 2:

If manipulating the merged configuration is your end goal (while having access to the ServiceManager) then there might be a simpler way.

In your module configuration file, override the Config factory definition.

return [ ... 'service_manager' => [ 'factories' => [ ... 'Config' => 'Your\Mvc\Service\ConfigFactory', ], ], ]; 

By doing so, every time the configuration is retrieved from the ServiceManager your instance will be instantiated and the cached value will be returned.

Your factory class may look something like:

<?php namespace Your\Mvc\Service; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class ConfigFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { /* @var $moduleManager \Zend\ModuleManager\ModuleManager */ $moduleManager = $serviceLocator->get('ModuleManager'); $moduleManager->loadModules(); $event = $moduleManager->getEvent(); $configListener = $event->getConfigListener(); $configuration = $configListener->getMergedConfig(false); $someService = $serviceLocator->get('Your/Custom/Service'); $information = $someService->fetchSomeInformation(); $configuration = array_merge($configuration, $information); return $configuration; } } 

This is a very old post but since no answer has been accepted and I recently needed to achieve the same thing, I thought I'd share my solution.

The reason I needed to access the ServiceManager before the Bootstrap event is triggered, was so I could manipulate the merged configuration with values retrieved from the database.

Solution:

public function onLoadModulesPost(ModuleEvent $event) { /* @var $serviceManager \Zend\ServiceManager\ServiceManager */ $serviceManager = $event->getParam('ServiceManager'); $configListener = $event->getConfigListener(); $configuration = $configListener->getMergedConfig(false); $someService = $serviceManager->get('Your/Custom/Service'); $information = $someService->fetchSomeInformation(); $configuration = array_merge($configuration, $information); $configListener->setMergedConfig($configuration); $event->setConfigListener($configListener); $serviceManager->setAllowOverride(true); $serviceManager->setService('Config', $configuration); $serviceManager->setAllowOverride(false); } 
Source Link

This is a very old post but since no answer has been accepted and I recently needed to achieve the same thing, I thought I'd share my solution(s).

I discovered two possible ways of achieving what you want, the reason I needed to access the ServiceManager before the Bootstrap event is triggered, was so I could manipulate the merged configuration with values retrieved from the database.

Problem:

The example found in the Zend documentation shows how to manipulate the merged configuration, but at that particular time the Service manager is empty, making it impossible to retrieve things like database adapters etc.

Solution 1:

In your module class, implement the interface InitProviderInterface and add the appropriate method.

public function init(ModuleManagerInterface $moduleManager) { $eventManager = $moduleManager->getEventManager(); $eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'onLoadModulesPost']); } 

The EVENT_LOAD_MODULES_POST event will get invoked after the EVENT_MERGE_CONFIG event but before the EVENT_BOOTSTRAP event is triggered. Also at this particular time the ServiceManager will contain all the factories, invokable classes you're wanting to access.

Your callback method may look something like.

public function onLoadModulesPost(ModuleEvent $event) { /* @var $serviceManager \Zend\ServiceManager\ServiceManager */ $serviceManager = $event->getParam('ServiceManager'); $configListener = $event->getConfigListener(); $configuration = $configListener->getMergedConfig(false); $someService = $serviceManager->get('Your/Custom/Service'); $information = $someService->fetchSomeInformation(); $configuration = array_merge($configuration, $information); $configListener->setMergedConfig($configuration); $event->setConfigListener($configListener); $serviceManager->setAllowOverride(true); $serviceManager->setService('Config', $configuration); $serviceManager->setAllowOverride(false); } 

Solution 2:

If manipulating the merged configuration is your end goal (while having access to the ServiceManager) then there might be a simpler way.

In your module configuration file, override the Config factory definition.

return [ ... 'service_manager' => [ 'factories' => [ ... 'Config' => 'Your\Mvc\Service\ConfigFactory', ], ], ]; 

By doing so, every time the configuration is retrieved from the ServiceManager your instance will be instantiated and the cached value will be returned.

Your factory class may look something like:

<?php namespace Your\Mvc\Service; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class ConfigFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { /* @var $moduleManager \Zend\ModuleManager\ModuleManager */ $moduleManager = $serviceLocator->get('ModuleManager'); $moduleManager->loadModules(); $event = $moduleManager->getEvent(); $configListener = $event->getConfigListener(); $configuration = $configListener->getMergedConfig(false); $someService = $serviceLocator->get('Your/Custom/Service'); $information = $someService->fetchSomeInformation(); $configuration = array_merge($configuration, $information); return $configuration; } }