jotaelesalinas/php-data-streams

Read and write huge CSV, JSON, XML and Excel streams in PHP without running out of memory.

Maintainers

Package info

github.com/jotaelesalinas/php-data-streams

pkg:composer/jotaelesalinas/php-data-streams

Statistics

Installs: 0

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

v2.0.0 2026-06-09 01:42 UTC

This package is auto-updated.

Last update: 2026-06-10 11:31:06 UTC


README

[!IMPORTANT] This is a breaking v2 release and is not compatible with the old jotaelesalinas/php-rwgen package. The package name, namespace, and public API have changed.

License CI

php-data-streams is a PHP streaming library for reading and writing large CSV, JSON, XML, XLSX, KML, HTTP and database-backed records without loading everything into memory.

It is designed for data pipelines, import/export jobs, CLI tools and integrations where you want small, explicit reader and writer abstractions instead of heavyweight framework-specific layers.

What it solves

  • Process large files and responses record by record.
  • Keep memory usage predictable while iterating through data.
  • Compose simple readers and writers around generators and iterables.
  • Reuse a shared contract across multiple formats.

How This Repository Is Organized

This project is a monorepo split into several smaller packages.

That means the repository contains multiple packages in one place, but you do not need to install all of them. Install only the package or packages you actually need.

For example:

  • If you only work with CSV files, install the CSV package.
  • If you only need JSON Lines, install the JSON package.
  • If you want the shared reader and writer interfaces for your own code, install the core package.

Packages

  • core for the shared Reader and Writer contracts.
  • csv for CSV and TSV readers and writers.
  • json for JSON, JSON arrays and NDJSON / JSON Lines.
  • xml for lazy XML readers and streaming XML writers.
  • kml for KML output built on XML.
  • pdo for database cursor helpers.
  • http for line and page-oriented HTTP streams.
  • excel for minimal XLSX sheet readers.

Quick Example

use JLSalinas\DataStreams\Csv\CsvReader;

$reader = new CsvReader(__DIR__ . '/customers.csv');

foreach ($reader as $customer) {
    echo $customer['name'] . PHP_EOL;
}

Installation

Install the package you need via Composer. For example, to work with CSV files:

composer require jotaelesalinas/php-data-streams-csv

If you want to work with JSON Lines:

composer require jotaelesalinas/php-data-streams-json

If you want to build your own reader or writer and only need the shared interfaces:

composer require jotaelesalinas/php-data-streams-core

You can also install more than one package if your project needs them.

Example Usage

CSV reader

use JLSalinas\DataStreams\Csv\CsvReader;

$reader = new CsvReader(__DIR__ . '/customers.csv');

foreach ($reader as $customer) {
    echo $customer['name'] . PHP_EOL;
}

CSV writer

use JLSalinas\DataStreams\Csv\CsvWriter;

$writer = new CsvWriter(__DIR__ . '/export.csv');
$writer->write(['name' => 'Ada', 'email' => 'ada@example.com']);
$writer->write(['name' => 'Grace', 'email' => 'grace@example.com']);
$writer->close();

JSON reader

use JLSalinas\DataStreams\Json\JsonReader;

$reader = new JsonReader(__DIR__ . '/events.ndjson');

foreach ($reader as $event) {
    var_dump($event);
}

Typical Use Cases

  • CSV/TSV imports and exports.
  • JSON Lines ingestion and generation.
  • XML feeds and document-to-record transforms.
  • XLSX sheet reading in long-running jobs.
  • KML generation from geospatial records.
  • Streaming database results and HTTP responses.

Updating from v1.x

If you are coming from jotaelesalinas/php-rwgen, review these changes before moving your code to this release:

  • Switch Composer to the package you actually use. For example:

    composer remove jotaelesalinas/php-rwgen
    composer require jotaelesalinas/php-data-streams-csv

    If your project uses multiple formats, install the matching subpackages.

  • Update the namespace imports. For example:

    use JLSalinas\RWGen\CsvReader;

    becomes:

    use JLSalinas\DataStreams\Csv\CsvReader;
  • If you previously relied on the monolithic package, move each reader or writer to the matching subpackage. For example, CSV streaming now lives in packages/csv, while the shared contracts live in packages/core.

  • Recheck class names and constructors when you migrate. If you used generic-looking classes such as Reader or Writer, they may now be specific implementations like CsvReader, JsonReader, CsvWriter, or JsonLinesWriter.

  • If your code depended on a single installation for everything, verify your imports and composer.json against the Packages section and the README for each subpackage.