loophp/iterators
loophp/iterators adds “missing” PHP iterators and iterator aggregates for lazy, composable pipelines: caching rewindable generators, chunk/map/filter/reduce/sort/unique/concat/pack/unpack, plus typed, random, recursive, and resource-based iterables.
Strengths:
MapIterableAggregate, FilterIterableAggregate, ReduceIterableAggregate), which aligns with modern Laravel/Lumen architectures leveraging collections, pipelines, and iterators (e.g., collect(), tap(), pipe()).ArrayIterator), enabling seamless integration without modifying core logic.Gaps:
QueryBuilderIterator).Cursor pagination or Chunk results).Collection methods (e.g., chunk(), filter(), map()) with custom iterators.$collection = collect([1, 2, 3, 4, 5]);
$iterator = new ChunkIterableAggregate($collection->all(), 2);
foreach ($iterator as $chunk) { /* [1,2], [3,4], [5] */ }
paginate()->items(), cursor()).app->bind(InterruptableIterableIteratorAggregate::class, ...)).SPL\CachingIterator under the hood, which may introduce memory spikes for large datasets. Benchmark against Laravel’s Collection caching.CachingIteratorAggregate).length parameter from UniqueIterableAggregate; ensure backward compatibility in legacy code.ChunkIterableAggregate for pagination vs. RecursiveIterableAggregate for nested Eloquent relations?)CachingIteratorAggregate, is the performance gain worth the memory cost in high-traffic endpoints?EloquentCursorIterator) or Query Builder (e.g., ChunkedQueryIterator)?InterruptableIterableIteratorAggregate work with queued jobs)?spatie/array-to-object, league/collection)?Collection methods with iterator-based alternatives (e.g., map() → MapIterableAggregate).$collection = collect([1, 2, 3]);
$mapped = (new MapIterableAggregate($collection->all(), fn($v) => $v * 2));
yield, Generator facade).$generator = Generator::make(function() {
yield from range(1, 1000);
});
$chunked = new ChunkIterableAggregate($generator, 10);
Cursor or paginate(), but no direct integration.$cursor = User::cursor();
$batched = new ChunkIterableAggregate($cursor, 50);
foreach ($iterator as $item) { event(new ItemProcessed($item)); }).Collection methods with iterators in performance-critical paths (e.g., filter() → FilterIterableAggregate).Collection methods post-migration.User::all() → User::cursor() + CachingIteratorAggregate).EloquentRecursiveIterator for nested relations).return-type and no-undefined-offset rules.CachingIteratorAggregate + ChunkIterableAggregate in a micro-benchmark vs. Laravel’s Collection.app->alias('iterators.cache', CachingIteratorAggregate::class)).memory_get_usage()) for CachingIteratorAggregate.stopwatch.laravel-integration.md in your repo.TestCase inheritance, service container binding).InterruptableIterableIteratorAggregate or RecursiveIterableAggregate may require deep debugging (use Xdebug + var_dump($iterator->getIterator())).laravel + php-iterators.CachingIteratorAggregate caches all keys/values in memory. Monitor with:
$iterator = new CachingIteratorAggregate($largeGenerator);
foreach ($iterator as $item) {
if (memory_get_usage() > 500MB) {
throw new \RuntimeException("Memory limit exceeded");
}
}
SimpleCachingIteratorAggregate for lightweight caching.SplFileObject) for very large datasets.How can I help you explore Laravel packages today?