diff --git a/CHANGELOG.md b/CHANGELOG.md index dec0398..e4e026d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Added + +- `Innmind\Filesystem\File\Content::io()` now accepts an instance of `Innmind\IO\Files\Read` + ## 8.0.0 - 2025-04-14 ### Added diff --git a/proofs/file/content.php b/proofs/file/content.php index 07820a5..0b9ff5b 100644 --- a/proofs/file/content.php +++ b/proofs/file/content.php @@ -44,6 +44,13 @@ ), )), ], + [ + 'Content::io()', + Set::of('LICENSE', 'CHANGELOG.md', 'composer.json') + ->map(static fn($path) => Model::io( + $io->files()->read(Path::of($path)), + )), + ], [ 'Content::none()', Set::of(Model::none()), diff --git a/src/File/Content.php b/src/File/Content.php index 6718cc4..f60b1aa 100644 --- a/src/File/Content.php +++ b/src/File/Content.php @@ -10,6 +10,7 @@ use Innmind\IO\{ IO, Streams\Stream, + Files\Read, Stream\Size, }; use Innmind\Url\Path; @@ -45,7 +46,7 @@ public static function atPath( /** * @psalm-pure */ - public static function io(Stream $io): self + public static function io(Stream|Read $io): self { return new self(Content\IO::of($io)); } diff --git a/src/File/Content/IO.php b/src/File/Content/IO.php index bb21e73..a46c54e 100644 --- a/src/File/Content/IO.php +++ b/src/File/Content/IO.php @@ -5,6 +5,7 @@ use Innmind\IO\{ Streams\Stream, + Files\Read, Frame, }; use Innmind\Immutable\{ @@ -21,14 +22,14 @@ final class IO implements Implementation { private function __construct( - private Stream $io, + private Stream|Read $io, ) { } /** * @psalm-pure */ - public static function of(Stream $io): self + public static function of(Stream|Read $io): self { return new self($io); } @@ -60,6 +61,15 @@ public function filter(callable $filter): Implementation #[\Override] public function lines(): Sequence { + if ($this->io instanceof Read) { + /** @psalm-suppress ImpureMethodCall */ + return $this + ->io + ->watch() + ->lines() + ->map(Line::fromStream(...)); + } + /** @psalm-suppress ImpureMethodCall */ return $this ->io @@ -81,6 +91,11 @@ public function reduce($carry, callable $reducer) #[\Override] public function size(): Maybe { + if ($this->io instanceof Read) { + /** @psalm-suppress ImpureMethodCall */ + return $this->io->size(); + } + /** @psalm-suppress ImpureMethodCall */ return $this->io->read()->internal()->size(); } @@ -97,6 +112,14 @@ public function toString(): string #[\Override] public function chunks(): Sequence { + if ($this->io instanceof Read) { + /** @psalm-suppress ImpureMethodCall */ + return $this + ->io + ->watch() + ->chunks(8192); + } + /** @psalm-suppress ImpureMethodCall */ return $this ->io