kjljon/streamingiterable

Convert iteratables into with streams using psr-7 without any other dependencies

v1.0.0 2022-07-18 02:33 UTC

This package is not auto-updated.

Last update: 2024-05-07 09:20:42 UTC


README

The idea of this package is to make it easier to work with iterable data as streams

Problem it solves

I find myself exporting data to the file system and using flysystem's writeStream. This becomes an issue when you don't have much local storage to write large exports to.

So my first iteration was pretty ugly (but worked), but then I saw what Guzzle's PSR-7 package did with StreamWrapper and using PSR-7 and figured I could refactor my app to use PSR-7.

My second iteration worked, but still assumed the export was a specific file type (csv).
When I had the requirements to stream JSON, I decided to refactor a little again by adding the ability to add a formatter decorator.

Usage

$iterable = [
    ['first' => 'Jim', 'last' => 'Thompson', 'month' => 'April'],
    ['first' => 'Tim', 'last' => 'Johnson', 'month' => 'December']
];

$formatter = new KJLJon\StreamingIterable\Formatter\CsvFormatter();
$stream = new KJLJon\StreamingIterable\IterableToReaderStream($iterable, $formatter);
$fp = KJLJon\StreamingIterable\StreamWrapper::getResource($stream);

//do something with stream (IE: flyweight's writeStream($location, $fp);
fpassthru($fp);

fclose($fp);

Questions / Comments

Please open an issue and/or pull request