|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Borobudur-DependencyInjection package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Borobudur\DependencyInjection; |
|
12
|
|
|
|
|
13
|
|
|
use Borobudur\DependencyInjection\Exception\InvalidArgumentException; |
|
14
|
|
|
use Borobudur\DependencyInjection\ParameterBag\Resolver; |
|
15
|
|
|
use ReflectionClass; |
|
16
|
|
|
use ReflectionMethod; |
|
17
|
|
|
use ReflectionParameter; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Iqbal Maulana <[email protected]> |
|
21
|
|
|
* @created 8/8/15 |
|
22
|
|
|
*/ |
|
23
|
|
|
class InstanceManager |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Di |
|
27
|
|
|
*/ |
|
28
|
|
|
private $di; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Resolver |
|
32
|
|
|
*/ |
|
33
|
|
|
private $resolver; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var object |
|
37
|
|
|
*/ |
|
38
|
|
|
private $service; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var ReflectionClass |
|
42
|
|
|
*/ |
|
43
|
|
|
private $reflection; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Di $di |
|
49
|
|
|
* @param mixed $service |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(Di $di, $service) |
|
52
|
|
|
{ |
|
53
|
|
|
if (is_callable($service) || !is_object($service)) { |
|
54
|
|
|
throw new InvalidArgumentException(sprintf( |
|
55
|
|
|
'Service should be object, "%s" given.', |
|
56
|
|
|
is_callable($service) ? 'callable' : gettype($service) |
|
57
|
|
|
)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->di = $di; |
|
61
|
|
|
$this->resolver = $di->getContainer()->getParameterBag()->getResolver(); |
|
62
|
|
|
$this->service = $service; |
|
63
|
|
|
$this->reflection = new ReflectionClass(get_class($service)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get original service. |
|
68
|
|
|
* |
|
69
|
|
|
* @return object |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getOriginalInstance() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->service; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get instance reflection. |
|
78
|
|
|
* |
|
79
|
|
|
* @return ReflectionClass |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getReflection() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->reflection; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get instance methods. |
|
88
|
|
|
* |
|
89
|
|
|
* @param int|null $filter |
|
90
|
|
|
* |
|
91
|
|
|
* @return ReflectionMethod[] |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getMethods($filter = null) |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->reflection->getMethods($filter); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get constructor arguments. |
|
100
|
|
|
* |
|
101
|
|
|
* @return ReflectionParameter[] |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getConstructorArguments() |
|
104
|
|
|
{ |
|
105
|
|
|
$constructor = $this->reflection->getConstructor(); |
|
106
|
|
|
|
|
107
|
|
|
return $constructor->getParameters(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param $method |
|
112
|
|
|
* |
|
113
|
|
|
* @return ReflectionParameter[] |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getMethodArguments($method) |
|
116
|
|
|
{ |
|
117
|
|
|
$method = $this->reflection->getMethod($method); |
|
118
|
|
|
|
|
119
|
|
|
return $method->getParameters(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Set sets of properties. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $properties |
|
126
|
|
|
* |
|
127
|
|
|
* @return static |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setProperties(array $properties) |
|
130
|
|
|
{ |
|
131
|
|
|
foreach ($properties as $property => $value) { |
|
132
|
|
|
$this->setProperty($property, $value); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Set property to service. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $property |
|
142
|
|
|
* @param mixed $value |
|
143
|
|
|
* |
|
144
|
|
|
* @return static |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setProperty($property, $value) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->service->{$property} = $this->resolver->resolveValue($value); |
|
149
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Call sets of method. |
|
155
|
|
|
* |
|
156
|
|
|
* @param array $methods |
|
157
|
|
|
* |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
public function callMethods(array $methods) |
|
161
|
|
|
{ |
|
162
|
|
|
$results = array(); |
|
163
|
|
|
foreach ($methods as $call) { |
|
164
|
|
|
if (2 === count($call)) { |
|
165
|
|
|
$results[] = $this->callMethod($call[0], (array) $call[1]); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $results; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Call method with auto resolution. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $method |
|
176
|
|
|
* @param array $arguments |
|
177
|
|
|
* |
|
178
|
|
|
* @return mixed |
|
179
|
|
|
*/ |
|
180
|
|
|
public function callMethod($method, array $arguments = array()) |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->di->call(array($this->service, $method), $arguments); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|