|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace evelikto\di; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Implements a dynamic Dependency Injection container with autowiring support. |
|
7
|
|
|
* |
|
8
|
|
|
* Evelikto-DI is configurable via an arbitrary class instance in which every method is |
|
9
|
|
|
* considered to be a factory method and each constant is considered to be a config value. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AppContextBase |
|
12
|
|
|
{ |
|
13
|
|
|
const DI_CONTAINER_KEY = 'appContext'; |
|
14
|
|
|
const DI_SESSION_STORAGE_KEY = 'diSessionStorage'; |
|
15
|
|
|
const DI_GLOBAL_STORAGE_KEY = 'diGlobalStorage'; |
|
16
|
|
|
|
|
17
|
|
|
/** @var object config Application config, which provides constants and factory methods */ |
|
18
|
|
|
protected $config; |
|
19
|
|
|
|
|
20
|
|
|
/** @var \ReflectionClass Cache for efficient retrieval of factory methods */ |
|
21
|
|
|
protected $configReflClass; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string configCName Cache for efficient retrieval of constant values */ |
|
24
|
|
|
protected $configClassName; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Creates new application context and registers itself. |
|
28
|
|
|
* |
|
29
|
|
|
* @param object $config Configuration class instance. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($config) { |
|
32
|
|
|
$this->config = $config; |
|
33
|
|
|
$this->configReflClass = new \ReflectionClass($config); |
|
34
|
|
|
$this->configClassName = get_class($config); |
|
35
|
|
|
|
|
36
|
|
|
$this->initSession(); |
|
37
|
|
|
$this->initGlobal(); |
|
38
|
|
|
|
|
39
|
|
|
$this->set(static::DI_CONTAINER_KEY, $this); |
|
40
|
|
|
$this->set(get_class($this), $this); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Creates a new or fetches a previously resolved dependency. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $name Name of the dependency to be resolved. |
|
47
|
|
|
* @return mixed The resolved dependency. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function get(string $name) { |
|
50
|
|
|
$value = $this->read($name); |
|
|
|
|
|
|
51
|
|
|
if ($value !== null) |
|
|
|
|
|
|
52
|
|
|
return $value; |
|
53
|
|
|
|
|
54
|
|
|
$bean = $this->create($name); |
|
55
|
|
|
if ($bean === null) |
|
56
|
|
|
throw new UnresolvableDependency($name); |
|
57
|
|
|
|
|
58
|
|
|
return $this->store($bean); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Stores manually created object. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $name Name of the dependency. |
|
65
|
|
|
* @param mixed $name Dependency to be stored. |
|
66
|
|
|
* @param string $name Scope name. Defaults to the singleton (local) scope. |
|
67
|
|
|
* @return mixed Returns the stored object back. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function set(string $name, $value, string $scope = null) { |
|
70
|
|
|
return $this->store(new Bean($name, $value, $scope)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Resolves arguments for a factory method or a controller action. |
|
75
|
|
|
* |
|
76
|
|
|
* @param \ReflectionParameter $param Parameter which has to be resolved. |
|
77
|
|
|
* @return mixed Resolved dependency. |
|
78
|
|
|
* @throws UnresolvableParameter If the parameter cannot be resolved. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function resolve(\ReflectionParameter $param) { |
|
81
|
|
|
$value = $this->resolveStoredByName($param); |
|
|
|
|
|
|
82
|
|
|
if ($value !== null) |
|
|
|
|
|
|
83
|
|
|
return $value; |
|
84
|
|
|
|
|
85
|
|
|
$bean = $this->resolveMethodByName($param) ?? $this->resolveByType($param); |
|
|
|
|
|
|
86
|
|
|
if ($bean !== null) |
|
|
|
|
|
|
87
|
|
|
return $this->store($bean); |
|
88
|
|
|
|
|
89
|
|
|
$value = $this->resolveByDefault($param); |
|
|
|
|
|
|
90
|
|
|
if ($value !== null) |
|
|
|
|
|
|
91
|
|
|
return $value; |
|
92
|
|
|
|
|
93
|
|
|
throw new UnresolvableParameter($param->getName()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Reads previously saved dependency or just a constant from config class. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $name Dependency which has to be read. |
|
100
|
|
|
* @return mixed Read dependency, constant or null. |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function read(string $name) { |
|
103
|
|
|
return $this->fromLocal($name) |
|
|
|
|
|
|
104
|
|
|
?? $this->fromConst($name) |
|
|
|
|
|
|
105
|
|
|
?? $this->fromSession($name) |
|
|
|
|
|
|
106
|
|
|
?? $this->fromGlobal($name) |
|
|
|
|
|
|
107
|
|
|
?? null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Creates a new dependency in a number of ways. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $name Name of the dependency to be created. Can be class or interface name. |
|
114
|
|
|
* @return Bean Resolved dependency wrapped as a Bean class instance or null. |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function create(string $name) { |
|
117
|
|
|
return $this->fromMethod($name) |
|
|
|
|
|
|
118
|
|
|
?? $this->fromClass($name) |
|
|
|
|
|
|
119
|
|
|
?? $this->fromInterfaceMethod($name) |
|
|
|
|
|
|
120
|
|
|
?? $this->fromInterfaceAlias($name) |
|
|
|
|
|
|
121
|
|
|
?? null; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Saves a dependency, returns unwrapped value. |
|
126
|
|
|
* |
|
127
|
|
|
* @param Bean $bean Wrapped dependency to be saved. |
|
128
|
|
|
* @return mixed Unwrapped dependency value. |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function store(Bean $bean) { |
|
131
|
|
|
return $this->toLocal($bean) |
|
|
|
|
|
|
132
|
|
|
?? $this->toSession($bean) |
|
|
|
|
|
|
133
|
|
|
?? $this->toGlobal($bean) |
|
|
|
|
|
|
134
|
|
|
?? $bean->value; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Recursively resolves all method parameters |
|
139
|
|
|
* |
|
140
|
|
|
* @param \ReflectionMethod $method Method to be autowired. |
|
141
|
|
|
* @returns Array of resolved parameters. |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function autowire(\ReflectionMethod $method) { |
|
144
|
|
|
return array_map([$this, 'resolve'], $method->getParameters()); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// Placeholders to disable functions not redefined by traits |
|
148
|
|
|
|
|
149
|
|
|
// ClassCreator |
|
150
|
|
|
protected function fromClass(string $name) { |
|
|
|
|
|
|
151
|
|
|
return null; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// InterfaceAliasCreator |
|
155
|
|
|
protected function fromInterfaceAlias(string $name) { |
|
|
|
|
|
|
156
|
|
|
return null; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// InterfaceMethodCreator |
|
160
|
|
|
protected function fromInterfaceMethod(string $name) { |
|
|
|
|
|
|
161
|
|
|
return null; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// MethodCreator |
|
165
|
|
|
protected function fromMethod(string $name) { |
|
|
|
|
|
|
166
|
|
|
return null; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// ConstReader |
|
170
|
|
|
protected function fromConst(string $name) { |
|
|
|
|
|
|
171
|
|
|
return null; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
// DefaultResolver |
|
175
|
|
|
protected function resolveByDefault(\ReflectionParameter $param) { |
|
|
|
|
|
|
176
|
|
|
return null; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
// NameResolver |
|
180
|
|
|
protected function resolveStoredByName(\ReflectionParameter $param) { |
|
|
|
|
|
|
181
|
|
|
return null; |
|
182
|
|
|
} |
|
183
|
|
|
protected function resolveMethodByName(\ReflectionParameter $param) { |
|
|
|
|
|
|
184
|
|
|
return null; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
// TypeResolver |
|
188
|
|
|
protected function resolveByType(\ReflectionParameter $param) { |
|
|
|
|
|
|
189
|
|
|
return null; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// LocalStorage |
|
193
|
|
|
protected function fromLocal(string $name) { |
|
|
|
|
|
|
194
|
|
|
return null; |
|
195
|
|
|
} |
|
196
|
|
|
protected function toLocal(Bean $bean) { |
|
|
|
|
|
|
197
|
|
|
return null; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
// SessionStorage |
|
201
|
|
|
protected function initSession() { |
|
202
|
|
|
} |
|
203
|
|
|
protected function fromSession(string $name) { |
|
|
|
|
|
|
204
|
|
|
return null; |
|
205
|
|
|
} |
|
206
|
|
|
protected function toSession(Bean $bean) { |
|
|
|
|
|
|
207
|
|
|
return null; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// GlobalStorage |
|
211
|
|
|
protected function initGlobal() { |
|
212
|
|
|
} |
|
213
|
|
|
protected function fromGlobal(string $name) { |
|
|
|
|
|
|
214
|
|
|
return null; |
|
215
|
|
|
} |
|
216
|
|
|
protected function toGlobal(Bean $bean) { |
|
|
|
|
|
|
217
|
|
|
return null; |
|
218
|
|
|
} |
|
219
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.