|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the aether/aether. |
| 5 | + * |
| 6 | + * Copyright (C) 2024 Dominik Szamburski |
| 7 | + * |
| 8 | + * This software may be modified and distributed under the terms |
| 9 | + * of the MIT license. See the LICENSE file for details. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Aether\DependencyInjection; |
| 13 | + |
| 14 | +use function array_key_exists; |
| 15 | + |
| 16 | +use Closure; |
| 17 | +use Exception; |
| 18 | +use InvalidArgumentException; |
| 19 | + |
| 20 | +use function is_object; |
| 21 | +use function is_scalar; |
| 22 | +use function is_string; |
| 23 | +use function property_exists; |
| 24 | + |
| 25 | +use Aether\DependencyInjection\Definition\Binding\Alias; |
| 26 | +use Aether\DependencyInjection\Definition\Binding\Factory; |
| 27 | +use Aether\DependencyInjection\Definition\Binding\Scalar; |
| 28 | +use Aether\DependencyInjection\Definition\Binding\Shared; |
| 29 | +use Aether\DependencyInjection\Definition\Binding\WeakReference; |
| 30 | + |
| 31 | +use Aether\DependencyInjection\Definition\Exception\CircularDependencyException; |
| 32 | +use Aether\DependencyInjection\Definition\Resolver\DefinitionResolver; |
| 33 | +use Aether\DependencyInjection\Definition\State; |
| 34 | +use Aether\DependencyInjection\Exception\EntryNotFoundException; |
| 35 | +use Aether\DependencyInjection\Invoker\Invoker; |
| 36 | + |
| 37 | +class Container implements ContainerInterface |
| 38 | +{ |
| 39 | + private State $state; |
| 40 | + private FactoryInterface $factory; |
| 41 | + |
| 42 | + public function __construct() |
| 43 | + { |
| 44 | + |
| 45 | + $this->state = new State(); |
| 46 | + $this->factory = new DefinitionResolver($this->state, $this); |
| 47 | + |
| 48 | + $shared = new Alias(self::class); |
| 49 | + |
| 50 | + $this->state->bindings = [ |
| 51 | + self::class => new WeakReference(\WeakReference::create($this)), |
| 52 | + ContainerInterface::class => $shared, |
| 53 | + FactoryInterface::class => $shared, |
| 54 | + InvokerInterface::class => $shared |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @inheritDoc |
| 60 | + */ |
| 61 | + public function has(string $id): bool |
| 62 | + { |
| 63 | + return array_key_exists($id, $this->state->instances) || array_key_exists($id, $this->state->bindings); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @inheritDoc |
| 68 | + */ |
| 69 | + public function get(string $id): int|float|string|callable|object |
| 70 | + { |
| 71 | + try { |
| 72 | + return $this->make($id); |
| 73 | + } catch (Exception $e) { |
| 74 | + if ($this->has($id) || $e instanceof CircularDependencyException) { |
| 75 | + throw $e; |
| 76 | + } |
| 77 | + |
| 78 | + throw new EntryNotFoundException($id); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @inheritDoc |
| 84 | + */ |
| 85 | + public function instance(string $id, int|float|string|callable|object $instance): int|float|string|callable|object |
| 86 | + { |
| 87 | + $this->state->instances[$id] = $instance; |
| 88 | + return $instance; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @inheritDoc |
| 93 | + */ |
| 94 | + public function bind(string $abstract, int|float|string|callable|object $concrete, bool $shared = false): void |
| 95 | + { |
| 96 | + $concrete = match (true) { |
| 97 | + $concrete instanceof Closure => new Factory($concrete, $shared), |
| 98 | + $concrete instanceof \WeakReference => new WeakReference($concrete), |
| 99 | + is_string($concrete) => new Alias($concrete, $shared), |
| 100 | + is_scalar($concrete) => new Scalar($concrete), |
| 101 | + is_object($concrete) => new Shared($concrete), |
| 102 | + default => throw new InvalidArgumentException(self::class . '::bind() unknown binding type.') |
| 103 | + }; |
| 104 | + |
| 105 | + $this->state->bindings[$abstract] = $concrete; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @inheritDoc |
| 110 | + */ |
| 111 | + public function shared(string $abstract, int|float|string|callable|object $concrete): void |
| 112 | + { |
| 113 | + $this->bind($abstract, $concrete, true); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @inheritDoc |
| 118 | + */ |
| 119 | + public function isShared(string $abstract): bool |
| 120 | + { |
| 121 | + if (array_key_exists($abstract, $this->state->instances)) { |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + if (array_key_exists($abstract, $this->state->bindings)) { |
| 126 | + if (property_exists($this->state->bindings[$abstract], 'shared')) { |
| 127 | + return (bool) $this->state->bindings[$abstract]->shared; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @inheritDoc |
| 136 | + */ |
| 137 | + public function make(string $abstract, array $parameters = []): int|float|string|callable|object |
| 138 | + { |
| 139 | + return $this->factory->make($abstract, $parameters); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @inheritDoc |
| 144 | + */ |
| 145 | + public function call(callable|array|string $callable, array $parameters = []): mixed |
| 146 | + { |
| 147 | + return $this->getInvoker()->call($callable, $parameters); |
| 148 | + } |
| 149 | + |
| 150 | + private function getInvoker(): InvokerInterface |
| 151 | + { |
| 152 | + return new Invoker($this); |
| 153 | + } |
| 154 | +} |
0 commit comments