amphp/parser
Streaming parser helper for AMPHP: build incremental, generator-based parsers for line-, delimiter-, or length-based protocols. Feed data via Parser::push(); yield a delimiter string, byte length, or null to flush/await more input. PHP 7.4+ compatible.
amphp/parser is a low-level utility for building streaming parsers using PHP generators. To begin, install via Composer: composer require amphp/parser. The core concept is to define a parser as a generator function that yields parsing results and accepts input chunks incrementally. The primary entry point is the Parser class, which wraps your generator and handles iteration logic. Start by defining a simple parser for a fixed-size header or delimiter-based stream — for example, parsing newline-terminated messages — using yield to return parsed chunks as they become available.
function* (string &$input): Generator where you consume $input incrementally, yield completed values (e.g., parsed messages), and update $input in-place (or via slicing) as you go.parseUntil(), parseBytes()) that chain to form more complex parsers — like building JSON tokens or HTTP headers line-by-line.BufferedInputStream or InputStream to feed parsed streams from network sockets (e.g., custom protocols over TCP).next()/send()) to enable backpressure — ideal for high-throughput or memory-constrained streaming scenarios.string &$input by reference; it’s your responsibility to consume bytes (e.g., substr($input, $consumed) or unset chunks) to avoid re-processing or memory bloat.yield on complete semantic units (e.g., a full record), not partial state — partials must be accumulated internally in the generator’s scope.null, throwing exceptions, or returning error types within your yielded structure.substr() or unpack() on the existing buffer. Reuse generator instances where possible to reduce allocation overhead.IteratorIterator or Generator::current() during development, feeding known test bytes via send() to trace state transitions.How can I help you explore Laravel packages today?