From ded2ff9676485a45a390d238c2e844c7a9b99d7f Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 4 Dec 2022 14:25:09 +0100 Subject: [PATCH 1/4] remove unreachable null coalesce --- src/Watch/Select.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Watch/Select.php b/src/Watch/Select.php index 74950df..ec40916 100644 --- a/src/Watch/Select.php +++ b/src/Watch/Select.php @@ -93,7 +93,7 @@ public function __invoke(): Maybe */ $writable = $this ->write - ->filter(static fn($resource) => \in_array($resource, $write ?? [], true)) + ->filter(static fn($resource) => \in_array($resource, $write, true)) ->values() ->reduce( Set::of(), From 07ad460a8fdb53af752413774830f7a8a175d9d0 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 4 Dec 2022 15:25:18 +0100 Subject: [PATCH 2/4] add Streams entrypoint --- CHANGELOG.md | 6 +++ README.md | 12 +++-- src/Streams.php | 36 ++++++++++++++ src/Streams/Readable.php | 27 +++++++++++ src/Streams/Temporary.php | 29 ++++++++++++ src/Streams/Watch.php | 35 ++++++++++++++ src/Streams/Writable.php | 27 +++++++++++ tests/StreamsTest.php | 99 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 266 insertions(+), 5 deletions(-) create mode 100644 src/Streams.php create mode 100644 src/Streams/Readable.php create mode 100644 src/Streams/Temporary.php create mode 100644 src/Streams/Watch.php create mode 100644 src/Streams/Writable.php create mode 100644 tests/StreamsTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 518610a..4a0f1d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Added + +- `Innmind\Stream\Streams` to have a central point from where to open watch for streams + ## 3.2.0 - 2022-03-19 ### Added diff --git a/README.md b/README.md index 0eb2c03..ae7cfec 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,10 @@ composer require innmind/stream File handling: ```php -use Innmind\Stream\Readable\Stream; +use Innmind\Stream\Streams; use Innmind\Url\Path; -$file = Stream::open(Path::of('/some/path/to/a/file')); +$file = Streams::of()->readable()->open(Path::of('/some/path/to/a/file')); while (!$file->end()) { echo $file->readLine()->match( @@ -40,13 +40,15 @@ Socket handling: ```php use Innmind\Stream\{ Stream\Bidirectional, - Watch\Select, + Streams, }; use Innmind\TimeContinuum\Earth\ElapsedPeriod; use Innmind\Immutable\Either; -$socket = new Bidirectional(stream_socket_client('unix:///path/to/socket.sock')); -$select = Select::timeoutAfter(new ElapsedPeriod(60 * 1000)) // select with a 1 minute timeout +$socket = Bidirectional::of(\stream_socket_client('unix:///path/to/socket.sock')); +$select = Streams::of() + ->watch() + ->timeoutAfter(new ElapsedPeriod(60 * 1000)) // select with a 1 minute timeout ->forRead($socket); do { diff --git a/src/Streams.php b/src/Streams.php new file mode 100644 index 0000000..ad37575 --- /dev/null +++ b/src/Streams.php @@ -0,0 +1,36 @@ +toString(), 'w')); + } +} diff --git a/tests/StreamsTest.php b/tests/StreamsTest.php new file mode 100644 index 0000000..956065d --- /dev/null +++ b/tests/StreamsTest.php @@ -0,0 +1,99 @@ +temporary()->new()->write(Str::of('a'))->match( + static fn($a) => $a, + static fn() => null, + ); + $b = $streams->temporary()->new()->write(Str::of('b'))->match( + static fn($a) => $a, + static fn() => null, + ); + + $this->assertNotNull($a); + $this->assertNotNull($b); + $this->assertNotSame($a, $b); + $this->assertSame( + 'a', + $a->toString()->match( + static fn($string) => $string, + static fn() => null, + ), + ); + $this->assertSame( + 'b', + $b->toString()->match( + static fn($string) => $string, + static fn() => null, + ), + ); + } + + public function testOpenReadable() + { + $self = Streams::of() + ->readable() + ->open(Path::of(__FILE__)) + ->toString() + ->match( + static fn($self) => $self, + static fn() => null, + ); + + $this->assertSame(\file_get_contents(__FILE__), $self); + } + + public function testOpenWritable() + { + $path = Path::of(\tempnam(\sys_get_temp_dir(), 'streams')); + $streams = Streams::of(); + $streams + ->writable() + ->open($path) + ->write(Str::of('foo')); + + $this->assertSame( + 'foo', + $streams + ->readable() + ->open($path) + ->toString() + ->match( + static fn($string) => $string, + static fn() => null, + ), + ); + } + + public function testWatch() + { + $this->assertInstanceOf( + Select::class, + Streams::of() + ->watch() + ->waitForever(), + ); + $this->assertInstanceOf( + Select::class, + Streams::of() + ->watch() + ->timeoutAfter(new ElapsedPeriod(1)), + ); + } +} From 03e0992f1546ec895f88b22c4d19c2eac2a94067 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 4 Dec 2022 15:30:33 +0100 Subject: [PATCH 3/4] bump minimum version of psalm --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 239c907..8f5cbc7 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "require-dev": { "phpunit/phpunit": "~9.0", "symfony/process": "~6.0", - "vimeo/psalm": "^4.21", + "vimeo/psalm": "^4.30", "nikic/php-parser": "^4.13.2", "innmind/black-box": "^4.8", "innmind/coding-standard": "~2.0" From 3fa0aa370e6172479891f5f829f7e537d21f15d8 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 4 Dec 2022 15:40:59 +0100 Subject: [PATCH 4/4] specify next release --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a0f1d8..538a3ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## [Unreleased] +## 3.3.0 - 2022-12-04 ### Added