|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Compose\Definition; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Compose\{ |
|
7
|
|
|
Definition\Service\Constructor, |
|
8
|
|
|
Definition\Service\Argument, |
|
|
|
|
|
|
9
|
|
|
Definitions, |
|
10
|
|
|
Exception\ServiceCannotDecorateMultipleServices |
|
11
|
|
|
}; |
|
12
|
|
|
use Innmind\Immutable\{ |
|
13
|
|
|
StreamInterface, |
|
14
|
|
|
Stream |
|
15
|
|
|
}; |
|
16
|
|
|
|
|
17
|
|
|
final class Service |
|
18
|
|
|
{ |
|
19
|
|
|
private $name; |
|
20
|
|
|
private $construct; |
|
21
|
|
|
private $arguments; |
|
22
|
|
|
private $exposeName; |
|
23
|
|
|
|
|
24
|
25 |
|
public function __construct( |
|
25
|
|
|
Name $name, |
|
26
|
|
|
Constructor $constructor, |
|
27
|
|
|
Argument ...$arguments |
|
28
|
|
|
) { |
|
29
|
25 |
|
$this->name = $name; |
|
30
|
25 |
|
$this->construct = $constructor; |
|
31
|
25 |
|
$this->arguments = Stream::of(Argument::class, ...$arguments); |
|
32
|
|
|
|
|
33
|
25 |
|
$decorates = $this->arguments->filter(static function(Argument $argument): bool { |
|
34
|
11 |
|
return $argument instanceof Argument\Decorate; |
|
35
|
25 |
|
}); |
|
36
|
|
|
|
|
37
|
25 |
|
if ($decorates->size() > 1) { |
|
38
|
1 |
|
throw new ServiceCannotDecorateMultipleServices((string) $name); |
|
39
|
|
|
} |
|
40
|
24 |
|
} |
|
41
|
|
|
|
|
42
|
23 |
|
public function exposeAs(Name $name): self |
|
43
|
|
|
{ |
|
44
|
23 |
|
$self = clone $this; |
|
45
|
23 |
|
$self->exposeName = $name; |
|
46
|
|
|
|
|
47
|
23 |
|
return $self; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
23 |
|
public function exposed(): bool |
|
51
|
|
|
{ |
|
52
|
23 |
|
return $this->exposeName instanceof Name; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
23 |
|
public function exposedAs(): Name |
|
56
|
|
|
{ |
|
57
|
23 |
|
return $this->exposeName; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
public function isExposedAs(Name $name): bool |
|
61
|
|
|
{ |
|
62
|
4 |
|
return (string) $this->exposeName === (string) $name; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
24 |
|
public function name(): Name |
|
66
|
|
|
{ |
|
67
|
24 |
|
return $this->name; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function constructor(): Constructor |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->construct; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return StreamInterface<Argument> |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function arguments(): StreamInterface |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->arguments; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
9 |
|
public function build(Definitions $definitions): object |
|
84
|
|
|
{ |
|
85
|
9 |
|
return ($this->construct)(...$this->arguments->reduce( |
|
86
|
9 |
|
Stream::of('mixed'), |
|
87
|
9 |
|
static function(Stream $arguments, Argument $argument) use ($definitions): Stream { |
|
88
|
4 |
|
return $argument->resolve($arguments, $definitions); |
|
|
|
|
|
|
89
|
9 |
|
} |
|
90
|
|
|
)); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: