/**
  * @param EventManagerInterface $events
  *
  * @return ServiceListener
  */
 public function attach(EventManagerInterface $events)
 {
     if (is_callable($this->overrideAppConfigHandler)) {
         $this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, $this->overrideAppConfigHandler);
     }
     parent::attach($events);
 }
 /**
  * Create the service listener service
  *
  * Tries to get a service named ServiceListenerInterface from the service
  * locator, otherwise creates a Zend\ModuleManager\Listener\ServiceListener
  * service, passing it the service locator instance and the default service
  * configuration, which can be overridden by modules.
  *
  * It looks for the 'service_listener_options' key in the application
  * config and tries to add service manager as configured. The value of
  * 'service_listener_options' must be a list (array) which contains the
  * following keys:
  *   - service_manager: the name of the service manage to create as string
  *   - config_key: the name of the configuration key to search for as string
  *   - interface: the name of the interface that modules can implement as string
  *   - method: the name of the method that modules have to implement as string
  *
  * @param  ServiceLocatorInterface  $serviceLocator
  * @return ServiceListener
  * @throws InvalidArgumentException For invalid configurations.
  * @throws RuntimeException
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $configuration = $serviceLocator->get('ApplicationConfig');
     if ($serviceLocator->has('ServiceListenerInterface')) {
         $serviceListener = $serviceLocator->get('ServiceListenerInterface');
         if (!$serviceListener instanceof ServiceListenerInterface) {
             throw new RuntimeException('The service named ServiceListenerInterface must implement ' . 'Zend\\ModuleManager\\Listener\\ServiceListenerInterface');
         }
         $serviceListener->setDefaultServiceConfig($this->defaultServiceConfig);
     } else {
         $serviceListener = new ServiceListener($serviceLocator, $this->defaultServiceConfig);
     }
     if (isset($configuration['service_listener_options'])) {
         if (!is_array($configuration['service_listener_options'])) {
             throw new InvalidArgumentException(sprintf('The value of service_listener_options must be an array, %s given.', gettype($configuration['service_listener_options'])));
         }
         foreach ($configuration['service_listener_options'] as $key => $newServiceManager) {
             if (!isset($newServiceManager['service_manager'])) {
                 throw new InvalidArgumentException(sprintf(self::MISSING_KEY_ERROR, $key, 'service_manager'));
             } elseif (!is_string($newServiceManager['service_manager'])) {
                 throw new InvalidArgumentException(sprintf(self::VALUE_TYPE_ERROR, 'service_manager', gettype($newServiceManager['service_manager'])));
             }
             if (!isset($newServiceManager['config_key'])) {
                 throw new InvalidArgumentException(sprintf(self::MISSING_KEY_ERROR, $key, 'config_key'));
             } elseif (!is_string($newServiceManager['config_key'])) {
                 throw new InvalidArgumentException(sprintf(self::VALUE_TYPE_ERROR, 'config_key', gettype($newServiceManager['config_key'])));
             }
             if (!isset($newServiceManager['interface'])) {
                 throw new InvalidArgumentException(sprintf(self::MISSING_KEY_ERROR, $key, 'interface'));
             } elseif (!is_string($newServiceManager['interface'])) {
                 throw new InvalidArgumentException(sprintf(self::VALUE_TYPE_ERROR, 'interface', gettype($newServiceManager['interface'])));
             }
             if (!isset($newServiceManager['method'])) {
                 throw new InvalidArgumentException(sprintf(self::MISSING_KEY_ERROR, $key, 'method'));
             } elseif (!is_string($newServiceManager['method'])) {
                 throw new InvalidArgumentException(sprintf(self::VALUE_TYPE_ERROR, 'method', gettype($newServiceManager['method'])));
             }
             $serviceListener->addServiceManager($newServiceManager['service_manager'], $newServiceManager['config_key'], $newServiceManager['interface'], $newServiceManager['method']);
         }
     }
     return $serviceListener;
 }
 /**
  * Creates and returns the module manager
  *
  * Instantiates the default module listeners, providing them configuration
  * from the "module_listener_options" key of the ApplicationConfiguration
  * service. Also sets the default config glob path.
  *
  * Module manager is instantiated and provided with an EventManager, to which
  * the default listener aggregate is attached. The ModuleEvent is also created
  * and attached to the module manager.
  *
  * @param  ServiceLocatorInterface $serviceLocator
  * @return ModuleManager
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $configuration = $serviceLocator->get('ApplicationConfiguration');
     $listenerOptions = new ListenerOptions($configuration['module_listener_options']);
     $defaultListeners = new DefaultListenerAggregate($listenerOptions);
     $serviceListener = new ServiceListener($serviceLocator, $this->defaultServiceConfiguration);
     $serviceListener->addServiceManager($serviceLocator, 'service_manager', 'Zend\\ModuleManager\\Feature\\ServiceProviderInterface', 'getServiceConfiguration');
     $serviceListener->addServiceManager('ControllerLoader', 'controllers', 'Zend\\ModuleManager\\Feature\\ControllerProviderInterface', 'getControllerConfiguration');
     $serviceListener->addServiceManager('ControllerPluginManager', 'controller_plugins', 'Zend\\ModuleManager\\Feature\\ControllerPluginProviderInterface', 'getControllerPluginConfiguration');
     $serviceListener->addServiceManager('ViewHelperManager', 'view_helpers', 'Zend\\ModuleManager\\Feature\\ViewHelperProviderInterface', 'getViewHelperConfiguration');
     $events = $serviceLocator->get('EventManager');
     $events->attach($defaultListeners);
     $events->attach($serviceListener);
     $moduleEvent = new ModuleEvent();
     $moduleEvent->setParam('ServiceManager', $serviceLocator);
     $moduleManager = new ModuleManager($configuration['modules'], $events);
     $moduleManager->setEvent($moduleEvent);
     return $moduleManager;
 }
Beispiel #4
0
 /**
  * Prepare services
  *
  * @param Listener\ServiceListener $serviceListener Service Listener
  * @param ServiceManager           $serviceLocator  Service Manager
  *
  * @return void
  */
 protected function prepareServices($serviceListener, $serviceLocator)
 {
     $serviceListener->addServiceManager($serviceLocator, 'service_manager', 'Zend\\ModuleManager\\Feature\\ServiceProviderInterface', 'getServiceConfig');
     $serviceListener->addServiceManager('ControllerLoader', 'controllers', 'Zend\\ModuleManager\\Feature\\ControllerProviderInterface', 'getControllerConfig');
     $serviceListener->addServiceManager('ControllerPluginManager', 'controller_plugins', 'Zend\\ModuleManager\\Feature\\ControllerPluginProviderInterface', 'getControllerPluginConfig');
     $serviceListener->addServiceManager('ViewHelperManager', 'view_helpers', 'Zend\\ModuleManager\\Feature\\ViewHelperProviderInterface', 'getViewHelperConfig');
     $serviceListener->addServiceManager('ValidatorManager', 'validators', 'Zend\\ModuleManager\\Feature\\ValidatorProviderInterface', 'getValidatorConfig');
     $serviceListener->addServiceManager('FilterManager', 'filters', 'Zend\\ModuleManager\\Feature\\FilterProviderInterface', 'getFilterConfig');
     $serviceListener->addServiceManager('FormElementManager', 'form_elements', 'Zend\\ModuleManager\\Feature\\FormElementProviderInterface', 'getFormElementConfig');
     $serviceListener->addServiceManager('RoutePluginManager', 'route_manager', 'Zend\\ModuleManager\\Feature\\RouteProviderInterface', 'getRouteConfig');
     $serviceListener->addServiceManager('SerializerAdapterManager', 'serializers', 'Zend\\ModuleManager\\Feature\\SerializerProviderInterface', 'getSerializerConfig');
     $serviceListener->addServiceManager('HydratorManager', 'hydrators', 'Zend\\ModuleManager\\Feature\\HydratorProviderInterface', 'getHydratorConfig');
     $serviceListener->addServiceManager('InputFilterManager', 'input_filters', 'Zend\\ModuleManager\\Feature\\InputFilterProviderInterface', 'getInputFilterConfig');
 }