Skip to content

Commit 1884a98

Browse files
authored
Merge pull request #2 from aternosorg/rewind-position
add rewind position feature
2 parents 4eac176 + e5523bc commit 1884a98

File tree

9 files changed

+136
-1
lines changed

9 files changed

+136
-1
lines changed

src/Exception/RewindException.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Aternos\IO\Exception;
4+
5+
/**
6+
* Class RewindException
7+
*
8+
* Thrown when a rewind operation fails
9+
*
10+
* @package Aternos\IO\Exception
11+
*/
12+
class RewindException extends IOException
13+
{
14+
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Aternos\IO\Interfaces\Features;
4+
5+
use Aternos\IO\Exception\IOException;
6+
7+
/**
8+
* Interface RewindPositionInterface
9+
*
10+
* Allows rewinding the seek position of an element to the beginning
11+
*
12+
* @package Aternos\IO\Interfaces\Features
13+
*/
14+
interface RewindPositionInterface extends GetPositionInterface
15+
{
16+
/**
17+
* Rewind seek position of an element to the beginning
18+
*
19+
* @throws IOException
20+
* @return $this
21+
*/
22+
public function rewindPosition(): static;
23+
}

src/Interfaces/Features/SetPositionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @package Aternos\IO\Interfaces\Features
1313
*/
14-
interface SetPositionInterface extends GetPositionInterface
14+
interface SetPositionInterface extends RewindPositionInterface
1515
{
1616
/**
1717
* Set the seek position of an element

src/System/File/File.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Aternos\IO\System\Socket\Traits\IsEndOfFileSocketTrait;
1919
use Aternos\IO\System\Socket\Traits\OpenSocketTrait;
2020
use Aternos\IO\System\Socket\Traits\ReadSocketTrait;
21+
use Aternos\IO\System\Socket\Traits\RewindSocketPositionTrait;
2122
use Aternos\IO\System\Socket\Traits\SetSocketPositionTrait;
2223
use Aternos\IO\System\Socket\Traits\TruncateSocketTrait;
2324
use Aternos\IO\System\Socket\Traits\WriteSocketTrait;
@@ -37,6 +38,7 @@ class File extends FilesystemElement implements FileInterface
3738
IsEndOfFileSocketTrait,
3839
ReadSocketTrait,
3940
SetSocketPositionTrait,
41+
RewindSocketPositionTrait,
4042
TruncateSocketTrait,
4143
WriteSocketTrait {
4244
write as traitWrite;

src/System/File/TempMemoryFile.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Aternos\IO\System\Socket\Traits\IsEndOfFileSocketTrait;
1111
use Aternos\IO\System\Socket\Traits\OpenSocketTrait;
1212
use Aternos\IO\System\Socket\Traits\ReadSocketTrait;
13+
use Aternos\IO\System\Socket\Traits\RewindSocketPositionTrait;
1314
use Aternos\IO\System\Socket\Traits\SetSocketPositionTrait;
1415
use Aternos\IO\System\Socket\Traits\TruncateSocketTrait;
1516
use Aternos\IO\System\Socket\Traits\WriteSocketTrait;
@@ -32,6 +33,7 @@ class TempMemoryFile implements VolatileFileInterface
3233
IsEndOfFileSocketTrait,
3334
ReadSocketTrait,
3435
SetSocketPositionTrait,
36+
RewindSocketPositionTrait,
3537
TruncateSocketTrait,
3638
WriteSocketTrait,
3739
GetSizeTrait;

src/System/Link/FileLink.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,13 @@ public function isEndOfFile(): bool
140140
{
141141
return $this->getTarget()->isEndOfFile();
142142
}
143+
144+
/**
145+
* @inheritDoc
146+
*/
147+
public function rewindPosition(): static
148+
{
149+
$this->getTarget()->rewindPosition();
150+
return $this;
151+
}
143152
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Aternos\IO\System\Socket\Traits;
4+
5+
use Aternos\IO\Exception\IOException;
6+
use Aternos\IO\Exception\RewindException;
7+
use Aternos\IO\Exception\SeekException;
8+
9+
/**
10+
* Trait RewindSocketPositionTrait
11+
*
12+
* Trait for socket based elements implementing {@link RewindPositionInterface}
13+
*
14+
* @package Aternos\IO\System\Socket\Traits
15+
*/
16+
trait RewindSocketPositionTrait
17+
{
18+
use SocketTrait;
19+
20+
/**
21+
* @inheritDoc
22+
* @throws SeekException|IOException
23+
*/
24+
public function rewindPosition(): static
25+
{
26+
$file = $this->getSocketResource();
27+
if (!@rewind($file)) {
28+
$this->throwException("Could not rewind {type} position", RewindException::class);
29+
}
30+
return $this;
31+
}
32+
}

test/Unit/System/File/VolatileFileTestTrait.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Aternos\IO\Exception\CreateFileException;
77
use Aternos\IO\Exception\IOException;
88
use Aternos\IO\Exception\ReadException;
9+
use Aternos\IO\Exception\RewindException;
910
use Aternos\IO\Exception\SeekException;
1011
use Aternos\IO\Exception\StatException;
1112
use Aternos\IO\Exception\TruncateException;
@@ -116,6 +117,38 @@ public function testSetPositionThrowsException(): void
116117
$file->setPosition(-1);
117118
}
118119

120+
/**
121+
* @return void
122+
* @throws CreateDirectoryException
123+
* @throws CreateFileException
124+
* @throws IOException
125+
*/
126+
public function testRewindPosition(): void
127+
{
128+
$file = $this->getVolatileFile();
129+
$file->write('test');
130+
$this->assertEquals(4, $file->getPosition());
131+
$file->rewindPosition();
132+
$this->assertEquals(0, $file->getPosition());
133+
}
134+
135+
/**
136+
* @return void
137+
* @throws CreateDirectoryException
138+
* @throws CreateFileException
139+
* @throws IOException
140+
* @throws ReflectionException
141+
*/
142+
public function testRewindPositionThrowsException(): void
143+
{
144+
$file = $this->getVolatileFile();
145+
$reflectionFile = new ReflectionObject($file);
146+
$socketResource = $reflectionFile->getProperty('socketResource');
147+
$socketResource->setValue($file, fopen('php://output', 'w'));
148+
$this->expectException(RewindException::class);
149+
$file->rewindPosition();
150+
}
151+
119152
/**
120153
* @throws CreateDirectoryException
121154
* @throws IOException

test/Unit/System/Link/FileLinkTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,25 @@ public function testSetPosition(): void
269269
$this->assertEquals("3", $element->read(1));
270270
}
271271

272+
/**
273+
* @return void
274+
* @throws DeleteException
275+
* @throws GetTargetException
276+
* @throws IOException
277+
* @throws SetTargetException
278+
*/
279+
public function testRewindPosition(): void
280+
{
281+
$targetPath = $this->getTmpPath() . "/test-target";
282+
file_put_contents($targetPath, "0123456789");
283+
$element = $this->createElement($this->getTmpPath() . "/test");
284+
$element->setTarget(new File($targetPath));
285+
$element->setPosition(3);
286+
$this->assertEquals(3, $element->getPosition());
287+
$element->rewindPosition();
288+
$this->assertEquals(0, $element->getPosition());
289+
}
290+
272291
/**
273292
* @throws GetTargetException
274293
* @throws SetTargetException

0 commit comments

Comments
 (0)