|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Ray.Di package. |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Ray\Di; |
|
9
|
|
|
|
|
10
|
|
|
use Ray\Di\Exception\NotFound; |
|
11
|
|
|
|
|
12
|
|
|
final class Bind |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Container |
|
16
|
|
|
*/ |
|
17
|
|
|
private $container; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $interface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $name = Name::ANY; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var DependencyInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $bound; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var BindValidator |
|
36
|
|
|
*/ |
|
37
|
|
|
private $validate; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Untarget |
|
41
|
|
|
*/ |
|
42
|
|
|
private $untarget; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Container $container dependency container |
|
46
|
|
|
* @param string $interface interface or concrete class name |
|
47
|
|
|
*/ |
|
48
|
86 |
|
public function __construct(Container $container, $interface) |
|
49
|
|
|
{ |
|
50
|
86 |
|
$this->container = $container; |
|
51
|
86 |
|
$this->interface = $interface; |
|
52
|
86 |
|
$this->validate = new BindValidator; |
|
53
|
86 |
|
$bindUntarget = class_exists($interface) && ! (new \ReflectionClass($interface))->isAbstract() && $this->hasNotRegistered($interface); |
|
54
|
86 |
|
if ($bindUntarget) { |
|
55
|
46 |
|
$this->untarget = new Untarget($interface); |
|
56
|
|
|
|
|
57
|
46 |
|
return; |
|
58
|
1 |
|
} |
|
59
|
85 |
|
$this->validate->constructor($interface); |
|
60
|
84 |
|
} |
|
61
|
|
|
|
|
62
|
80 |
|
public function __destruct() |
|
63
|
|
|
{ |
|
64
|
80 |
|
if ($this->untarget) { |
|
65
|
17 |
|
$this->untarget->__invoke($this->container, $this); |
|
66
|
17 |
|
$this->untarget = null; |
|
67
|
17 |
|
} |
|
68
|
80 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $name |
|
72
|
|
|
* |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
27 |
|
public function annotatedWith($name) |
|
76
|
|
|
{ |
|
77
|
27 |
|
$this->name = $name; |
|
78
|
|
|
|
|
79
|
27 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $class |
|
84
|
|
|
* |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
56 |
|
public function to($class) |
|
88
|
|
|
{ |
|
89
|
56 |
|
$this->untarget = null; |
|
90
|
56 |
|
$this->validate->to($this->interface, $class); |
|
91
|
54 |
|
$this->bound = (new DependencyFactory)->newAnnotatedDependency(new \ReflectionClass($class)); |
|
92
|
54 |
|
$this->container->add($this); |
|
93
|
|
|
|
|
94
|
54 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $class class name |
|
99
|
|
|
* @param string $name varName=bindName,... |
|
100
|
|
|
* @param InjectionPoints $injectionPoints injection points |
|
101
|
|
|
* @param null $postConstruct method name of initialization after all dependencies are injected |
|
102
|
|
|
* |
|
103
|
|
|
* @return $this |
|
104
|
|
|
*/ |
|
105
|
3 |
|
public function toConstructor($class, $name, InjectionPoints $injectionPoints = null, $postConstruct = null) |
|
106
|
|
|
{ |
|
107
|
3 |
|
$this->untarget = null; |
|
108
|
3 |
|
$postConstruct = $postConstruct ? new \ReflectionMethod($class, $postConstruct) : null; |
|
109
|
3 |
|
$this->bound = (new DependencyFactory)->newToConstructor(new \ReflectionClass($class), $name, $injectionPoints, $postConstruct); |
|
110
|
3 |
|
$this->container->add($this); |
|
111
|
|
|
|
|
112
|
3 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $provider |
|
117
|
|
|
* |
|
118
|
|
|
* @return $this |
|
119
|
|
|
* |
|
120
|
|
|
* @throws NotFound |
|
121
|
|
|
*/ |
|
122
|
14 |
|
public function toProvider($provider) |
|
123
|
|
|
{ |
|
124
|
14 |
|
$this->untarget = null; |
|
125
|
14 |
|
$this->validate->toProvider($provider); |
|
126
|
12 |
|
$this->bound = (new DependencyFactory)->newProvider(new \ReflectionClass($provider)); |
|
127
|
12 |
|
$this->container->add($this); |
|
128
|
|
|
|
|
129
|
12 |
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param mixed $instance |
|
134
|
|
|
* |
|
135
|
|
|
* @return $this |
|
136
|
|
|
*/ |
|
137
|
74 |
|
public function toInstance($instance) |
|
138
|
|
|
{ |
|
139
|
74 |
|
$this->untarget = null; |
|
140
|
74 |
|
$this->bound = new Instance($instance); |
|
141
|
74 |
|
$this->container->add($this); |
|
142
|
|
|
|
|
143
|
74 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $scope |
|
148
|
|
|
* |
|
149
|
|
|
* @return $this |
|
150
|
|
|
*/ |
|
151
|
42 |
|
public function in($scope) |
|
152
|
|
|
{ |
|
153
|
42 |
|
if ($this->bound instanceof Dependency || $this->bound instanceof DependencyProvider) { |
|
154
|
41 |
|
$this->bound->setScope($scope); |
|
155
|
41 |
|
} |
|
156
|
42 |
|
if ($this->untarget) { |
|
157
|
2 |
|
$this->untarget->setScope($scope); |
|
158
|
2 |
|
} |
|
159
|
|
|
|
|
160
|
42 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
80 |
|
public function __toString() |
|
167
|
|
|
{ |
|
168
|
80 |
|
return $this->interface . '-' . $this->name; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return DependencyInterface |
|
173
|
|
|
*/ |
|
174
|
79 |
|
public function getBound() |
|
175
|
|
|
{ |
|
176
|
79 |
|
return $this->bound; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param DependencyInterface $bound |
|
181
|
|
|
*/ |
|
182
|
17 |
|
public function setBound(DependencyInterface $bound) |
|
183
|
|
|
{ |
|
184
|
17 |
|
$this->bound = $bound; |
|
185
|
17 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param string $interface |
|
189
|
|
|
* |
|
190
|
|
|
* @return bool |
|
191
|
|
|
*/ |
|
192
|
46 |
|
private function hasNotRegistered($interface) |
|
193
|
|
|
{ |
|
194
|
46 |
|
$hasNotRegistered = ! isset($this->container->getContainer()[$interface . '-' . Name::ANY]); |
|
195
|
|
|
|
|
196
|
46 |
|
return $hasNotRegistered; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|