From e5523bc8b74da9a65c635ec9578d0da734503996 Mon Sep 17 00:00:00 2001 From: Matthias Neid Date: Tue, 8 Apr 2025 18:16:42 +0200 Subject: [PATCH] add rewind position feature --- src/Exception/RewindException.php | 15 +++++++++ .../Features/RewindPositionInterface.php | 23 +++++++++++++ .../Features/SetPositionInterface.php | 2 +- src/System/File/File.php | 2 ++ src/System/File/TempMemoryFile.php | 2 ++ src/System/Link/FileLink.php | 9 +++++ .../Traits/RewindSocketPositionTrait.php | 32 ++++++++++++++++++ .../System/File/VolatileFileTestTrait.php | 33 +++++++++++++++++++ test/Unit/System/Link/FileLinkTest.php | 19 +++++++++++ 9 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/Exception/RewindException.php create mode 100644 src/Interfaces/Features/RewindPositionInterface.php create mode 100644 src/System/Socket/Traits/RewindSocketPositionTrait.php diff --git a/src/Exception/RewindException.php b/src/Exception/RewindException.php new file mode 100644 index 0000000..18f05fb --- /dev/null +++ b/src/Exception/RewindException.php @@ -0,0 +1,15 @@ +getTarget()->isEndOfFile(); } + + /** + * @inheritDoc + */ + public function rewindPosition(): static + { + $this->getTarget()->rewindPosition(); + return $this; + } } \ No newline at end of file diff --git a/src/System/Socket/Traits/RewindSocketPositionTrait.php b/src/System/Socket/Traits/RewindSocketPositionTrait.php new file mode 100644 index 0000000..1317a16 --- /dev/null +++ b/src/System/Socket/Traits/RewindSocketPositionTrait.php @@ -0,0 +1,32 @@ +getSocketResource(); + if (!@rewind($file)) { + $this->throwException("Could not rewind {type} position", RewindException::class); + } + return $this; + } +} \ No newline at end of file diff --git a/test/Unit/System/File/VolatileFileTestTrait.php b/test/Unit/System/File/VolatileFileTestTrait.php index 543d1dd..4847a1f 100644 --- a/test/Unit/System/File/VolatileFileTestTrait.php +++ b/test/Unit/System/File/VolatileFileTestTrait.php @@ -6,6 +6,7 @@ use Aternos\IO\Exception\CreateFileException; use Aternos\IO\Exception\IOException; use Aternos\IO\Exception\ReadException; +use Aternos\IO\Exception\RewindException; use Aternos\IO\Exception\SeekException; use Aternos\IO\Exception\StatException; use Aternos\IO\Exception\TruncateException; @@ -116,6 +117,38 @@ public function testSetPositionThrowsException(): void $file->setPosition(-1); } + /** + * @return void + * @throws CreateDirectoryException + * @throws CreateFileException + * @throws IOException + */ + public function testRewindPosition(): void + { + $file = $this->getVolatileFile(); + $file->write('test'); + $this->assertEquals(4, $file->getPosition()); + $file->rewindPosition(); + $this->assertEquals(0, $file->getPosition()); + } + + /** + * @return void + * @throws CreateDirectoryException + * @throws CreateFileException + * @throws IOException + * @throws ReflectionException + */ + public function testRewindPositionThrowsException(): void + { + $file = $this->getVolatileFile(); + $reflectionFile = new ReflectionObject($file); + $socketResource = $reflectionFile->getProperty('socketResource'); + $socketResource->setValue($file, fopen('php://output', 'w')); + $this->expectException(RewindException::class); + $file->rewindPosition(); + } + /** * @throws CreateDirectoryException * @throws IOException diff --git a/test/Unit/System/Link/FileLinkTest.php b/test/Unit/System/Link/FileLinkTest.php index db86951..3e03e5d 100644 --- a/test/Unit/System/Link/FileLinkTest.php +++ b/test/Unit/System/Link/FileLinkTest.php @@ -269,6 +269,25 @@ public function testSetPosition(): void $this->assertEquals("3", $element->read(1)); } + /** + * @return void + * @throws DeleteException + * @throws GetTargetException + * @throws IOException + * @throws SetTargetException + */ + public function testRewindPosition(): void + { + $targetPath = $this->getTmpPath() . "/test-target"; + file_put_contents($targetPath, "0123456789"); + $element = $this->createElement($this->getTmpPath() . "/test"); + $element->setTarget(new File($targetPath)); + $element->setPosition(3); + $this->assertEquals(3, $element->getPosition()); + $element->rewindPosition(); + $this->assertEquals(0, $element->getPosition()); + } + /** * @throws GetTargetException * @throws SetTargetException