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