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 |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
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<T>> $provider |
||
0 ignored issues
–
show
|
|||
67 | * |
||
68 | * @template T of mixed |
||
69 | */ |
||
70 | public function toProvider(string $provider): void |
||
71 | { |
||
72 | $this->bind(new LazyProvider($provider), $this->key); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param mixed $instance |
||
77 | */ |
||
78 | public function toInstance($instance): void |
||
79 | { |
||
80 | $this->bind(new LazyInstance($instance), $this->key); |
||
81 | } |
||
82 | |||
83 | private function bind(LazyInteterface $lazy, ?string $key): void |
||
84 | { |
||
85 | $bindings = []; |
||
86 | if ($this->multiBindings->offsetExists($this->interface)) { |
||
87 | $bindings = $this->multiBindings->offsetGet($this->interface); |
||
88 | } |
||
89 | |||
90 | if ($key === null) { |
||
91 | $bindings[] = $lazy; |
||
92 | $this->multiBindings->offsetSet($this->interface, $bindings); |
||
93 | |||
94 | return; |
||
95 | } |
||
96 | |||
97 | $bindings[$key] = $lazy; |
||
98 | $this->multiBindings->offsetSet($this->interface, $bindings); |
||
99 | } |
||
100 | } |
||
101 |