|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/discovery package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Puli\Discovery; |
|
13
|
|
|
|
|
14
|
|
|
use Puli\Discovery\Api\Binding\Binding; |
|
15
|
|
|
use Puli\Discovery\Api\Type\BindingType; |
|
16
|
|
|
use Puli\Discovery\Api\Type\DuplicateTypeException; |
|
17
|
|
|
use Puli\Discovery\Api\Type\NoSuchTypeException; |
|
18
|
|
|
use Webmozart\Assert\Assert; |
|
19
|
|
|
use Webmozart\Expression\Expr; |
|
20
|
|
|
use Webmozart\Expression\Expression; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A resource discovery that holds the bindings in memory. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 1.0 |
|
26
|
|
|
* |
|
27
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class InMemoryDiscovery extends AbstractEditableDiscovery |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var BindingType[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $types = array(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Binding[][] |
|
38
|
|
|
*/ |
|
39
|
|
|
private $bindingsByTypeName = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
27 |
|
public function addBindingType(BindingType $type) |
|
45
|
|
|
{ |
|
46
|
27 |
|
if (isset($this->types[$type->getName()])) { |
|
47
|
1 |
|
throw DuplicateTypeException::forTypeName($type->getName()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
27 |
|
$this->types[$type->getName()] = $type; |
|
51
|
27 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
4 |
|
public function removeBindingType($typeName) |
|
57
|
|
|
{ |
|
58
|
4 |
|
Assert::stringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s'); |
|
59
|
|
|
|
|
60
|
3 |
|
unset($this->types[$typeName]); |
|
61
|
|
|
|
|
62
|
3 |
|
$this->removeBindingsWithTypeName($typeName); |
|
63
|
3 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function removeBindingTypes() |
|
69
|
|
|
{ |
|
70
|
2 |
|
$this->types = array(); |
|
71
|
2 |
|
$this->bindingsByTypeName = array(); |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
5 |
|
public function hasBindingType($typeName) |
|
78
|
|
|
{ |
|
79
|
5 |
|
Assert::stringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s'); |
|
80
|
|
|
|
|
81
|
4 |
|
return isset($this->types[$typeName]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
21 |
|
public function getBindingType($typeName) |
|
88
|
|
|
{ |
|
89
|
21 |
|
Assert::stringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s'); |
|
90
|
|
|
|
|
91
|
20 |
|
if (!isset($this->types[$typeName])) { |
|
92
|
2 |
|
throw NoSuchTypeException::forTypeName($typeName); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
18 |
|
return $this->types[$typeName]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function hasBindingTypes() |
|
102
|
|
|
{ |
|
103
|
|
|
return count($this->types) > 0; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
3 |
|
public function getBindingTypes() |
|
110
|
|
|
{ |
|
111
|
3 |
|
return array_values($this->types); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
16 |
|
public function addBinding(Binding $binding) |
|
118
|
|
|
{ |
|
119
|
16 |
|
$typeName = $binding->getTypeName(); |
|
120
|
|
|
|
|
121
|
16 |
|
if (isset($this->bindingsByTypeName[$typeName])) { |
|
122
|
9 |
|
foreach ($this->bindingsByTypeName[$typeName] as $other) { |
|
123
|
9 |
|
if ($binding->equals($other)) { |
|
124
|
9 |
|
return; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
16 |
|
$this->initializeBinding($binding); |
|
130
|
|
|
|
|
131
|
14 |
|
$this->bindingsByTypeName[$typeName][] = $binding; |
|
132
|
14 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
10 |
|
public function findBindings($typeName, Expression $expr = null) |
|
138
|
|
|
{ |
|
139
|
10 |
|
Assert::stringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s'); |
|
140
|
|
|
|
|
141
|
9 |
|
if (!isset($this->bindingsByTypeName[$typeName])) { |
|
142
|
3 |
|
return array(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
7 |
|
$bindings = $this->bindingsByTypeName[$typeName]; |
|
146
|
|
|
|
|
147
|
7 |
|
if (null !== $expr) { |
|
148
|
1 |
|
$bindings = Expr::filter($bindings, $expr); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
7 |
|
return array_values($bindings); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
15 |
|
public function getBindings() |
|
158
|
|
|
{ |
|
159
|
15 |
|
$bindings = array(); |
|
160
|
|
|
|
|
161
|
15 |
|
foreach ($this->bindingsByTypeName as $bindingsOfType) { |
|
162
|
7 |
|
foreach ($bindingsOfType as $binding) { |
|
163
|
7 |
|
$bindings[] = $binding; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
15 |
|
return $bindings; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritdoc} |
|
172
|
|
|
*/ |
|
173
|
2 |
|
protected function removeAllBindings() |
|
174
|
|
|
{ |
|
175
|
2 |
|
$this->bindingsByTypeName = array(); |
|
176
|
2 |
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* {@inheritdoc} |
|
180
|
|
|
*/ |
|
181
|
1 |
|
protected function removeBindingsThatMatch(Expression $expr) |
|
182
|
|
|
{ |
|
183
|
1 |
|
foreach ($this->bindingsByTypeName as $typeName => $bindings) { |
|
184
|
1 |
|
foreach ($bindings as $key => $binding) { |
|
185
|
1 |
|
if ($expr->evaluate($binding)) { |
|
186
|
1 |
|
unset($this->bindingsByTypeName[$typeName][$key]); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
if (0 === count($this->bindingsByTypeName[$typeName])) { |
|
191
|
1 |
|
unset($this->bindingsByTypeName[$typeName]); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
1 |
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* {@inheritdoc} |
|
198
|
|
|
*/ |
|
199
|
6 |
|
protected function removeBindingsWithTypeName($typeName) |
|
200
|
|
|
{ |
|
201
|
6 |
|
if (!isset($this->bindingsByTypeName[$typeName])) { |
|
202
|
4 |
|
return; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
2 |
|
unset($this->bindingsByTypeName[$typeName]); |
|
206
|
2 |
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* {@inheritdoc} |
|
210
|
|
|
*/ |
|
211
|
3 |
|
protected function removeBindingsWithTypeNameThatMatch($typeName, Expression $expr) |
|
212
|
|
|
{ |
|
213
|
3 |
|
if (!isset($this->bindingsByTypeName[$typeName])) { |
|
214
|
2 |
|
return; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
1 |
|
foreach ($this->bindingsByTypeName[$typeName] as $key => $binding) { |
|
218
|
1 |
|
if ($expr->evaluate($binding)) { |
|
219
|
1 |
|
unset($this->bindingsByTypeName[$typeName][$key]); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
1 |
|
if (0 === count($this->bindingsByTypeName[$typeName])) { |
|
224
|
|
|
unset($this->bindingsByTypeName[$typeName]); |
|
225
|
|
|
} |
|
226
|
1 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* {@inheritdoc} |
|
230
|
|
|
*/ |
|
231
|
2 |
|
protected function hasAnyBinding() |
|
232
|
|
|
{ |
|
233
|
2 |
|
return count($this->bindingsByTypeName) > 0; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* {@inheritdoc} |
|
238
|
|
|
*/ |
|
239
|
|
|
protected function hasBindingsThatMatch(Expression $expr) |
|
240
|
|
|
{ |
|
241
|
|
|
foreach ($this->bindingsByTypeName as $typeName => $bindings) { |
|
242
|
|
|
foreach ($bindings as $key => $binding) { |
|
243
|
|
|
if ($expr->evaluate($binding)) { |
|
244
|
|
|
return true; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return false; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* {@inheritdoc} |
|
254
|
|
|
*/ |
|
255
|
2 |
|
protected function hasBindingsWithTypeName($typeName) |
|
256
|
|
|
{ |
|
257
|
2 |
|
return !empty($this->bindingsByTypeName[$typeName]); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* {@inheritdoc} |
|
262
|
|
|
*/ |
|
263
|
2 |
|
protected function hasBindingsWithTypeNameThatMatch($typeName, Expression $expr) |
|
264
|
|
|
{ |
|
265
|
2 |
|
if (!isset($this->bindingsByTypeName[$typeName])) { |
|
266
|
1 |
|
return false; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
1 |
|
foreach ($this->bindingsByTypeName[$typeName] as $binding) { |
|
270
|
1 |
|
if ($expr->evaluate($binding)) { |
|
271
|
1 |
|
return true; |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
1 |
|
return false; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|