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) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 2 | public function loadMany(array $keys) |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 11 | public function clear($key) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 1 | public function clearAll() |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 4 | public function prime($key, $value) |
|
140 | |||
141 | /** |
||
142 | * Schedules the dispatch to happen on the next tick of the EventLoop |
||
143 | * If batching is disabled, schedule the dispatch immediately. |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | 29 | private function scheduleDispatch() |
|
161 | |||
162 | /** |
||
163 | * Resets and dispatches the DataLoaders queue. |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | 28 | private function dispatchQueue() |
|
180 | |||
181 | /** |
||
182 | * Dispatches a batch of a queue. The given batch can also be the whole queue. |
||
183 | * |
||
184 | * @param array $batch |
||
185 | */ |
||
186 | 28 | private function dispatchQueueBatch($batch) |
|
209 | |||
210 | /** |
||
211 | * Dispatches the given queue in multiple batches. |
||
212 | * |
||
213 | * @param array $queue |
||
214 | * @param int $maxBatchSize |
||
215 | * |
||
216 | * @return void |
||
217 | */ |
||
218 | 1 | private function dispatchQueueInMultipleBatches(array $queue, $maxBatchSize) |
|
228 | |||
229 | /** |
||
230 | * Handles the batch by resolving the promises and rejecting ones that return Exceptions. |
||
231 | * |
||
232 | * @param array $batch |
||
233 | * @param array $values |
||
234 | */ |
||
235 | 21 | private function handleSuccessfulDispatch(array $batch, array $values) |
|
246 | |||
247 | /** |
||
248 | * Handles the failed batch dispatch. |
||
249 | * |
||
250 | * @param array $batch |
||
251 | * @param \Exception $error |
||
252 | */ |
||
253 | 7 | private function handleFailedDispatch(array $batch, \Exception $error) |
|
261 | |||
262 | /** |
||
263 | * Validates the batch promise's output. |
||
264 | * |
||
265 | * @param array $values Values from resolved promise. |
||
266 | * @param array $keys Keys which the DataLoaders load was called with |
||
267 | */ |
||
268 | 23 | private function validateBatchPromiseOutput($values, $keys) |
|
287 | |||
288 | /** |
||
289 | * Validates the batch promise returned from the batch load function. |
||
290 | * |
||
291 | * @param $batchPromise |
||
292 | * |
||
293 | * @throws DataLoaderException |
||
294 | */ |
||
295 | 28 | private function validateBatchPromise($batchPromise) |
|
305 | } |
||
306 |