1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2014-05-26 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\Locator\Configuration; |
8
|
|
|
|
9
|
|
|
use Net\Bazzline\Component\Locator\MethodBodyBuilder\FetchFromFactoryInstancePoolBuilder; |
10
|
|
|
use Net\Bazzline\Component\Locator\MethodBodyBuilder\FetchFromSharedInstancePoolBuilder; |
11
|
|
|
use Net\Bazzline\Component\Locator\MethodBodyBuilder\FetchFromSharedInstancePoolOrCreateByFactoryBuilder; |
12
|
|
|
use Net\Bazzline\Component\Locator\MethodBodyBuilder\MethodBodyBuilderInterface; |
13
|
|
|
use Net\Bazzline\Component\Locator\MethodBodyBuilder\NewInstanceBuilder; |
14
|
|
|
use Net\Bazzline\Component\Locator\RuntimeException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Interface Configuration |
18
|
|
|
* @package Net\Bazzline\Component\Locator\Configuration\Assembler |
19
|
|
|
*/ |
20
|
|
|
class Configuration |
21
|
|
|
{ |
22
|
|
|
//begin of properties |
23
|
|
|
/** @var string */ |
24
|
|
|
private $extends; |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
private $instances = array(); |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
private $implements = array(); |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $className; |
34
|
|
|
|
35
|
|
|
/** @var bool */ |
36
|
|
|
private $createLocatorGeneratorInterface = false; |
37
|
|
|
|
38
|
|
|
/** @var FetchFromFactoryInstancePoolBuilder */ |
39
|
|
|
private $fetchFromFactoryInstancePoolBuilder; |
40
|
|
|
|
41
|
|
|
/** @var FetchFromSharedInstancePoolBuilder */ |
42
|
|
|
private $fetchFromSharedInstancePoolBuilder; |
43
|
|
|
|
44
|
|
|
/** @var FetchFromSharedInstancePoolOrCreateByFactoryBuilder */ |
45
|
|
|
private $fetchFromSharedInstancePoolOrCreateByFactoryBuilder; |
46
|
|
|
|
47
|
|
|
/** @var bool */ |
48
|
|
|
private $hasFactoryInstances = false; |
49
|
|
|
|
50
|
|
|
/** @var bool */ |
51
|
|
|
private $hasSharedInstances = false; |
52
|
|
|
|
53
|
|
|
/** @var Instance */ |
54
|
|
|
private $instance; |
55
|
|
|
|
56
|
|
|
/** @var string */ |
57
|
|
|
private $namespace; |
58
|
|
|
|
59
|
|
|
/** @var NewInstanceBuilder */ |
60
|
|
|
private $newInstanceBuilder; |
61
|
|
|
|
62
|
|
|
/** @var string */ |
63
|
|
|
private $methodPrefix; |
64
|
|
|
|
65
|
|
|
/** @var array */ |
66
|
|
|
private $methodBodyBuilderInstancePool = array(); |
67
|
|
|
|
68
|
|
|
/** @var string */ |
69
|
|
|
private $fileName; |
70
|
|
|
|
71
|
|
|
/** @var string */ |
72
|
|
|
private $filePath; |
73
|
|
|
|
74
|
|
|
/** @var array */ |
75
|
|
|
private $useCollection = array(); |
76
|
|
|
|
77
|
|
|
/** @var Uses */ |
78
|
|
|
private $uses; |
79
|
|
|
//end of properties |
80
|
|
|
|
81
|
|
|
//begin of public methods |
82
|
|
|
/** |
83
|
|
|
* @return null|string |
84
|
|
|
*/ |
85
|
|
|
public function getClassName() |
86
|
|
|
{ |
87
|
|
|
return $this->className; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $name |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function setClassName($name) |
95
|
|
|
{ |
96
|
|
|
$this->className = (string) $name; |
97
|
|
|
$this->fileName = $this->className . $this->getFileNameExtension(); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function createLocatorGeneratorInterface() |
106
|
|
|
{ |
107
|
|
|
return $this->createLocatorGeneratorInterface; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param bool $flag |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function setCreateLocatorGeneratorInterface($flag = false) |
115
|
|
|
{ |
116
|
|
|
$this->createLocatorGeneratorInterface = (bool) $flag; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return null|string |
123
|
|
|
*/ |
124
|
|
|
public function getFileName() |
125
|
|
|
{ |
126
|
|
|
return $this->fileName; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getFileNameExtension() |
133
|
|
|
{ |
134
|
|
|
return '.php'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return null|string |
139
|
|
|
*/ |
140
|
|
|
public function getFilePath() |
141
|
|
|
{ |
142
|
|
|
return $this->filePath; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $path |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function setFilePath($path) |
150
|
|
|
{ |
151
|
|
|
$this->filePath = (string) $path; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param Instance $instance |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function setInstance(Instance $instance) |
161
|
|
|
{ |
162
|
|
|
$this->instance = $instance; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param Uses $uses |
169
|
|
|
* @return $this |
170
|
|
|
*/ |
171
|
|
|
public function setUses(Uses $uses) |
172
|
|
|
{ |
173
|
|
|
$this->uses = $uses; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return null|string |
180
|
|
|
*/ |
181
|
|
|
public function getNamespace() |
182
|
|
|
{ |
183
|
|
|
return $this->namespace; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
public function hasNamespace() |
190
|
|
|
{ |
191
|
|
|
return (is_string($this->namespace)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $namespace |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
|
|
public function setNamespace($namespace) |
199
|
|
|
{ |
200
|
|
|
$namespace = trim((string) $namespace); |
201
|
|
|
|
202
|
|
|
if (strlen($namespace) > 0) { |
203
|
|
|
$this->namespace = $namespace; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $methodPrefix |
211
|
|
|
* @return $this |
212
|
|
|
*/ |
213
|
|
|
public function setMethodPrefix($methodPrefix) |
214
|
|
|
{ |
215
|
|
|
$this->methodPrefix = (string) $methodPrefix; |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return null|string |
222
|
|
|
*/ |
223
|
|
|
public function getMethodPrefix() |
224
|
|
|
{ |
225
|
|
|
return $this->methodPrefix; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param string $className |
230
|
|
|
* @param bool $isFactory |
231
|
|
|
* @param bool $isShared |
232
|
|
|
* @param string $returnValue |
233
|
|
|
* @param string $alias |
234
|
|
|
* @param null|string $methodBodyBuilderClassName |
235
|
|
|
* @return $this |
236
|
|
|
* @throws RuntimeException |
237
|
|
|
*/ |
238
|
|
|
public function addInstance($className, $isFactory, $isShared, $returnValue, $alias, $methodBodyBuilderClassName = null) |
239
|
|
|
{ |
240
|
|
|
$instance = $this->getNewInstance(); |
241
|
|
|
|
242
|
|
|
if ($isFactory) { |
243
|
|
|
$this->enableHasFactoryInstances(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if ($isShared) { |
247
|
|
|
$this->hasSharedInstances = true; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$instance->setAlias($alias); |
251
|
|
|
$instance->setClassName($className); |
252
|
|
|
$instance->setIsFactory($isFactory); |
253
|
|
|
$instance->setIsShared($isShared); |
254
|
|
|
$instance->setReturnValue($returnValue); |
255
|
|
|
|
256
|
|
|
if (!is_null($methodBodyBuilderClassName)) { |
257
|
|
|
$methodBodyBuilder = $this->fetchFromMethodBodyBuilderInstancePool($methodBodyBuilderClassName); |
258
|
|
|
$methodBodyBuilder->setInstance($instance); |
259
|
|
|
$instance->setMethodBodyBuilder($methodBodyBuilder); |
260
|
|
|
} else { |
261
|
|
|
$instance = $this->tryToDetermineMethodBodyBuilder($instance); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$this->instances[] = $instance; |
265
|
|
|
|
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return array|Instance[] |
271
|
|
|
*/ |
272
|
|
|
public function getInstances() |
273
|
|
|
{ |
274
|
|
|
return $this->instances; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return boolean |
279
|
|
|
*/ |
280
|
|
|
public function hasInstances() |
281
|
|
|
{ |
282
|
|
|
return (!empty($this->instances)); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return bool |
287
|
|
|
*/ |
288
|
|
|
public function hasFactoryInstances() |
289
|
|
|
{ |
290
|
|
|
return $this->hasFactoryInstances; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @return bool |
295
|
|
|
*/ |
296
|
|
|
public function hasSharedInstances() |
297
|
|
|
{ |
298
|
|
|
return $this->hasSharedInstances; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param string $interfaceName |
303
|
|
|
* @return $this |
304
|
|
|
*/ |
305
|
|
|
public function addImplements($interfaceName) |
306
|
|
|
{ |
307
|
|
|
$this->implements[] = $interfaceName; |
308
|
|
|
|
309
|
|
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @return array |
314
|
|
|
*/ |
315
|
|
|
public function getImplements() |
316
|
|
|
{ |
317
|
|
|
return $this->implements; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @return boolean |
322
|
|
|
*/ |
323
|
|
|
public function hasImplements() |
324
|
|
|
{ |
325
|
|
|
return (!empty($this->implements)); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param string $className |
330
|
|
|
* @return $this |
331
|
|
|
*/ |
332
|
|
|
public function setExtends($className) |
333
|
|
|
{ |
334
|
|
|
$this->extends = (string) $className; |
335
|
|
|
|
336
|
|
|
return $this; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @return bool |
341
|
|
|
*/ |
342
|
|
|
public function hasExtends() |
343
|
|
|
{ |
344
|
|
|
return (is_string($this->extends)); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @return null|string |
349
|
|
|
*/ |
350
|
|
|
public function getExtends() |
351
|
|
|
{ |
352
|
|
|
return $this->extends; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @param string $className |
357
|
|
|
* @param string $alias |
358
|
|
|
* @return $this |
359
|
|
|
*/ |
360
|
|
|
public function addUses($className, $alias = '') |
361
|
|
|
{ |
362
|
|
|
$uses = $this->getNewUses(); |
363
|
|
|
|
364
|
|
|
$uses->setAlias($alias); |
365
|
|
|
$uses->setClassName($className); |
366
|
|
|
|
367
|
|
|
$this->useCollection[] = $uses; |
368
|
|
|
|
369
|
|
|
return $this; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @return bool |
374
|
|
|
*/ |
375
|
|
|
public function hasUses() |
376
|
|
|
{ |
377
|
|
|
return (!empty($this->useCollection)); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @return array|Uses[] |
382
|
|
|
*/ |
383
|
|
|
public function getUseCollection() |
384
|
|
|
{ |
385
|
|
|
return $this->useCollection; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @param FetchFromFactoryInstancePoolBuilder $fetchFromFactoryInstancePoolBuilder |
390
|
|
|
* @return $this |
391
|
|
|
*/ |
392
|
|
|
public function setFetchFromFactoryInstancePoolBuilder(FetchFromFactoryInstancePoolBuilder $fetchFromFactoryInstancePoolBuilder) |
393
|
|
|
{ |
394
|
|
|
$this->fetchFromFactoryInstancePoolBuilder = $fetchFromFactoryInstancePoolBuilder; |
395
|
|
|
|
396
|
|
|
return $this; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @param FetchFromSharedInstancePoolBuilder $fetchFromSharedInstancePoolBuilder |
401
|
|
|
* @return $this |
402
|
|
|
*/ |
403
|
|
|
public function setFetchFromSharedInstancePoolBuilder(FetchFromSharedInstancePoolBuilder $fetchFromSharedInstancePoolBuilder) |
404
|
|
|
{ |
405
|
|
|
$this->fetchFromSharedInstancePoolBuilder = $fetchFromSharedInstancePoolBuilder; |
406
|
|
|
|
407
|
|
|
return $this; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @param FetchFromSharedInstancePoolOrCreateByFactoryBuilder $fetchFromSharedInstancePoolOrCreateByFactoryBuilder |
412
|
|
|
* @return $this |
413
|
|
|
*/ |
414
|
|
|
public function setFetchFromSharedInstancePoolOrCreateByFactoryBuilder(FetchFromSharedInstancePoolOrCreateByFactoryBuilder $fetchFromSharedInstancePoolOrCreateByFactoryBuilder) |
415
|
|
|
{ |
416
|
|
|
$this->fetchFromSharedInstancePoolOrCreateByFactoryBuilder = $fetchFromSharedInstancePoolOrCreateByFactoryBuilder; |
417
|
|
|
|
418
|
|
|
return $this; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @param NewInstanceBuilder $newInstanceBuilder |
423
|
|
|
* @return $this |
424
|
|
|
*/ |
425
|
|
|
public function setNewInstanceBuilder(NewInstanceBuilder $newInstanceBuilder) |
426
|
|
|
{ |
427
|
|
|
$this->newInstanceBuilder = $newInstanceBuilder; |
428
|
|
|
|
429
|
|
|
return $this; |
430
|
|
|
} |
431
|
|
|
//end of public methods |
432
|
|
|
|
433
|
|
|
//begin of private methods |
434
|
|
|
/** |
435
|
|
|
* @return Uses |
436
|
|
|
*/ |
437
|
|
|
private function getNewUses() |
438
|
|
|
{ |
439
|
|
|
return clone $this->uses; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* @return Instance |
444
|
|
|
*/ |
445
|
|
|
private function getNewInstance() |
446
|
|
|
{ |
447
|
|
|
return clone $this->instance; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* @param Instance $instance |
452
|
|
|
* @return Instance |
453
|
|
|
* @throws RuntimeException |
454
|
|
|
*/ |
455
|
|
|
private function tryToDetermineMethodBodyBuilder(Instance $instance) |
456
|
|
|
{ |
457
|
|
|
$isUniqueInvokableInstance = ((!$instance->isFactory()) && (!$instance->isShared())); |
|
|
|
|
458
|
|
|
$isUniqueInvokableFactorizedInstance = (($instance->isFactory()) && (!$instance->isShared())); |
|
|
|
|
459
|
|
|
$isSharedInvokableInstance = ((!$instance->isFactory()) && ($instance->isShared())); |
|
|
|
|
460
|
|
|
$isSharedInvokableFactorizedInstance = (($instance->isFactory()) && ($instance->isShared())); |
461
|
|
|
|
462
|
|
|
if ($isUniqueInvokableInstance) { |
463
|
|
|
$instance->setMethodBodyBuilder($this->newInstanceBuilder); |
464
|
|
|
} else if ($isUniqueInvokableFactorizedInstance) { |
465
|
|
|
$instance->setMethodBodyBuilder($this->fetchFromFactoryInstancePoolBuilder); |
466
|
|
|
} else if ($isSharedInvokableInstance) { |
467
|
|
|
$instance->setMethodBodyBuilder($this->fetchFromSharedInstancePoolBuilder); |
468
|
|
|
} else if ($isSharedInvokableFactorizedInstance) { |
469
|
|
|
$instance->setMethodBodyBuilder($this->fetchFromSharedInstancePoolOrCreateByFactoryBuilder); |
470
|
|
|
} else { |
471
|
|
|
throw new RuntimeException( |
472
|
|
|
'could not determine method body builder for instance.' . PHP_EOL . |
473
|
|
|
'please set a method_body_builder for following instance: ' . PHP_EOL . |
474
|
|
|
var_export($instance, true) |
475
|
|
|
); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
return $instance; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* @param $methodBodyBuilderClassName |
483
|
|
|
* @return \Net\Bazzline\Component\Locator\MethodBodyBuilder\MethodBodyBuilderInterface |
484
|
|
|
* @throws RuntimeException |
485
|
|
|
*/ |
486
|
|
|
private function fetchFromMethodBodyBuilderInstancePool($methodBodyBuilderClassName) |
487
|
|
|
{ |
488
|
|
|
$key = sha1($methodBodyBuilderClassName); |
489
|
|
|
|
490
|
|
|
if (!isset($this->methodBodyBuilderInstancePool[$key])) { |
491
|
|
|
if (class_exists($methodBodyBuilderClassName)) { |
492
|
|
|
$methodBodyBuilder = new $methodBodyBuilderClassName(); |
493
|
|
|
if (!$methodBodyBuilder instanceof MethodBodyBuilderInterface) { |
494
|
|
|
throw new RuntimeException( |
495
|
|
|
'provided method body builder class name "' . $methodBodyBuilderClassName . |
496
|
|
|
'" does not implements MethodBodyBuilderInterface' |
497
|
|
|
); |
498
|
|
|
} |
499
|
|
|
$this->methodBodyBuilderInstancePool[$key] = $methodBodyBuilder; |
500
|
|
|
} else { |
501
|
|
|
throw new RuntimeException( |
502
|
|
|
'provided method_body_builder_class_name "' . $methodBodyBuilderClassName . '" does not exists' |
503
|
|
|
); |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
return $this->methodBodyBuilderInstancePool[$key]; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
private function enableHasFactoryInstances() |
511
|
|
|
{ |
512
|
|
|
if (!$this->hasFactoryInstances) { |
513
|
|
|
$this->hasFactoryInstances = true; |
514
|
|
|
$this->addUses('Net\Bazzline\Component\Locator\FactoryInterface'); |
515
|
|
|
} |
516
|
|
|
} |
517
|
|
|
//end of private methods |
518
|
|
|
} |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.