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
|
|
|
}; |
14
|
|
|
use Innmind\Immutable\{ |
15
|
|
|
Set, |
16
|
|
|
Map, |
17
|
|
|
StreamInterface |
18
|
|
|
}; |
19
|
|
|
|
20
|
|
|
final class Dependency |
21
|
|
|
{ |
22
|
|
|
private $name; |
23
|
|
|
private $services; |
24
|
|
|
private $parameters; |
25
|
|
|
|
26
|
53 |
|
public function __construct( |
27
|
|
|
Name $name, |
28
|
|
|
Services $services, |
29
|
|
|
Parameter ...$parameters |
30
|
|
|
) { |
31
|
53 |
|
$this->name = $name; |
32
|
53 |
|
$this->services = $services; |
33
|
53 |
|
$this->parameters = Set::of(Parameter::class, ...$parameters); |
34
|
53 |
|
} |
35
|
|
|
|
36
|
33 |
|
public function name(): Name |
37
|
|
|
{ |
38
|
33 |
|
return $this->name; |
39
|
|
|
} |
40
|
|
|
|
41
|
8 |
|
public function bind(Services $services): self |
42
|
|
|
{ |
43
|
|
|
$parameters = $this |
44
|
8 |
|
->parameters |
45
|
8 |
|
->reduce( |
46
|
8 |
|
new Map('string', 'mixed'), |
47
|
8 |
|
static function(Map $parameters, Parameter $parameter) use ($services): Map { |
48
|
8 |
|
return $parameters->put( |
49
|
8 |
|
(string) $parameter->name(), |
50
|
8 |
|
$parameter->resolve($services) |
51
|
|
|
); |
52
|
8 |
|
} |
53
|
|
|
); |
54
|
|
|
|
55
|
8 |
|
$self = clone $this; |
56
|
8 |
|
$self->services = $self->services->inject($parameters); |
57
|
|
|
|
58
|
8 |
|
return $self; |
59
|
|
|
} |
60
|
|
|
|
61
|
21 |
|
public function lazy(Name $name): Lazy |
62
|
|
|
{ |
63
|
21 |
|
if (!$this->has($name)) { |
64
|
7 |
|
throw new ReferenceNotFound((string) $name); |
65
|
|
|
} |
66
|
|
|
|
67
|
14 |
|
return new Lazy( |
68
|
14 |
|
$name, |
69
|
14 |
|
$this->services |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
public function build(Name $name): object |
74
|
|
|
{ |
75
|
5 |
|
return $this->lazy($name)->load(); |
76
|
|
|
} |
77
|
|
|
|
78
|
44 |
|
public function has(Name $name): bool |
79
|
|
|
{ |
80
|
44 |
|
if (!$this->services->has($name)) { |
81
|
6 |
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
39 |
|
$service = $this->services->get($name); |
85
|
|
|
|
86
|
39 |
|
if (!$service->exposed() || !$service->isExposedAs($name)) { |
87
|
12 |
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
28 |
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
5 |
|
public function dependsOn(self $other): bool |
94
|
|
|
{ |
95
|
5 |
|
return $this->parameters->reduce( |
96
|
5 |
|
false, |
97
|
5 |
|
function(bool $dependsOn, Parameter $parameter) use ($other): bool { |
98
|
3 |
|
return $dependsOn || $parameter->refersTo($other); |
99
|
5 |
|
} |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
public function decorate( |
104
|
|
|
Name $decorator, |
105
|
|
|
Name $decorated, |
106
|
|
|
Name $newName = null |
107
|
|
|
): Service { |
108
|
10 |
|
if (!$this->has($decorator)) { |
109
|
4 |
|
throw new ReferenceNotFound((string) $decorator); |
110
|
|
|
} |
111
|
|
|
|
112
|
6 |
|
$service = $this->services->get($decorator); |
113
|
|
|
|
114
|
6 |
|
return $service->tunnel($this->name, $decorated, $newName); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param StreamInterface<mixed> $arguments |
119
|
|
|
* |
120
|
|
|
* @return StreamInterface<mixed> |
121
|
|
|
*/ |
122
|
11 |
|
public function extract( |
123
|
|
|
Name $name, |
124
|
|
|
StreamInterface $arguments, |
125
|
|
|
Argument $argument |
126
|
|
|
): StreamInterface { |
127
|
11 |
|
if (!$this->has($name)) { |
128
|
5 |
|
throw new ReferenceNotFound((string) $name); |
129
|
|
|
} |
130
|
|
|
|
131
|
6 |
|
$service = $this->services->get($name); |
132
|
|
|
|
133
|
|
|
if ( |
134
|
6 |
|
!$service->decorates() || |
135
|
6 |
|
!$service->arguments()->contains($argument) |
136
|
|
|
) { |
137
|
2 |
|
throw new ArgumentNotExtractable; |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
return $argument->resolve( |
141
|
4 |
|
$arguments, |
142
|
4 |
|
$this->services |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: