3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Service
;
12 use Zend\EventManager\EventManagerAwareInterface
;
13 use Zend\EventManager\EventManagerInterface
;
14 use Zend\ServiceManager\ConfigInterface
;
15 use Zend\ServiceManager\ServiceLocatorAwareInterface
;
16 use Zend\ServiceManager\ServiceManager
;
17 use Zend\ServiceManager\ServiceManagerAwareInterface
;
19 class ServiceManagerConfig
implements ConfigInterface
22 * Services that can be instantiated without factories
26 protected $invokables = array(
27 'SharedEventManager' => 'Zend\EventManager\SharedEventManager',
35 protected $factories = array(
36 'EventManager' => 'Zend\Mvc\Service\EventManagerFactory',
37 'ModuleManager' => 'Zend\Mvc\Service\ModuleManagerFactory',
45 protected $abstractFactories = array();
52 protected $aliases = array(
53 'Zend\EventManager\EventManagerInterface' => 'EventManager',
59 * Services are shared by default; this is primarily to indicate services
60 * that should NOT be shared
64 protected $shared = array(
65 'EventManager' => false,
71 * Merges internal arrays with those passed via configuration
73 * @param array $configuration
75 public function __construct(array $configuration = array())
77 if (isset($configuration['invokables'])) {
78 $this->invokables
= array_merge($this->invokables
, $configuration['invokables']);
81 if (isset($configuration['factories'])) {
82 $this->factories
= array_merge($this->factories
, $configuration['factories']);
85 if (isset($configuration['abstract_factories'])) {
86 $this->abstractFactories
= array_merge($this->abstractFactories
, $configuration['abstract_factories']);
89 if (isset($configuration['aliases'])) {
90 $this->aliases
= array_merge($this->aliases
, $configuration['aliases']);
93 if (isset($configuration['shared'])) {
94 $this->shared
= array_merge($this->shared
, $configuration['shared']);
100 * Configure the provided service manager instance with the configuration
103 * In addition to using each of the internal properties to configure the
104 * service manager, also adds an initializer to inject ServiceManagerAware
105 * and ServiceLocatorAware classes with the service manager.
107 * @param ServiceManager $serviceManager
110 public function configureServiceManager(ServiceManager
$serviceManager)
112 foreach ($this->invokables
as $name => $class) {
113 $serviceManager->setInvokableClass($name, $class);
116 foreach ($this->factories
as $name => $factoryClass) {
117 $serviceManager->setFactory($name, $factoryClass);
120 foreach ($this->abstractFactories
as $factoryClass) {
121 $serviceManager->addAbstractFactory($factoryClass);
124 foreach ($this->aliases
as $name => $service) {
125 $serviceManager->setAlias($name, $service);
128 foreach ($this->shared
as $name => $value) {
129 $serviceManager->setShared($name, $value);
132 $serviceManager->addInitializer(function ($instance) use ($serviceManager) {
133 if ($instance instanceof EventManagerAwareInterface
) {
134 if ($instance->getEventManager() instanceof EventManagerInterface
) {
135 $instance->getEventManager()->setSharedManager(
136 $serviceManager->get('SharedEventManager')
139 $instance->setEventManager($serviceManager->get('EventManager'));
144 $serviceManager->addInitializer(function ($instance) use ($serviceManager) {
145 if ($instance instanceof ServiceManagerAwareInterface
) {
146 $instance->setServiceManager($serviceManager);
150 $serviceManager->addInitializer(function ($instance) use ($serviceManager) {
151 if ($instance instanceof ServiceLocatorAwareInterface
) {
152 $instance->setServiceLocator($serviceManager);
156 $serviceManager->setService('ServiceManager', $serviceManager);
157 $serviceManager->setAlias('Zend\ServiceManager\ServiceLocatorInterface', 'ServiceManager');
158 $serviceManager->setAlias('Zend\ServiceManager\ServiceManager', 'ServiceManager');