ray-di /
Ray.Di
| 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 | private readonly Container $container; |
||
| 20 | |||
| 21 | /** @var MultiBindings */ |
||
| 22 | private $multiBindings; |
||
| 23 | private ?string $key = null; |
||
| 24 | |||
| 25 | private function __construct(AbstractModule $module, private readonly string $interface) |
||
| 26 | { |
||
| 27 | $this->container = $module->getContainer(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 28 | $this->multiBindings = $this->container->multiBindings; |
||
| 29 | $this->container->add( |
||
| 30 | (new Bind($this->container, MultiBindings::class))->toInstance($this->multiBindings) |
||
| 31 | ); |
||
| 32 | } |
||
| 33 | |||
| 34 | public static function newInstance(AbstractModule $module, string $interface): self |
||
| 35 | { |
||
| 36 | return new self($module, $interface); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function addBinding(?string $key = null): self |
||
| 40 | { |
||
| 41 | $this->key = $key; |
||
| 42 | |||
| 43 | return $this; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function setBinding(?string $key = null): self |
||
| 47 | { |
||
| 48 | $this->container->multiBindings->exchangeArray([]); |
||
| 49 | $this->key = $key; |
||
| 50 | |||
| 51 | return $this; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param class-string $class |
||
|
0 ignored issues
–
show
|
|||
| 56 | */ |
||
| 57 | public function to(string $class): void |
||
| 58 | { |
||
| 59 | $this->bind(new LazyTo($class), $this->key); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param class-string<ProviderInterface<T>> $provider |
||
|
0 ignored issues
–
show
|
|||
| 64 | * |
||
| 65 | * @template T of mixed |
||
| 66 | */ |
||
| 67 | public function toProvider(string $provider): void |
||
| 68 | { |
||
| 69 | $this->bind(new LazyProvider($provider), $this->key); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param mixed $instance |
||
| 74 | */ |
||
| 75 | public function toInstance($instance): void |
||
| 76 | { |
||
| 77 | $this->bind(new LazyInstance($instance), $this->key); |
||
| 78 | } |
||
| 79 | |||
| 80 | private function bind(LazyInterface $lazy, ?string $key): void |
||
| 81 | { |
||
| 82 | $bindings = []; |
||
| 83 | if ($this->multiBindings->offsetExists($this->interface)) { |
||
| 84 | $bindings = $this->multiBindings->offsetGet($this->interface); |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($key === null) { |
||
| 88 | $bindings[] = $lazy; |
||
| 89 | $this->multiBindings->offsetSet($this->interface, $bindings); |
||
| 90 | |||
| 91 | return; |
||
| 92 | } |
||
| 93 | |||
| 94 | $bindings[$key] = $lazy; |
||
| 95 | $this->multiBindings->offsetSet($this->interface, $bindings); |
||
| 96 | } |
||
| 97 | } |
||
| 98 |