-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathContainer.php
146 lines (124 loc) · 3.81 KB
/
Container.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Qiq;
use Psr\Container\ContainerInterface;
use Qiq\Exception;
use ReflectionClass;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionType;
/**
* This is a low-powered container to handle only basic constructor DI.
* You can configure arguments by passing `$config['className']['parameterName']`.
*
* Parameter resolution:
*
* - First, use an argument from $config, if one is available.
* - Next, try to get an object of the parameter type from the the Container.
* - Last, use the default parameter value, if one is defined.
*
* If none of these work, you'll get a ParameterNotResolved exception.
*/
class Container implements ContainerInterface
{
/**
* @var object[]
*/
protected array $instances = [];
/**
* @param array<string, array<string, mixed>> $config
*/
public function __construct(protected array $config = [])
{
}
/**
* @template T of object
* @param class-string<T> $class
* @return T of object
*/
public function get(string $class) : mixed
{
if (! isset($this->instances[$class])) {
$this->instances[$class] = $this->new($class);
}
/** @var T of object */
return $this->instances[$class];
}
public function has(string $class) : bool
{
return class_exists($class);
}
/**
* @template T of object
* @param class-string<T> $class
* @return T of object
*/
protected function new(string $class) : object
{
if (! $this->has($class)) {
throw new Exception\ObjectNotFound(
"Object of class '{$class}' does not exist.",
);
}
$constructor = (new ReflectionClass($class))->getConstructor();
$arguments = $constructor ? $this->arguments($class, $constructor) : [];
/** @var T of object */
return new $class(...$arguments);
}
/**
* @return mixed[]
*/
protected function arguments(
string $declaringClass,
ReflectionMethod $constructor,
) : array
{
$arguments = [];
$parameters = $constructor->getParameters();
foreach ($parameters as $parameter) {
$arguments[] = $this->argument($declaringClass, $parameter);
}
return $arguments;
}
protected function argument(
string $declaringClass,
ReflectionParameter $parameter,
) : mixed
{
$name = $parameter->getName();
// is there a config element for this class and parameter?
if (isset($this->config[$declaringClass][$name])) {
return $this->config[$declaringClass][$name];
}
$type = $parameter->getType();
if (! $type instanceof ReflectionNamedType) {
// not a named type, try for the default value
return $this->default($declaringClass, $parameter, $name, $type);
}
/** @var class-string */
$parameterClass = $type->getName();
// is the parameter type an existing class?
if ($this->has($parameterClass)) {
// use an object of that type
return $this->get($parameterClass);
}
// no configured value, not an existing class;
// try for the default value
return $this->default($declaringClass, $parameter, $name, $type);
}
protected function default(
string $declaringClass,
ReflectionParameter $parameter,
string $name,
?ReflectionType $type,
) : mixed
{
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
throw new Exception\ParameterNotResolved(
"Cannot create argument for '{$declaringClass}::\${$name}' of type '{$type}'.",
);
}
}