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