withAll()¶
Iterates over the given stream source and yields its items. During iteration process, every yielded item is stored into a buffer.
When iteration completes the finalization function is invoked providing the collected items as argument.
If iteration of the stream source ends prematurely (for example, via a break statement), the finalization function is invoked when the operator instance is garbage-collected with the items that were collected up to that point.
Warning
The memory consumption of this operator depends on the number of items in the stream and it is considered as memory unsafe.
- class RunOpenCode\Component\Dataset\Operator\WithAll¶
- __construct(iterable<TKey, TValue> $source, callable(Buffer<TKey, TValue>): void $finalizer)¶
- Parameters:
$source –
iterable<TKey, TValue>Stream source to iterate over.$finalizer –
callable(Buffer<TKey, TValue>): voidUser defined callable to invoke when iterator is depleted, or operator instance is garbage collected.
- getIterator()¶
- Returns:
\Traversable<TKey, TValue>Items from the stream source.
Use cases¶
Use this operator when you want to express finalization logic when stream processing is completed, or when stream processing is interrupted prematurely (for example, via a break statement) on iterated items.
Example¶
Mark iterated entities as processed when stream processing is completed.
1<?php
2
3use RunOpenCode\Component\Dataset\Stream;
4use RunOpenCode\Component\Dataset\Model\Buffer;
5
6new Stream(...)
7 ->withAll(function(Buffer $buffer) use ($database): void {
8 foreach ($buffer as $item) {
9 $database->markAsProcessed($item);
10 }
11 });