This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
ServiceManagerConfig.php
160 lines (138 loc) · 5.01 KB
/
ServiceManagerConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Mvc\Service;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
class ServiceManagerConfig implements ConfigInterface
{
/**
* Services that can be instantiated without factories
*
* @var array
*/
protected $invokables = array(
'SharedEventManager' => 'Zend\EventManager\SharedEventManager',
);
/**
* Service factories
*
* @var array
*/
protected $factories = array(
'EventManager' => 'Zend\Mvc\Service\EventManagerFactory',
'ModuleManager' => 'Zend\Mvc\Service\ModuleManagerFactory',
);
/**
* Abstract factories
*
* @var array
*/
protected $abstractFactories = array();
/**
* Aliases
*
* @var array
*/
protected $aliases = array(
'Zend\EventManager\EventManagerInterface' => 'EventManager',
);
/**
* Shared services
*
* Services are shared by default; this is primarily to indicate services
* that should NOT be shared
*
* @var array
*/
protected $shared = array(
'EventManager' => false,
);
/**
* Constructor
*
* Merges internal arrays with those passed via configuration
*
* @param array $configuration
*/
public function __construct(array $configuration = array())
{
if (isset($configuration['invokables'])) {
$this->invokables = array_merge($this->invokables, $configuration['invokables']);
}
if (isset($configuration['factories'])) {
$this->factories = array_merge($this->factories, $configuration['factories']);
}
if (isset($configuration['abstract_factories'])) {
$this->abstractFactories = array_merge($this->abstractFactories, $configuration['abstract_factories']);
}
if (isset($configuration['aliases'])) {
$this->aliases = array_merge($this->aliases, $configuration['aliases']);
}
if (isset($configuration['shared'])) {
$this->shared = array_merge($this->shared, $configuration['shared']);
}
}
/**
* Configure the provided service manager instance with the configuration
* in this class.
*
* In addition to using each of the internal properties to configure the
* service manager, also adds an initializer to inject ServiceManagerAware
* and ServiceLocatorAware classes with the service manager.
*
* @param ServiceManager $serviceManager
* @return void
*/
public function configureServiceManager(ServiceManager $serviceManager)
{
foreach ($this->invokables as $name => $class) {
$serviceManager->setInvokableClass($name, $class);
}
foreach ($this->factories as $name => $factoryClass) {
$serviceManager->setFactory($name, $factoryClass);
}
foreach ($this->abstractFactories as $factoryClass) {
$serviceManager->addAbstractFactory($factoryClass);
}
foreach ($this->aliases as $name => $service) {
$serviceManager->setAlias($name, $service);
}
foreach ($this->shared as $name => $value) {
$serviceManager->setShared($name, $value);
}
$serviceManager->addInitializer(function ($instance) use ($serviceManager) {
if ($instance instanceof EventManagerAwareInterface) {
if ($instance->getEventManager() instanceof EventManagerInterface) {
$instance->getEventManager()->setSharedManager(
$serviceManager->get('SharedEventManager')
);
} else {
$instance->setEventManager($serviceManager->get('EventManager'));
}
}
});
$serviceManager->addInitializer(function ($instance) use ($serviceManager) {
if ($instance instanceof ServiceManagerAwareInterface) {
$instance->setServiceManager($serviceManager);
}
});
$serviceManager->addInitializer(function ($instance) use ($serviceManager) {
if ($instance instanceof ServiceLocatorAwareInterface) {
$instance->setServiceLocator($serviceManager);
}
});
$serviceManager->setService('ServiceManager', $serviceManager);
$serviceManager->setAlias('Zend\ServiceManager\ServiceLocatorInterface', 'ServiceManager');
$serviceManager->setAlias('Zend\ServiceManager\ServiceManager', 'ServiceManager');
}
}