1 | <?php |
||
14 | final class DataLoader implements DataLoaderInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var callable |
||
18 | */ |
||
19 | private $batchLoadFunction; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $promiseQueue = []; |
||
25 | |||
26 | /** |
||
27 | * @var CacheMapInterface |
||
28 | */ |
||
29 | private $promiseCache; |
||
30 | |||
31 | /** |
||
32 | * @var LoopInterface |
||
33 | */ |
||
34 | private $eventLoop; |
||
35 | |||
36 | /** |
||
37 | * @var DataLoaderOptions |
||
38 | */ |
||
39 | private $options; |
||
40 | |||
41 | /** |
||
42 | * Initiates a new DataLoader. |
||
43 | * |
||
44 | * @param callable $batchLoadFunction The function which will be called for the batch loading. |
||
45 | * It must accept an array of keys and returns a Promise which resolves to an array of values. |
||
46 | * @param LoopInterface $loop |
||
47 | * @param CacheMapInterface $cacheMap |
||
48 | * @param null|DataLoaderOptions $options |
||
49 | */ |
||
50 | public function __construct( |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function load($key): ExtendedPromiseInterface |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function loadMany(array $keys): ExtendedPromiseInterface |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function clear($key): void |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function clearAll(): void |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function prime($key, $value): void |
||
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 | private function scheduleDispatch(): void |
||
161 | |||
162 | /** |
||
163 | * Resets and dispatches the DataLoaders queue. |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | private function dispatchQueue(): void |
||
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 | 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 | private function dispatchQueueInMultipleBatches(array $queue, $maxBatchSize): void |
||
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 | private function handleSuccessfulDispatch(array $batch, array $values): void |
||
246 | |||
247 | /** |
||
248 | * Handles the failed batch dispatch. |
||
249 | * |
||
250 | * @param array $batch |
||
251 | * @param \Exception $error |
||
252 | */ |
||
253 | 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 | * @throws DataLoaderException |
||
269 | */ |
||
270 | private function validateBatchPromiseOutput($values, $keys): void |
||
289 | |||
290 | /** |
||
291 | * Validates the batch promise returned from the batch load function. |
||
292 | * |
||
293 | * @param $batchPromise |
||
294 | * |
||
295 | * @throws DataLoaderException |
||
296 | */ |
||
297 | private function validateBatchPromise($batchPromise): void |
||
307 | } |
||
308 |