|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Di; |
|
6
|
|
|
|
|
7
|
|
|
use Ray\Di\MultiBinding\LazyInstance; |
|
8
|
|
|
use Ray\Di\MultiBinding\LazyInteterface; |
|
9
|
|
|
use Ray\Di\MultiBinding\LazyProvider; |
|
10
|
|
|
use Ray\Di\MultiBinding\LazyTo; |
|
11
|
|
|
use Ray\Di\MultiBinding\MultiBindings; |
|
12
|
|
|
|
|
13
|
|
|
final class MultiBinder |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Container */ |
|
16
|
|
|
private $container; |
|
17
|
|
|
|
|
18
|
|
|
/** @var MultiBindings */ |
|
19
|
|
|
private $multiBindings; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $interface; |
|
23
|
|
|
|
|
24
|
|
|
/** @var ?string */ |
|
25
|
|
|
private $key; |
|
26
|
|
|
|
|
27
|
|
|
private function __construct(AbstractModule $module, string $interface) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->container = $module->getContainer(); |
|
30
|
|
|
$this->multiBindings = $this->container->multiBindings; |
|
31
|
|
|
$this->interface = $interface; |
|
32
|
|
|
$this->container->add( |
|
33
|
|
|
(new Bind($this->container, MultiBindings::class))->toInstance($this->multiBindings) |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function newInstance(AbstractModule $module, string $interface): self |
|
38
|
|
|
{ |
|
39
|
|
|
return new self($module, $interface); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function addBinding(?string $key = null): self |
|
43
|
|
|
{ |
|
44
|
|
|
$this->key = $key; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function setBinding(?string $key = null): self |
|
50
|
|
|
{ |
|
51
|
|
|
$this->container->multiBindings->exchangeArray([]); |
|
52
|
|
|
$this->key = $key; |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param class-string $class |
|
|
|
|
|
|
59
|
|
|
*/ |
|
60
|
|
|
public function to(string $class): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->bind(new LazyTo($class), $this->key); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param class-string<ProviderInterface> $provider |
|
|
|
|
|
|
67
|
|
|
*/ |
|
68
|
|
|
public function toProvider(string $provider): void |
|
69
|
|
|
{ |
|
70
|
|
|
$this->bind(new LazyProvider($provider), $this->key); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param mixed $instance |
|
75
|
|
|
*/ |
|
76
|
|
|
public function toInstance($instance): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->bind(new LazyInstance($instance), $this->key); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function bind(LazyInteterface $lazy, ?string $key): void |
|
82
|
|
|
{ |
|
83
|
|
|
$bindings = []; |
|
84
|
|
|
if ($this->multiBindings->offsetExists($this->interface)) { |
|
85
|
|
|
$bindings = $this->multiBindings->offsetGet($this->interface); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($key === null) { |
|
89
|
|
|
$bindings[] = $lazy; |
|
90
|
|
|
$this->multiBindings->offsetSet($this->interface, $bindings); // @phpstan-ignore-line |
|
91
|
|
|
|
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$bindings[$key] = $lazy; |
|
96
|
|
|
$this->multiBindings->offsetSet($this->interface, $bindings); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|