diff --git a/CHANGELOG.md b/CHANGELOG.md index d968215..0f84252 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 7.1.0 - 2023-10-22 + +### Added + +- `Innmind\Filesystem\File\Content::io()` + ## 7.0.0 - 2023-10-21 ### Added diff --git a/proofs/file/content.php b/proofs/file/content.php index b67e31d..c40bf7d 100644 --- a/proofs/file/content.php +++ b/proofs/file/content.php @@ -37,6 +37,16 @@ $path, )), ], + [ + 'Content::io()', + Set\Elements::of('LICENSE', 'CHANGELOG.md', 'composer.json') + ->map(Path::of(...)) + ->map(static fn($path) => Model::io( + $io->wrap( + $capabilities->readable()->open($path), + ), + )), + ], [ 'Content::none()', Set\Elements::of(Model::none()), diff --git a/src/File/Content.php b/src/File/Content.php index 5da3478..9de2163 100644 --- a/src/File/Content.php +++ b/src/File/Content.php @@ -44,6 +44,14 @@ public static function atPath( return new self(Content\AtPath::of($capabilities, $io, $path)); } + /** + * @psalm-pure + */ + public static function io(IO\Readable\Stream $io): self + { + return new self(Content\IO::of($io)); + } + /** * @psalm-pure * diff --git a/src/File/Content/IO.php b/src/File/Content/IO.php new file mode 100644 index 0000000..7a41724 --- /dev/null +++ b/src/File/Content/IO.php @@ -0,0 +1,96 @@ +io = $io; + } + + /** + * @psalm-pure + */ + public static function of(Stream $io): self + { + return new self($io); + } + + public function foreach(callable $function): SideEffect + { + return $this->lines()->foreach($function); + } + + public function map(callable $map): Implementation + { + return Lines::of($this->lines()->map($map)); + } + + public function flatMap(callable $map): Implementation + { + return Lines::of($this->lines())->flatMap($map); + } + + public function filter(callable $filter): Implementation + { + return Lines::of($this->lines()->filter($filter)); + } + + public function lines(): Sequence + { + /** @psalm-suppress ImpureMethodCall */ + return $this + ->io + ->lines() + ->lazy() + ->rewindable() + ->sequence() + ->map(static fn($line) => Line::fromStream($line)); + } + + public function reduce($carry, callable $reducer) + { + return $this->lines()->reduce($carry, $reducer); + } + + public function size(): Maybe + { + /** @psalm-suppress ImpureMethodCall */ + return $this->io->size(); + } + + public function toString(): string + { + return $this + ->chunks() + ->fold(new Concat) + ->toString(); + } + + public function chunks(): Sequence + { + /** @psalm-suppress ImpureMethodCall */ + return $this + ->io + ->chunks(8192) + ->lazy() + ->rewindable() + ->sequence(); + } +}