1 | <?php |
||
7 | final class Bind |
||
8 | { |
||
9 | /** |
||
10 | * @var Container |
||
11 | */ |
||
12 | private $container; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $interface; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $name = Name::ANY; |
||
23 | |||
24 | /** |
||
25 | * @var DependencyInterface |
||
26 | */ |
||
27 | private $bound; |
||
28 | |||
29 | /** |
||
30 | * @var BindValidator |
||
31 | */ |
||
32 | private $validate; |
||
33 | |||
34 | /** |
||
35 | * @var null|Untarget |
||
36 | */ |
||
37 | private $untarget; |
||
38 | |||
39 | /** |
||
40 | * @param Container $container dependency container |
||
41 | * @param string $interface interface or concrete class name |
||
42 | */ |
||
43 | public function __construct(Container $container, string $interface) |
||
56 | |||
57 | 50 | public function __destruct() |
|
64 | 87 | ||
65 | 43 | public function __toString() |
|
69 | |||
70 | 87 | /** |
|
71 | * Set dependency name |
||
72 | 87 | */ |
|
73 | public function annotatedWith(string $name) : self |
||
74 | { |
||
75 | $this->name = $name; |
||
76 | |||
77 | return $this; |
||
78 | 31 | } |
|
79 | |||
80 | 31 | /** |
|
81 | * Bind to class |
||
82 | 31 | */ |
|
83 | public function to(string $class) : self |
||
84 | { |
||
85 | $this->untarget = null; |
||
86 | $refClass = $this->validate->to($this->interface, $class); |
||
87 | $this->bound = (new DependencyFactory)->newAnnotatedDependency($refClass); |
||
88 | 61 | $this->container->add($this); |
|
89 | |||
90 | 61 | return $this; |
|
91 | 61 | } |
|
92 | 59 | ||
93 | 59 | /** |
|
94 | * Bind to constructor |
||
95 | 59 | * |
|
96 | * @param string $class class name |
||
97 | * @param string | array $name "varName=bindName,..." or [[$varName => $bindName],[$varName => $bindName]...] |
||
98 | * @param InjectionPoints $injectionPoints injection points |
||
99 | * @param string $postConstruct method name of initialization after all dependencies are injected* |
||
100 | */ |
||
101 | public function toConstructor(string $class, $name, InjectionPoints $injectionPoints = null, string $postConstruct = null) : self |
||
102 | { |
||
103 | if (\is_array($name)) { |
||
104 | $name = $this->getStringName($name); |
||
105 | } |
||
106 | 4 | $this->untarget = null; |
|
107 | $postConstructRef = $postConstruct ? (new NewReflectionMethod)($class, $postConstruct) : null; |
||
108 | 4 | $this->bound = (new DependencyFactory)->newToConstructor((new NewReflectionClass)($class), $name, $injectionPoints, $postConstructRef); |
|
109 | 1 | $this->container->add($this); |
|
110 | |||
111 | 4 | return $this; |
|
112 | 4 | } |
|
113 | 4 | ||
114 | 4 | /** |
|
115 | * Bind to provider |
||
116 | 4 | */ |
|
117 | public function toProvider(string $provider, string $context = '') : self |
||
126 | 48 | ||
127 | 48 | /** |
|
128 | * Bind to instance |
||
129 | 48 | */ |
|
130 | public function toInstance($instance) : self |
||
138 | |||
139 | 80 | /** |
|
140 | 80 | * Set scope |
|
141 | 80 | */ |
|
142 | public function in(string $scope) : self |
||
143 | 80 | { |
|
144 | if ($this->bound instanceof Dependency || $this->bound instanceof DependencyProvider) { |
||
145 | $this->bound->setScope($scope); |
||
146 | } |
||
147 | if ($this->untarget) { |
||
148 | $this->untarget->setScope($scope); |
||
149 | 46 | } |
|
150 | |||
151 | 46 | return $this; |
|
152 | 45 | } |
|
153 | |||
154 | 46 | public function getBound() : DependencyInterface |
|
155 | 41 | { |
|
156 | return $this->bound; |
||
157 | } |
||
158 | 46 | ||
159 | public function setBound(DependencyInterface $bound) |
||
163 | 86 | ||
164 | private function isRegistered(string $interface) : bool |
||
168 | 43 | ||
169 | 43 | /** |
|
170 | * Return string |
||
171 | 50 | * |
|
172 | * input: [['varA' => 'nameA'], ['varB' => 'nameB']] |
||
173 | 50 | * output: "varA=nameA,varB=nameB" |
|
174 | */ |
||
175 | 50 | private function getStringName(array $name) : string |
|
185 | } |
||
186 |