1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Appserver\DependencyInjectionContainer\ObjectManager |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @author Bernhard Wick <[email protected]> |
16
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
17
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
18
|
|
|
* @link https://github.com/appserver-io/appserver |
19
|
|
|
* @link http://www.appserver.io |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace AppserverIo\Appserver\DependencyInjectionContainer; |
23
|
|
|
|
24
|
|
|
use AppserverIo\Storage\StorageInterface; |
25
|
|
|
use AppserverIo\Appserver\Core\AbstractManager; |
26
|
|
|
use AppserverIo\Psr\Di\ObjectManagerInterface; |
27
|
|
|
use AppserverIo\Psr\Di\UnknownObjectDescriptorException; |
28
|
|
|
use AppserverIo\Psr\Deployment\DescriptorInterface; |
29
|
|
|
use AppserverIo\Psr\Application\ApplicationInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The object manager is necessary to load and provides information about all |
33
|
|
|
* objects related with the application itself. |
34
|
|
|
* |
35
|
|
|
* @author Tim Wagner <[email protected]> |
36
|
|
|
* @author Bernhard Wick <[email protected]> |
37
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
38
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
39
|
|
|
* @link https://github.com/appserver-io/appserver |
40
|
|
|
* @link http://www.appserver.io |
41
|
|
|
* |
42
|
|
|
* @property \AppserverIo\Storage\StorageInterface $objectDescriptors Storage for our collected object descriptors |
43
|
|
|
*/ |
44
|
|
|
class ObjectManager extends AbstractManager implements ObjectManagerInterface |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Inject the storage for the object descriptors. |
49
|
|
|
* |
50
|
|
|
* @param \AppserverIo\Storage\StorageInterface $objectDescriptors The storage for the object descriptors |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function injectObjectDescriptors(StorageInterface $objectDescriptors) |
55
|
|
|
{ |
56
|
|
|
$this->objectDescriptors = $objectDescriptors; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the storage with the object descriptors. |
61
|
|
|
* |
62
|
|
|
* @return \AppserverIo\Storage\StorageInterface The storage with the object descriptors |
63
|
|
|
*/ |
64
|
|
|
public function getObjectDescriptors() |
65
|
|
|
{ |
66
|
|
|
return $this->objectDescriptors; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Has been automatically invoked by the container after the application |
71
|
|
|
* instance has been created. |
72
|
|
|
* |
73
|
|
|
* @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
* @see \AppserverIo\Psr\Application\ManagerInterface::initialize() |
77
|
|
|
*/ |
78
|
|
|
public function initialize(ApplicationInterface $application) |
79
|
|
|
{ |
80
|
|
|
|
81
|
|
|
// parse the object descriptors |
82
|
|
|
$this->parseObjectDescriptors(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds the passed object descriptor to the object manager. If the merge flag is TRUE, then |
87
|
|
|
* we check if already an object descriptor for the class exists before they will be merged. |
88
|
|
|
* |
89
|
|
|
* When we merge object descriptors this means, that the values of the passed descriptor |
90
|
|
|
* will override the existing ones. |
91
|
|
|
* |
92
|
|
|
* @param \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor The object descriptor to add |
93
|
|
|
* @param boolean $merge TRUE if we want to merge with an existing object descriptor |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function addPreference(DescriptorInterface $objectDescriptor, $merge = false) |
98
|
|
|
{ |
99
|
|
|
|
100
|
|
|
// query whether or not an existing preference has to be overwritten |
101
|
|
|
if ($this->hasAttribute($interface = $objectDescriptor->getInterface()) && !$merge) { |
|
|
|
|
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// add the new preference |
106
|
|
|
$this->setAttribute($interface, $objectDescriptor->getClassName()); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return's the preference for the passed class name. |
111
|
|
|
* |
112
|
|
|
* @param string $className The class name to return the preference for |
113
|
|
|
* |
114
|
|
|
* @return string The preference or the original class name |
115
|
|
|
*/ |
116
|
|
|
public function getPreference($className) |
117
|
|
|
{ |
118
|
|
|
|
119
|
|
|
// query whether or not we've a preference |
120
|
|
|
if ($this->hasAttribute($className)) { |
121
|
|
|
return $this->getAttribute($className); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// if not, return the original class name |
125
|
|
|
return $className; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Adds the passed object descriptor to the object manager. If the merge flag is TRUE, then |
130
|
|
|
* we check if already an object descriptor for the class exists before they will be merged. |
131
|
|
|
* |
132
|
|
|
* When we merge object descriptors this means, that the values of the passed descriptor |
133
|
|
|
* will override the existing ones. |
134
|
|
|
* |
135
|
|
|
* @param \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor The object descriptor to add |
136
|
|
|
* @param boolean $merge TRUE if we want to merge with an existing object descriptor |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function addObjectDescriptor(DescriptorInterface $objectDescriptor, $merge = false) |
141
|
|
|
{ |
142
|
|
|
|
143
|
|
|
// query whether we've to merge the configuration found in annotations |
144
|
|
|
if ($this->hasObjectDescriptor($objectDescriptor->getName()) && $merge) { |
|
|
|
|
145
|
|
|
// load the existing descriptor |
146
|
|
|
$existingDescriptor = $this->getObjectDescriptor($objectDescriptor->getName()); |
147
|
|
|
$existingDescriptorType = get_class($existingDescriptor); |
148
|
|
|
// log on info level to make overwriting more obvious |
149
|
|
|
$this->getApplication()->getInitialContext()->getSystemLogger()->debug( |
|
|
|
|
150
|
|
|
sprintf( |
151
|
|
|
'Overriding descriptor "%s" of webapp "%s" from XML configuration.', |
152
|
|
|
$existingDescriptor->getName(), |
153
|
|
|
$this->getApplication()->getName() |
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
// Merge the descriptors. XML configuration overrides values from annotation but we have to make sure |
158
|
|
|
// that the descriptors are of the same type. If not we have to take the XML descriptor as our new base descriptor |
159
|
|
|
// and merge the annotations (but XML still has to be dominant) |
160
|
|
|
$mergedDescriptor = null; |
161
|
|
|
if (!$objectDescriptor instanceof $existingDescriptorType) { |
162
|
|
|
// create a temporary clone of the object (XML) descriptor to merge the existing descriptor into an instance with the same type |
163
|
|
|
$mergedDescriptor = clone $objectDescriptor; |
164
|
|
|
$mergedDescriptor->merge($existingDescriptor); |
|
|
|
|
165
|
|
|
$mergedDescriptor->merge($objectDescriptor); |
166
|
|
|
|
|
|
|
|
167
|
|
|
} else { |
168
|
|
|
// merge the object descriptor into the existing one |
169
|
|
|
$mergedDescriptor = $existingDescriptor; |
170
|
|
|
$mergedDescriptor->merge($objectDescriptor); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// re-register the merged descriptor |
174
|
|
|
$this->setObjectDescriptor($mergedDescriptor); |
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
} else { |
177
|
|
|
// register the object descriptor |
178
|
|
|
$this->setObjectDescriptor($objectDescriptor); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Registers the passed object descriptor under its class name. |
184
|
|
|
* |
185
|
|
|
* @param \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor The object descriptor to set |
186
|
|
|
* |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
public function setObjectDescriptor(DescriptorInterface $objectDescriptor) |
190
|
|
|
{ |
191
|
|
|
$this->objectDescriptors->set($objectDescriptor->getName(), $objectDescriptor); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Query if we've an object descriptor for the passed name. |
196
|
|
|
* |
197
|
|
|
* @param string $name The name we query for a object descriptor |
198
|
|
|
* |
199
|
|
|
* @return boolean TRUE if an object descriptor has been registered, else FALSE |
200
|
|
|
*/ |
201
|
|
|
public function hasObjectDescriptor($name) |
202
|
|
|
{ |
203
|
|
|
return $this->objectDescriptors->has($name); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Returns the object descriptor if we've registered it. |
208
|
|
|
* |
209
|
|
|
* @param string $name The name we want to return the object descriptor for |
210
|
|
|
* |
211
|
|
|
* @return \AppserverIo\Psr\Deployment\DescriptorInterface|null The requested object descriptor instance |
212
|
|
|
* @throws \AppserverIo\Psr\Di\UnknownObjectDescriptorException Is thrown if someone tries to access an unknown object desciptor |
213
|
|
|
*/ |
214
|
|
|
public function getObjectDescriptor($name) |
215
|
|
|
{ |
216
|
|
|
|
217
|
|
|
// query if we've an object descriptor registered |
218
|
|
|
if ($this->hasObjectDescriptor($name)) { |
219
|
|
|
// return the object descriptor |
220
|
|
|
return $this->objectDescriptors->get($name); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// throw an exception is object descriptor has not been registered |
224
|
|
|
throw new UnknownObjectDescriptorException(sprintf('Object Descriptor with name "%s" has not been registered', $name)); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns the identifier for the object manager instance. |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
* @see \AppserverIo\Psr\Application\ManagerInterface::getIdentifier() |
232
|
|
|
*/ |
233
|
|
|
public function getIdentifier() |
234
|
|
|
{ |
235
|
|
|
return ObjectManagerInterface::IDENTIFIER; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|