-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolver.php
More file actions
153 lines (126 loc) · 3.56 KB
/
Copy pathResolver.php
File metadata and controls
153 lines (126 loc) · 3.56 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
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
147
148
149
150
151
152
153
<?php
namespace Hackphp\Container;
use Closure;
use ReflectionClass;
use ReflectionException;
use ReflectionNamedType;
use ReflectionParameter;
use Hackphp\Container\Exceptions\BindingException;
trait Resolver
{
/**
* Build stack to detect cirular dependency.
*
* @var array
*/
private array $buildStack = [];
/**
* Resolve the entry.
*
* @param string $name
* @return mixed
*/
protected function resolve(string $name)
{
if (isset($this->instances[$name])) {
return $this->instances[$name];
}
$resolvedEntry = $this->getResolvedEntry($name);
if ($this->isShared($name)) {
$this->instances[$name] = $resolvedEntry;
}
return $resolvedEntry;
}
/**
* Get the resolved entry.
*
* @param string $name
* @return mixed
*/
protected function getResolvedEntry(string $name)
{
$entry = $this->bindings[$name]["entry"] ?? $name;
if ($entry instanceof Closure || $entry === $name) {
$resolved = $this->build($entry);
} else {
$resolved = $this->resolve($entry);
}
return $resolved;
}
/**
* Build the entry.
*
* @param Closure|string $entry
* @return mixed
*/
protected function build($entry)
{
if ($entry instanceof Closure) {
return $entry();
}
return $this->autowire($entry);
}
/**
* Resolve the entry dependencies.
*
* @param string $entry
* @return object
*/
protected function autowire(string $entry)
{
try {
$reflector = new ReflectionClass($entry);
} catch (ReflectionException $e) {
throw new BindingException("Target class [$entry] does not exists.", 0, $e);
}
if (!$reflector->isInstantiable()) {
throw new BindingException("Target class [$entry] is not instantiable.", 0, $e);
}
$constructor = $reflector->getConstructor();
if (is_null($constructor)) {
return new $entry;
}
// To Detect the circular dependency
if (in_array($entry, $this->buildStack)) {
throw new BindingException("Circular dependency.");
}
$this->buildStack[] = $entry;
$instances = $this->resolveDependencies(
$constructor->getParameters()
);
array_pop($this->buildStack);
return $reflector->newInstanceArgs($instances);
}
/**
* Resolve the class constructor dependencies.
*
* @param array $dependencies
* @return array
*/
protected function resolveDependencies(array $dependencies)
{
$results = [];
/** @var ReflectionParameter $dependency */
foreach ($dependencies as $dependency) {
$type = $dependency->getType();
$declaringClass = $dependency->getDeclaringClass()->getName();
if ($type->isBuiltin() || !$type instanceof ReflectionNamedType) {
throw new BindingException(
"Unresolvable dependency: resolving [$dependency] in class {$declaringClass}"
);
}
$results[] = $this->resolve($type->getName());
}
return $results;
}
/**
* Check if the entry is shared or not.
*
* @param string $name
* @return bool
*/
protected function isShared(string $name): bool
{
return isset($this->bindings[$name]) && $this->bindings[$name]["shared"];
}
}