1 | <?php |
||
8 | class DataLoader implements DataLoaderInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var callable |
||
12 | */ |
||
13 | private $batchLoadFunction; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $promiseQueue = []; |
||
19 | |||
20 | /** |
||
21 | * @var CacheMapInterface |
||
22 | */ |
||
23 | private $promiseCache; |
||
24 | |||
25 | /** |
||
26 | * @var LoopInterface |
||
27 | */ |
||
28 | private $eventLoop; |
||
29 | |||
30 | /** |
||
31 | * @var DataLoaderOptions |
||
32 | */ |
||
33 | private $options; |
||
34 | |||
35 | /** |
||
36 | * Initiates a new DataLoader. |
||
37 | * |
||
38 | * @param callable $batchLoadFunction The function which will be called for the batch loading. |
||
39 | * It must accept an array of keys and returns a Promise which resolves to an array of values. |
||
40 | * @param LoopInterface $loop |
||
41 | * @param CacheMapInterface $cacheMap |
||
42 | * @param null|DataLoaderOptions $options |
||
43 | */ |
||
44 | 32 | public function __construct( |
|
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 32 | public function load($key) |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 2 | public function loadMany(array $keys) |
|
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | 7 | public function clear($key) |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 1 | public function clearAll() |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 4 | public function prime($key, $value) |
|
142 | |||
143 | /** |
||
144 | * Schedules the dispatch to happen on the next tick of the EventLoop |
||
145 | * If batching is disabled, schedule the dispatch immediately. |
||
146 | * |
||
147 | * @return void |
||
148 | */ |
||
149 | 29 | private function scheduleDispatch() |
|
163 | |||
164 | /** |
||
165 | * Resets and dispatches the DataLoaders queue. |
||
166 | * |
||
167 | * @return void |
||
168 | */ |
||
169 | 28 | private function dispatchQueue() |
|
182 | |||
183 | /** |
||
184 | * Dispatches a batch of a queue. The given batch can also be the whole queue. |
||
185 | * |
||
186 | * @param array $batch |
||
187 | */ |
||
188 | private function dispatchQueueBatch($batch) |
||
212 | 1 | ||
213 | 1 | /** |
|
214 | 1 | * Dispatches the given queue in multiple batches. |
|
215 | * |
||
216 | 23 | * @param array $queue |
|
217 | 2 | * @param int $maxBatchSize |
|
218 | 2 | * |
|
219 | 2 | * @return void |
|
220 | 2 | */ |
|
221 | 2 | private function dispatchQueueInMultipleBatches(array $queue, $maxBatchSize) |
|
231 | 20 | ||
232 | /** |
||
233 | 22 | * Handles the batch by resolving the promises and rejecting ones that return Exceptions. |
|
234 | 24 | * |
|
235 | 1 | * @param array $batch |
|
236 | 1 | * @param array $values |
|
237 | 1 | */ |
|
238 | 24 | private function handleSuccessfulDispatch(array $batch, array $values) |
|
249 | 1 | ||
250 | /** |
||
251 | 1 | * Handles the failed batch dispatch. |
|
252 | * |
||
253 | 1 | * @param array $batch |
|
254 | 1 | * @param \Exception $error |
|
255 | 1 | */ |
|
256 | 1 | private function handleFailedDispatch(array $batch, \Exception $error) |
|
264 | |||
265 | /** |
||
266 | * Validates the batch promises output. |
||
267 | * |
||
268 | 3 | * @param array $values Values from resolved promise. |
|
269 | * @param array $keys Keys which the DataLoaders load was called with |
||
270 | 3 | */ |
|
271 | private function validateBatchPromiseOutput($values, $keys) |
||
290 | } |
||
291 |