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 | 32 | public function __construct( |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 32 | public function load($key): ExtendedPromiseInterface |
|
66 | { |
||
67 | 32 | if ($key === null) { |
|
68 | 2 | throw new \InvalidArgumentException(self::class . '::load must be called with a value, but got null'); |
|
69 | } |
||
70 | |||
71 | 30 | if ($this->options->shouldCache() && $this->promiseCache->get($key)) { |
|
72 | 9 | return $this->promiseCache->get($key); |
|
73 | } |
||
74 | |||
75 | 29 | $promise = new Promise( |
|
76 | function (callable $resolve, callable $reject) use ($key) { |
||
77 | 29 | $this->promiseQueue[] = [ |
|
78 | 29 | 'key' => $key, |
|
79 | 29 | 'resolve' => $resolve, |
|
80 | 29 | 'reject' => $reject, |
|
81 | ]; |
||
82 | |||
83 | 29 | if (\count($this->promiseQueue) === 1) { |
|
84 | 29 | $this->scheduleDispatch(); |
|
85 | } |
||
86 | 29 | } |
|
87 | ); |
||
88 | |||
89 | 29 | if ($this->options->shouldCache()) { |
|
90 | 28 | $this->promiseCache->set($key, $promise); |
|
91 | } |
||
92 | |||
93 | 29 | return $promise; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 2 | public function loadMany(array $keys): ExtendedPromiseInterface |
|
100 | { |
||
101 | 2 | return all( |
|
102 | 2 | \array_map( |
|
103 | function ($key) { |
||
104 | 2 | return $this->load($key); |
|
105 | 2 | }, |
|
106 | 2 | $keys |
|
107 | ) |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 11 | public function clear($key): void |
|
115 | { |
||
116 | 11 | $this->promiseCache->delete($key); |
|
117 | 11 | } |
|
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 1 | public function clearAll(): void |
|
123 | { |
||
124 | 1 | $this->promiseCache->clear(); |
|
125 | 1 | } |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 4 | 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 | 29 | private function scheduleDispatch(): void |
|
161 | |||
162 | /** |
||
163 | * Resets and dispatches the DataLoaders queue. |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | 28 | 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 | 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): 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 | 21 | 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 | 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 | * @throws DataLoaderException |
||
269 | */ |
||
270 | 23 | 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 | 28 | private function validateBatchPromise($batchPromise): void |
|
307 | } |
||
308 |