定制 flyokai/amp-csv-reader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

flyokai/amp-csv-reader

最新稳定版本:0.1.0

Composer 安装命令:

composer require flyokai/amp-csv-reader

包简介

Async csv file reader using amphp

README 文档

README

User docs → README.md · Agent quick-ref → CLAUDE.md · Agent deep dive → AGENTS.md

Async, generator-based CSV streaming for AMPHP 3.x with backpressure-aware row buffering.

Streams a CSV file row by row without buffering the whole file in memory, while staying friendly to the AMPHP cooperative event loop. Multi-line quoted fields are handled correctly.

Features

  • IteratorAggregate — drop into foreach
  • Stateful generator parser → str_getcsv() for final field interpretation
  • Configurable bounded queue for backpressure
  • Cancellation support
  • array2csv() helper for the reverse direction

Installation

composer require flyokai/amp-csv-reader

Quick start

use Amp\File;
use Flyokai\AmpCsvReader\CsvReader;

$stream = File\openFile('data.csv', 'r');
$reader = new CsvReader($stream, bufferSize: 500);

foreach ($reader as $row) {
    // $row is a numeric array — no header handling
}

Constructor

new CsvReader(
    Amp\ByteStream\ReadableStream $stream,
    int $bufferSize = 0,                 // 0 = unlimited
    string $separator = ',',
    string $enclosure = '"',
    string $escape = "\\",
    ?Amp\Cancellation $stopCancellation = null,
);

Lifecycle

ReadableStream → Parser (generator)
    → Buffer accumulation (multi-line quoted fields)
    → consumeBuffer() → str_getcsv()
    → Queue → ConcurrentIterator → foreach
  1. getIterator() lazily creates a Queue and schedules read() on the event loop
  2. read() pulls chunks from the stream, feeds the parser
  3. parse() tracks escape mode for quoted fields, accumulates a row buffer
  4. Complete rows flow through str_getcsv() and into the queue
  5. The consumer iterates via ConcurrentIterator

Helpers

  • isComplete(): bool — whether all rows have been parsed
  • onComplete(\Closure $callback): void — fires when parsing finishes
  • array2csv(array $data, string $delimiter = ',', string $enclosure = '"', string $escape = '\\'): string — array → CSV (uses php://memory)

Gotchas

  • No header handling — every row is a numeric array. Map columns yourself if you need them by name.
  • Empty rows are droppedconsumeBuffer() silently skips lines that trim to empty.
  • bufferSize=0 is unlimited — slow consumers on large files will grow memory unbounded.
  • Single-usegetIterator() is lazy but non-resettable. To re-read the file, open a new stream and a new reader.
  • Cancellation flushes the buffer — the reader still emits buffered rows after cancellation before completing.

See also

  • Used by data-import pipelines built on flyokai/amp-data-pipeline.

License

MIT

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固