1
0
Fork 0
mirror of https://github.com/shufflingpixels/php-io.git synced 2026-10-16 05:06:45 +02:00

Adopt Go-style minimal interfaces for streams

Replace PSR-7 StreamInterface with a set of small, composable interfaces
(ReaderInterface, WriterInterface, SeekerInterface and their
combinations).
BinaryReader now depends only on ReaderInterface, BinaryWriter on
WriterInterface, making both easy to satisfy with any byte source or
sink.

Buffer and Resource are simplified — no capability flags, no
detach/close
lifecycle, no PSR-7 metadata methods. LimitedResource is replaced by
LimitedReader, a lightweight reader that enforces a byte count limit.
BinaryReader::read() is renamed to readExact() for clarity.

Tests updated throughout to match the new APIs.
This commit is contained in:
Henrik Hautakoski 2026-06-27 08:40:11 +02:00
parent a27c0a87ea
commit 07ec405512
25 changed files with 451 additions and 1168 deletions

View file

@ -4,31 +4,27 @@ use Shufflingpixels\IO\Exception\IOException;
use Shufflingpixels\IO\File;
use Shufflingpixels\IO\FileMode;
it('opens readable files and reads contents', function () {
it('opens a file and reads its contents', function () {
$path = tempnam(sys_get_temp_dir(), 'php-io-');
file_put_contents($path, 'hello');
$file = File::open($path, FileMode::READ);
expect($file->isReadable())->toBeTrue()
->and($file->isWritable())->toBeFalse()
->and($file->getSize())->toBe(5)
expect($file->length())->toBe(5)
->and($file->read(5))->toBe('hello');
$file->close();
unlink($path);
});
it('opens read-write files and persists writes', function () {
it('opens a file for reading and writing', function () {
$path = tempnam(sys_get_temp_dir(), 'php-io-');
file_put_contents($path, 'abc');
$file = File::open($path, FileMode::RW);
$file->seek(0);
expect($file->isReadable())->toBeTrue()
->and($file->isWritable())->toBeTrue()
->and($file->write('X'))->toBe(1);
expect($file->write('X'))->toBe(1);
$file->seek(0);
expect($file->read(3))->toBe('Xbc');
@ -37,15 +33,13 @@ it('opens read-write files and persists writes', function () {
unlink($path);
});
it('opens write mode files and truncates existing contents', function () {
it('opens a file for writing and truncates existing contents', function () {
$path = tempnam(sys_get_temp_dir(), 'php-io-');
file_put_contents($path, 'abcdef');
$file = File::open($path, FileMode::WRITE);
expect($file->isReadable())->toBeFalse()
->and($file->isWritable())->toBeTrue()
->and($file->getSize())->toBe(0)
expect($file->length())->toBe(0)
->and($file->write('xy'))->toBe(2);
$file->close();
@ -53,7 +47,19 @@ it('opens write mode files and truncates existing contents', function () {
unlink($path);
});
it('throws an io exception when opening a missing path', function () {
it('throws when writing to a read-only file', function () {
$path = tempnam(sys_get_temp_dir(), 'php-io-');
file_put_contents($path, 'hello');
$file = File::open($path, FileMode::READ);
expect(fn () => $file->write('x'))->toThrow(IOException::class);
$file->close();
unlink($path);
});
it('throws an IOException when opening a missing path', function () {
$path = sys_get_temp_dir() . '/php-io-missing-dir/' . uniqid('', true) . '.txt';
expect(fn () => File::open($path, FileMode::READ))->toThrow(IOException::class);