1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Compose\Definition; |
5
|
|
|
|
6
|
|
|
use Innmind\Compose\{ |
7
|
|
|
Definition\Dependency\Parameter, |
8
|
|
|
Definition\Service\Argument, |
9
|
|
|
Services, |
10
|
|
|
Lazy, |
11
|
|
|
Exception\ReferenceNotFound, |
12
|
|
|
Exception\ArgumentNotExtractable, |
13
|
|
|
Compilation\Dependency as CompiledDependency |
14
|
|
|
}; |
15
|
|
|
use Innmind\Immutable\{ |
16
|
|
|
Set, |
17
|
|
|
MapInterface, |
18
|
|
|
Map, |
19
|
|
|
StreamInterface |
20
|
|
|
}; |
21
|
|
|
|
22
|
|
|
final class Dependency |
23
|
|
|
{ |
24
|
|
|
private $name; |
25
|
|
|
private $services; |
26
|
|
|
private $parameters; |
27
|
|
|
|
28
|
67 |
|
public function __construct( |
29
|
|
|
Name $name, |
30
|
|
|
Services $services, |
31
|
|
|
Parameter ...$parameters |
32
|
|
|
) { |
33
|
67 |
|
$this->name = $name; |
34
|
67 |
|
$this->services = $services; |
35
|
67 |
|
$this->parameters = Set::of(Parameter::class, ...$parameters); |
36
|
67 |
|
} |
37
|
|
|
|
38
|
42 |
|
public function name(): Name |
39
|
|
|
{ |
40
|
42 |
|
return $this->name; |
41
|
|
|
} |
42
|
|
|
|
43
|
9 |
|
public function bind(Services $services): self |
44
|
|
|
{ |
45
|
|
|
$parameters = $this |
46
|
9 |
|
->parameters |
47
|
9 |
|
->reduce( |
48
|
9 |
|
new Map('string', 'mixed'), |
49
|
9 |
|
static function(Map $parameters, Parameter $parameter) use ($services): Map { |
50
|
9 |
|
return $parameters->put( |
51
|
9 |
|
(string) $parameter->name(), |
52
|
9 |
|
$parameter->resolve($services) |
53
|
|
|
); |
54
|
9 |
|
} |
55
|
|
|
); |
56
|
|
|
|
57
|
9 |
|
$self = clone $this; |
58
|
9 |
|
$self->services = $self->services->inject($parameters); |
59
|
|
|
|
60
|
9 |
|
return $self; |
61
|
|
|
} |
62
|
|
|
|
63
|
22 |
|
public function lazy(Name $name): Lazy |
64
|
|
|
{ |
65
|
22 |
|
if (!$this->has($name)) { |
66
|
7 |
|
throw new ReferenceNotFound((string) $name); |
67
|
|
|
} |
68
|
|
|
|
69
|
15 |
|
return Lazy::service( |
70
|
15 |
|
$name, |
71
|
15 |
|
$this->services |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
public function build(Name $name): object |
76
|
|
|
{ |
77
|
5 |
|
return $this->lazy($name)->load(); |
78
|
|
|
} |
79
|
|
|
|
80
|
50 |
|
public function has(Name $name): bool |
81
|
|
|
{ |
82
|
50 |
|
if (!$this->services->has($name)) { |
83
|
6 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
45 |
|
$service = $this->services->get($name); |
87
|
|
|
|
88
|
45 |
|
if (!$service->exposed() || !$service->isExposedAs($name)) { |
89
|
12 |
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
34 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
public function dependsOn(self $other): bool |
96
|
|
|
{ |
97
|
5 |
|
return $this->parameters->reduce( |
98
|
5 |
|
false, |
99
|
5 |
|
function(bool $dependsOn, Parameter $parameter) use ($other): bool { |
100
|
3 |
|
return $dependsOn || $parameter->refersTo($other); |
101
|
5 |
|
} |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
15 |
|
public function decorate( |
106
|
|
|
Name $decorator, |
107
|
|
|
Name $decorated, |
108
|
|
|
Name $newName = null |
109
|
|
|
): Service { |
110
|
15 |
|
if (!$this->has($decorator)) { |
111
|
4 |
|
throw new ReferenceNotFound((string) $decorator); |
112
|
|
|
} |
113
|
|
|
|
114
|
11 |
|
$service = $this->services->get($decorator); |
115
|
|
|
|
116
|
11 |
|
return $service->tunnel($this->name, $decorated, $newName); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param StreamInterface<mixed> $arguments |
121
|
|
|
* |
122
|
|
|
* @return StreamInterface<mixed> |
123
|
|
|
*/ |
124
|
11 |
|
public function extract( |
125
|
|
|
Name $name, |
126
|
|
|
StreamInterface $arguments, |
127
|
|
|
Argument $argument |
128
|
|
|
): StreamInterface { |
129
|
11 |
|
if (!$this->has($name)) { |
130
|
5 |
|
throw new ReferenceNotFound((string) $name); |
131
|
|
|
} |
132
|
|
|
|
133
|
6 |
|
$service = $this->services->get($name); |
134
|
|
|
|
135
|
|
|
if ( |
136
|
6 |
|
!$service->decorates() || |
137
|
6 |
|
!$service->arguments()->contains($argument) |
138
|
|
|
) { |
139
|
2 |
|
throw new ArgumentNotExtractable; |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
return $argument->resolve( |
143
|
4 |
|
$arguments, |
144
|
4 |
|
$this->services |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* The list of exposed services name with their constructor |
150
|
|
|
* |
151
|
|
|
* @return MapInterface<Name, Constructor> |
152
|
|
|
*/ |
153
|
3 |
|
public function exposed(): MapInterface |
154
|
|
|
{ |
155
|
3 |
|
return $this->services->exposed(); |
156
|
|
|
} |
157
|
|
|
|
158
|
8 |
|
public function compile(): CompiledDependency |
159
|
|
|
{ |
160
|
8 |
|
return new CompiledDependency( |
161
|
8 |
|
$this->name, |
162
|
8 |
|
$this->services, |
163
|
8 |
|
...$this->parameters |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|