|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DICIT; |
|
4
|
|
|
|
|
5
|
|
|
use DICIT\Activators\DefaultActivator; |
|
6
|
|
|
use DICIT\Activators\StaticInvocationActivator; |
|
7
|
|
|
use DICIT\Activators\InstanceInvocationActivator; |
|
8
|
|
|
use DICIT\Activators\LazyActivator; |
|
9
|
|
|
use DICIT\Activators\RemoteActivator; |
|
10
|
|
|
use DICIT\Activators\RemoteAdapterFactory; |
|
11
|
|
|
|
|
12
|
|
|
class ActivatorFactory |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
private $activators = array(); |
|
16
|
|
|
private $activatorsDecorators = array(); |
|
17
|
|
|
|
|
18
|
|
|
public function __construct($deferActivations = false) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->addActivator('default', new DefaultActivator(), $deferActivations); |
|
21
|
|
|
$this->addActivator('builder-static', new StaticInvocationActivator(), $deferActivations); |
|
22
|
|
|
$this->addActivator('builder', new InstanceInvocationActivator(), $deferActivations); |
|
23
|
|
|
$this->addActivator('remote', new RemoteActivator(new RemoteAdapterFactory()), $deferActivations); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $key |
|
28
|
|
|
* @param boolean $deferredActivations |
|
29
|
|
|
*/ |
|
30
|
|
|
public function addActivator($key, Activator $activator, $deferredActivations) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($activator instanceof ActivatorDecorator) { |
|
33
|
|
|
$this->activatorsDecorators[$key] = $activator; |
|
34
|
|
|
} else { |
|
35
|
|
|
if ($deferredActivations) { |
|
36
|
|
|
$activator = new LazyActivator($activator); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->activators[$key] = $activator; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $serviceName |
|
46
|
|
|
* @param array $configuration |
|
47
|
|
|
* @throws UnbuildableServiceException |
|
48
|
|
|
* @return \DICIT\Activator |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getActivator($serviceName, array $configuration) |
|
51
|
|
|
{ |
|
52
|
|
|
$activator = null; |
|
53
|
|
|
|
|
54
|
|
|
if (array_key_exists('builder', $configuration)) { |
|
55
|
|
|
$builderType = $this->getBuilderType($configuration['builder']); |
|
56
|
|
|
|
|
57
|
|
|
if ('static' == $builderType) { |
|
58
|
|
|
$activator = $this->activators['builder-static']; |
|
59
|
|
|
} |
|
60
|
|
|
elseif ('instance' == $builderType) { |
|
61
|
|
|
$activator = $this->activators['builder']; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
elseif (array_key_exists('class', $configuration)) { |
|
65
|
|
|
if (array_key_exists('remote', $configuration)) { |
|
66
|
|
|
$activator = $this->activators['remote']; |
|
67
|
|
|
} else { |
|
68
|
|
|
$activator = $this->activators['default']; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($activator == null) { |
|
73
|
|
|
throw new UnbuildableServiceException( |
|
74
|
|
|
sprintf("Unbuildable service : '%s', no suitable activator found.",$serviceName) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
//Decorating the activators if adequate parameters are present |
|
79
|
|
|
foreach($this->activatorsDecorators as $key=>$decorator) { |
|
80
|
|
|
/* @var $decorator ActivatorDecorator */ |
|
81
|
|
|
if (array_key_exists($key, $configuration)) { |
|
82
|
|
|
$dec = clone $decorator; |
|
83
|
|
|
$dec->setNext($activator); |
|
84
|
|
|
$activator = $dec; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $activator; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function getBuilderType($builderKey) |
|
92
|
|
|
{ |
|
93
|
|
|
if (false !== strpos($builderKey, '::')) { |
|
94
|
|
|
return 'static'; |
|
95
|
|
|
} |
|
96
|
|
|
elseif (false !== strpos($builderKey, '->')) { |
|
97
|
|
|
return 'instance'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return 'null'; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|