Skip to content

add rewind position feature #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Exception/RewindException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Aternos\IO\Exception;

/**
* Class RewindException
*
* Thrown when a rewind operation fails
*
* @package Aternos\IO\Exception
*/
class RewindException extends IOException
{

}
23 changes: 23 additions & 0 deletions src/Interfaces/Features/RewindPositionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Aternos\IO\Interfaces\Features;

use Aternos\IO\Exception\IOException;

/**
* Interface RewindPositionInterface
*
* Allows rewinding the seek position of an element to the beginning
*
* @package Aternos\IO\Interfaces\Features
*/
interface RewindPositionInterface extends GetPositionInterface
{
/**
* Rewind seek position of an element to the beginning
*
* @throws IOException
* @return $this
*/
public function rewindPosition(): static;
}
2 changes: 1 addition & 1 deletion src/Interfaces/Features/SetPositionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @package Aternos\IO\Interfaces\Features
*/
interface SetPositionInterface extends GetPositionInterface
interface SetPositionInterface extends RewindPositionInterface
{
/**
* Set the seek position of an element
Expand Down
2 changes: 2 additions & 0 deletions src/System/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Aternos\IO\System\Socket\Traits\IsEndOfFileSocketTrait;
use Aternos\IO\System\Socket\Traits\OpenSocketTrait;
use Aternos\IO\System\Socket\Traits\ReadSocketTrait;
use Aternos\IO\System\Socket\Traits\RewindSocketPositionTrait;
use Aternos\IO\System\Socket\Traits\SetSocketPositionTrait;
use Aternos\IO\System\Socket\Traits\TruncateSocketTrait;
use Aternos\IO\System\Socket\Traits\WriteSocketTrait;
Expand All @@ -37,6 +38,7 @@ class File extends FilesystemElement implements FileInterface
IsEndOfFileSocketTrait,
ReadSocketTrait,
SetSocketPositionTrait,
RewindSocketPositionTrait,
TruncateSocketTrait,
WriteSocketTrait {
write as traitWrite;
Expand Down
2 changes: 2 additions & 0 deletions src/System/File/TempMemoryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Aternos\IO\System\Socket\Traits\IsEndOfFileSocketTrait;
use Aternos\IO\System\Socket\Traits\OpenSocketTrait;
use Aternos\IO\System\Socket\Traits\ReadSocketTrait;
use Aternos\IO\System\Socket\Traits\RewindSocketPositionTrait;
use Aternos\IO\System\Socket\Traits\SetSocketPositionTrait;
use Aternos\IO\System\Socket\Traits\TruncateSocketTrait;
use Aternos\IO\System\Socket\Traits\WriteSocketTrait;
Expand All @@ -32,6 +33,7 @@ class TempMemoryFile implements VolatileFileInterface
IsEndOfFileSocketTrait,
ReadSocketTrait,
SetSocketPositionTrait,
RewindSocketPositionTrait,
TruncateSocketTrait,
WriteSocketTrait,
GetSizeTrait;
Expand Down
9 changes: 9 additions & 0 deletions src/System/Link/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,13 @@ public function isEndOfFile(): bool
{
return $this->getTarget()->isEndOfFile();
}

/**
* @inheritDoc
*/
public function rewindPosition(): static
{
$this->getTarget()->rewindPosition();
return $this;
}
}
32 changes: 32 additions & 0 deletions src/System/Socket/Traits/RewindSocketPositionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Aternos\IO\System\Socket\Traits;

use Aternos\IO\Exception\IOException;
use Aternos\IO\Exception\RewindException;
use Aternos\IO\Exception\SeekException;

/**
* Trait RewindSocketPositionTrait
*
* Trait for socket based elements implementing {@link RewindPositionInterface}
*
* @package Aternos\IO\System\Socket\Traits
*/
trait RewindSocketPositionTrait
{
use SocketTrait;

/**
* @inheritDoc
* @throws SeekException|IOException
*/
public function rewindPosition(): static
{
$file = $this->getSocketResource();
if (!@rewind($file)) {
$this->throwException("Could not rewind {type} position", RewindException::class);
}
return $this;
}
}
33 changes: 33 additions & 0 deletions test/Unit/System/File/VolatileFileTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/Unit/System/Link/FileLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down