-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathObjectContainer.php
More file actions
114 lines (95 loc) · 3.6 KB
/
ObjectContainer.php
File metadata and controls
114 lines (95 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace Internal\DLoad\Module\Common\Internal;
use Internal\DLoad\Module\Common\Internal\Injection\ConfigLoader;
use Internal\DLoad\Service\Container;
use Internal\DLoad\Service\Factoriable;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Yiisoft\Injector\Injector;
/**
* Simple dependency injection container.
*
* Provides service creation and caching with autowiring capabilities.
* Automatically loads configuration for config classes.
*
* @internal
*/
final class ObjectContainer implements Container, ContainerInterface
{
/** @var array<class-string, object> */
private array $cache = [];
/** @var array<class-string, array|\Closure(Container): object> */
private array $factory = [];
private readonly Injector $injector;
/**
* @psalm-suppress PropertyTypeCoercion
*/
public function __construct()
{
$this->injector = (new Injector($this))->withCacheReflections(false);
$this->cache[Injector::class] = $this->injector;
$this->cache[self::class] = $this;
$this->cache[Container::class] = $this;
$this->cache[ContainerInterface::class] = $this;
}
public function get(string $id, array $arguments = []): object
{
/** @psalm-suppress InvalidReturnStatement */
return $this->cache[$id] ??= $this->make($id, $arguments);
}
public function has(string $id): bool
{
return \array_key_exists($id, $this->cache) || \array_key_exists($id, $this->factory);
}
public function set(object $service, ?string $id = null): void
{
\assert($id === null || $service instanceof $id, "Service must be instance of {$id}.");
$this->cache[$id ?? \get_class($service)] = $service;
}
public function make(string $class, array $arguments = []): object
{
$binding = $this->factory[$class] ?? null;
if ($binding instanceof \Closure) {
$result = $this->injector->invoke($binding);
} else {
try {
$result = $this->injector->make($class, \array_merge((array) $binding, $arguments));
} catch (\Throwable $e) {
throw new class("Unable to create object of class $class.", previous: $e) extends \RuntimeException implements NotFoundExceptionInterface {};
}
}
\assert($result instanceof $class, "Created object must be instance of {$class}.");
// Detect related types
// Configs
if (\str_starts_with($class, 'Internal\\DLoad\\Module\\Config\\Schema\\')) {
// Hydrate config
/** @var ConfigLoader $configLoader */
$configLoader = $this->get(ConfigLoader::class);
$configLoader->hydrate($result);
}
return $result;
}
/**
* @template T
* @param class-string<T> $id Service identifier
* @param null|array|\Closure(Container): T $binding Factory function or constructor arguments
*/
public function bind(string $id, \Closure|array|null $binding = null): void
{
if ($binding !== null) {
$this->factory[$id] = $binding;
return;
}
(\class_exists($id) && \is_a($id, Factoriable::class, true)) or throw new \InvalidArgumentException(
"Class `$id` must have a factory or be a factory itself and implement `Factoriable`.",
);
/** @var T $object */
$object = $id::create(...);
$this->factory[$id] = $object;
}
public function destroy(): void
{
unset($this->cache, $this->factory, $this->injector);
}
}