|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Appserver\Core\AbstractManager |
|
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\Core; |
|
23
|
|
|
|
|
24
|
|
|
use Psr\Container\ContainerInterface; |
|
25
|
|
|
use AppserverIo\Storage\StorageInterface; |
|
26
|
|
|
use AppserverIo\Storage\GenericStackable; |
|
27
|
|
|
use AppserverIo\Lang\Reflection\ReflectionClass; |
|
28
|
|
|
use AppserverIo\Psr\Di\ProviderInterface; |
|
29
|
|
|
use AppserverIo\Psr\Application\ManagerInterface; |
|
30
|
|
|
use AppserverIo\Psr\Application\ApplicationInterface; |
|
31
|
|
|
use AppserverIo\Psr\Application\ManagerConfigurationInterface; |
|
32
|
|
|
use AppserverIo\Psr\Naming\InitialContext as NamingDirectory; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Abstract manager implementation. |
|
36
|
|
|
* |
|
37
|
|
|
* @author Tim Wagner <[email protected]> |
|
38
|
|
|
* @author Bernhard Wick <[email protected]> |
|
39
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
40
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
41
|
|
|
* @link https://github.com/appserver-io/appserver |
|
42
|
|
|
* @link http://www.appserver.io |
|
43
|
|
|
* |
|
44
|
|
|
* @property \AppserverIo\Storage\StorageInterface $data Storage container for arbitrary data |
|
45
|
|
|
* @property \AppserverIo\Psr\Naming\InitialContext $initialContext The initial context of our naming directory |
|
46
|
|
|
* @property \AppserverIo\Psr\Application\ApplicationInterface $application The application to manage |
|
47
|
|
|
* @property \AppserverIo\Psr\Application\ManagerConfigurationInterface $managerConfiguration The application to manage |
|
48
|
|
|
*/ |
|
49
|
|
|
abstract class AbstractManager extends GenericStackable implements ManagerInterface, ContainerInterface |
|
50
|
|
|
{ |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Inject the configuration for this manager. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \AppserverIo\Psr\Application\ManagerConfigurationInterface $managerConfiguration The managers configuration |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public function injectManagerConfiguration(ManagerConfigurationInterface $managerConfiguration) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->managerConfiguration = $managerConfiguration; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Inject the data storage. |
|
66
|
|
|
* |
|
67
|
|
|
* @param \AppserverIo\Storage\StorageInterface $data The data storage to use |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function injectData(StorageInterface $data) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->data = $data; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Inject the application instance. |
|
78
|
|
|
* |
|
79
|
|
|
* @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function injectApplication(ApplicationInterface $application) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->application = $application; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the application instance. |
|
90
|
|
|
* |
|
91
|
|
|
* @return \AppserverIo\Psr\Application\ApplicationInterface|\AppserverIo\Psr\Naming\NamingDirectoryInterface The application instance |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getApplication() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->application; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return the storage with the naming directory. |
|
100
|
|
|
* |
|
101
|
|
|
* @return \AppserverIo\Storage\StorageInterface The storage with the naming directory |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getNamingDirectory() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->getApplication()->getNamingDirectory(); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* The global naming directory. |
|
110
|
|
|
* |
|
111
|
|
|
* @param \AppserverIo\Psr\Naming\InitialContext $initialContext The global naming directory |
|
112
|
|
|
* |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
public function injectInitialContext(NamingDirectory $initialContext) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->initialContext = $initialContext; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Returns the global naming directory. |
|
122
|
|
|
* |
|
123
|
|
|
* @return \AppserverIo\Psr\Naming\InitialContext The global naming directory |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getInitialContext() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->initialContext; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Returns the absolute path to the web application. |
|
132
|
|
|
* |
|
133
|
|
|
* @return string The absolute path |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getWebappPath() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->getApplication()->getWebappPath(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Returns the absolute path to the application directory. |
|
142
|
|
|
* |
|
143
|
|
|
* @return string The absolute path to the application directory |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getAppBase() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->getApplication()->getAppBase(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns the absolute path to the application server's base directory. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $directoryToAppend A directory to append to the base directory |
|
154
|
|
|
* |
|
155
|
|
|
* @return string The absolute path the application server's base directory |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getBaseDirectory($directoryToAppend = null) |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->getApplication()->getBaseDirectory($directoryToAppend); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Query's whether or not the attribute is available. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $key The key of the attribute to query for |
|
166
|
|
|
* |
|
167
|
|
|
* @return boolean TRUE if the attribute is set, else FALSE |
|
168
|
|
|
*/ |
|
169
|
|
|
public function hasAttribute($key) |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->data->has($key); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Registers the value with the passed key in the container. |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $key The key to register the value with |
|
178
|
|
|
* @param object $value The value to register |
|
179
|
|
|
* |
|
180
|
|
|
* @return void |
|
181
|
|
|
*/ |
|
182
|
|
|
public function setAttribute($key, $value) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->data->set($key, $value); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Returns the attribute with the passed key from the container. |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $key The key the requested value is registered with |
|
191
|
|
|
* |
|
192
|
|
|
* @return mixed|null The requested value if available |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getAttribute($key) |
|
195
|
|
|
{ |
|
196
|
|
|
if ($this->data->has($key)) { |
|
197
|
|
|
return $this->data->get($key); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Returns a new reflection class intance for the passed class name. |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $className The class name to return the reflection class instance for |
|
205
|
|
|
* |
|
206
|
|
|
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance |
|
207
|
|
|
*/ |
|
208
|
|
|
public function newReflectionClass($className) |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->newReflectionClass($className); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Return's the manager configuration. |
|
215
|
|
|
* |
|
216
|
|
|
* @return \AppserverIo\Psr\Application\ManagerConfigurationInterface The manager configuration |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getManagerConfiguration() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->managerConfiguration; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Returns a reflection class intance for the passed class name. |
|
225
|
|
|
* |
|
226
|
|
|
* @param string $className The class name to return the reflection class instance for |
|
227
|
|
|
* |
|
228
|
|
|
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance |
|
229
|
|
|
* @see \AppserverIo\Psr\Di\ProviderInterface::getReflectionClass() |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getReflectionClass($className) |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->getReflectionClass($className); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Returns a reflection class intance for the passed class name. |
|
238
|
|
|
* |
|
239
|
|
|
* @param object $instance The instance to return the reflection class instance for |
|
240
|
|
|
* |
|
241
|
|
|
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance |
|
242
|
|
|
* @see \AppserverIo\Psr\Di\ProviderInterface::newReflectionClass() |
|
243
|
|
|
* @see \AppserverIo\Psr\Di\ProviderInterface::getReflectionClass() |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getReflectionClassForObject($instance) |
|
246
|
|
|
{ |
|
247
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->getReflectionClassForObject($instance); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Returns a new instance of the passed class name. |
|
252
|
|
|
* |
|
253
|
|
|
* @param string $className The fully qualified class name to return the instance for |
|
254
|
|
|
* @param array $args Arguments to pass to the constructor of the instance |
|
255
|
|
|
* |
|
256
|
|
|
* @return object The instance itself |
|
257
|
|
|
*/ |
|
258
|
|
|
public function newInstance($className, array $args = array()) |
|
259
|
|
|
{ |
|
260
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->newInstance($className, $args); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Finds an entry of the container by its identifier and returns it. |
|
265
|
|
|
* |
|
266
|
|
|
* @param string $id Identifier of the entry to look for |
|
267
|
|
|
* |
|
268
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface No entry was found for **this** identifier. |
|
269
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface Error while retrieving the entry. |
|
270
|
|
|
* |
|
271
|
|
|
* @return mixed Entry. |
|
272
|
|
|
*/ |
|
273
|
|
|
public function get($id) |
|
274
|
|
|
{ |
|
275
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->get($id); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Returns true if the container can return an entry for the given identifier. |
|
280
|
|
|
* Returns false otherwise. |
|
281
|
|
|
* |
|
282
|
|
|
* `has($id)` returning TRUE does not mean that `get($id)` will not throw an exception. |
|
283
|
|
|
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
284
|
|
|
* |
|
285
|
|
|
* @param string $id Identifier of the entry to look for. |
|
286
|
|
|
* |
|
287
|
|
|
* @return boolean TRUE if an entroy for the given identifier exists, else FALSE |
|
288
|
|
|
*/ |
|
289
|
|
|
public function has($id) |
|
290
|
|
|
{ |
|
291
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->has($id); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Register's the passed value with the passed ID. |
|
296
|
|
|
* |
|
297
|
|
|
* @param string $id The ID of the value to add |
|
298
|
|
|
* @param string $value The value to add |
|
299
|
|
|
* |
|
300
|
|
|
* @return void |
|
301
|
|
|
* @throws \AppserverIo\Appserver\DependencyInjectionContainer\ContainerException Is thrown, if a value with the passed key has already been added |
|
302
|
|
|
*/ |
|
303
|
|
|
public function set($id, $value) |
|
304
|
|
|
{ |
|
305
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->set($id, $value); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Query's whether or not an instance of the passed already exists. |
|
310
|
|
|
* |
|
311
|
|
|
* @param string $id Identifier of the entry to look for |
|
312
|
|
|
* |
|
313
|
|
|
* @return boolean TRUE if an instance exists, else FALSE |
|
314
|
|
|
*/ |
|
315
|
|
|
public function exists($id) |
|
316
|
|
|
{ |
|
317
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->exists($id); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Lifecycle callback that'll be invoked after the application has been started. |
|
322
|
|
|
* |
|
323
|
|
|
* @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance |
|
324
|
|
|
* |
|
325
|
|
|
* @return void |
|
326
|
|
|
* @see \AppserverIo\Psr\Application\ManagerInterface::postStartup() |
|
327
|
|
|
*/ |
|
328
|
|
|
public function postStartup(ApplicationInterface $application) |
|
329
|
|
|
{ |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Parse the manager's object descriptors. |
|
334
|
|
|
* |
|
335
|
|
|
* @return void |
|
336
|
|
|
*/ |
|
337
|
|
|
public function parseObjectDescriptors() |
|
338
|
|
|
{ |
|
339
|
|
|
|
|
340
|
|
|
// load the manager configuration |
|
341
|
|
|
/** @var \AppserverIo\Appserver\Core\Api\Node\ManagerNodeInterface $managerConfiguration */ |
|
342
|
|
|
$managerConfiguration = $this->getManagerConfiguration(); |
|
343
|
|
|
|
|
344
|
|
|
// load the object description configuration |
|
345
|
|
|
$objectDescription = $managerConfiguration->getObjectDescription(); |
|
346
|
|
|
|
|
347
|
|
|
// initialize the parsers and start initializing the object descriptors |
|
348
|
|
|
/** @var \AppserverIo\Appserver\Core\Api\Node\ParserNodeInterface */ |
|
349
|
|
|
foreach ($objectDescription->getParsers() as $parserConfiguration) { |
|
350
|
|
|
// query whether or not a factory has been configured |
|
351
|
|
|
if ($parserFactoryConfiguration = $parserConfiguration->getFactory()) { |
|
352
|
|
|
// create a reflection class from the parser factory |
|
353
|
|
|
$reflectionClass = new ReflectionClass($parserFactoryConfiguration); |
|
354
|
|
|
$factory = $reflectionClass->newInstance(); |
|
355
|
|
|
|
|
356
|
|
|
// create the parser instance and start parsing |
|
357
|
|
|
$parser = $factory->createParser($parserConfiguration, $this); |
|
|
|
|
|
|
358
|
|
|
$parser->parse(); |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Returns the managers object descriptors to use. |
|
365
|
|
|
* |
|
366
|
|
|
* @return array The object descriptors |
|
367
|
|
|
*/ |
|
368
|
|
|
public function getDescriptors() |
|
369
|
|
|
{ |
|
370
|
|
|
|
|
371
|
|
|
// load the manager configuration |
|
372
|
|
|
/** @var \AppserverIo\Appserver\Core\Api\Node\ManagerNodeInterface $managerConfiguration */ |
|
373
|
|
|
$managerConfiguration = $this->getManagerConfiguration(); |
|
374
|
|
|
|
|
375
|
|
|
// load the object description configuration |
|
376
|
|
|
$objectDescription = $managerConfiguration->getObjectDescription(); |
|
377
|
|
|
|
|
378
|
|
|
// return the configured object descriptors |
|
379
|
|
|
return $objectDescription->getDescriptors(); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* A dummy functionality to implement the stop functionality. |
|
384
|
|
|
* |
|
385
|
|
|
* @return void |
|
386
|
|
|
* \AppserverIo\Psr\Application\ManagerInterface::stop() |
|
387
|
|
|
*/ |
|
388
|
|
|
public function stop() |
|
389
|
|
|
{ |
|
390
|
|
|
error_log(sprintf('Now shutdown manager "%s"', $this->getIdentifier())); |
|
391
|
|
|
} |
|
392
|
|
|
} |
|
393
|
|
|
|