ThrowableGenerator
Generator wrapper for yield be sequential regardless of using Generator::throw., (*1)
From top level view, Generator::throw discards next value from generator,
but it's not completely lost, but instead it becames return value of throw()
method., (*2)
This is rather inconvenient if you want values be returned by yield
., (*3)
<?php
function generator() {
foreach (range(1, 6) as $x) {
try {
yield $x;
} catch (Throwable $e) {
echo " !! exception: {$e->getMessage()}\n";
}
}
}
$generator = generator();
foreach ($generator as $x) {
echo "process: $x\n";
if ($x % 2 === 0) {
$generator->throw(new RuntimeException($x));
}
}
The above code prints:, (*4)
process: 1
process: 2
!! exception: 2
process: 4
!! exception: 4
process: 6
!! exception: 6
i.e values 3
and 5
were "lost" by using throw
. This library makes result to be:, (*5)
process: 1
process: 2
!! exception: 2
process: 3
process: 4
!! exception: 4
process: 5
process: 6
!! exception: 6
See question and discussion on stackoverflow post, (*6)
To use this class, wrap your original generator with this class:, (*7)
$generator = new ThrowableGenerator($generator);
Note: There is API change, throw()
will return nothing instead of next value from generator., (*8)