1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Compose\Definition; |
5
|
|
|
|
6
|
|
|
use Innmind\Compose\{ |
7
|
|
|
Definition\Dependency\Argument, |
8
|
|
|
Services, |
9
|
|
|
Lazy, |
10
|
|
|
Exception\ReferenceNotFound |
11
|
|
|
}; |
12
|
|
|
use Innmind\Immutable\{ |
13
|
|
|
Set, |
14
|
|
|
Map |
15
|
|
|
}; |
16
|
|
|
|
17
|
|
|
final class Dependency |
18
|
|
|
{ |
19
|
|
|
private $name; |
20
|
|
|
private $services; |
21
|
|
|
private $arguments; |
22
|
|
|
|
23
|
28 |
|
public function __construct( |
24
|
|
|
Name $name, |
25
|
|
|
Services $services, |
26
|
|
|
Argument ...$arguments |
27
|
|
|
) { |
28
|
28 |
|
$this->name = $name; |
29
|
28 |
|
$this->services = $services; |
30
|
28 |
|
$this->arguments = Set::of(Argument::class, ...$arguments); |
31
|
28 |
|
} |
32
|
|
|
|
33
|
18 |
|
public function name(): Name |
34
|
|
|
{ |
35
|
18 |
|
return $this->name; |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
public function bind(Services $services): self |
39
|
|
|
{ |
40
|
|
|
$arguments = $this |
41
|
3 |
|
->arguments |
42
|
3 |
|
->reduce( |
43
|
3 |
|
new Map('string', 'mixed'), |
44
|
3 |
|
static function(Map $arguments, Argument $argument) use ($services): Map { |
45
|
3 |
|
return $arguments->put( |
46
|
3 |
|
(string) $argument->name(), |
47
|
3 |
|
$argument->resolve($services) |
48
|
|
|
); |
49
|
3 |
|
} |
50
|
|
|
); |
51
|
|
|
|
52
|
3 |
|
$self = clone $this; |
53
|
3 |
|
$self->services = $self->services->inject($arguments); |
54
|
|
|
|
55
|
3 |
|
return $self; |
56
|
|
|
} |
57
|
|
|
|
58
|
16 |
|
public function lazy(Name $name): Lazy |
59
|
|
|
{ |
60
|
16 |
|
if (!$this->has($name)) { |
61
|
7 |
|
throw new ReferenceNotFound((string) $name); |
62
|
|
|
} |
63
|
|
|
|
64
|
9 |
|
return new Lazy( |
65
|
9 |
|
$name, |
66
|
9 |
|
$this->services |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
5 |
|
public function build(Name $name): object |
71
|
|
|
{ |
72
|
5 |
|
return $this->lazy($name)->load(); |
73
|
|
|
} |
74
|
|
|
|
75
|
20 |
|
public function has(Name $name): bool |
76
|
|
|
{ |
77
|
20 |
|
if (!$this->services->has($name)) { |
78
|
2 |
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
19 |
|
$service = $this->services->get($name); |
82
|
|
|
|
83
|
19 |
|
if (!$service->exposed() || !$service->isExposedAs($name)) { |
84
|
7 |
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
13 |
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
public function dependsOn(self $other): bool |
91
|
|
|
{ |
92
|
4 |
|
return $this->arguments->reduce( |
93
|
4 |
|
false, |
94
|
4 |
|
function(bool $dependsOn, Argument $argument) use ($other): bool { |
95
|
2 |
|
return $dependsOn || $argument->refersTo($other); |
96
|
4 |
|
} |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|