Skip to content

Commit 39162eb

Browse files
Add symlink functionality
1 parent 36f773b commit 39162eb

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/Storage/File.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,27 @@ private function checkDir(): void
119119
}
120120
}
121121
}
122+
123+
/**
124+
* Determines if the given path is a symbolic link
125+
*
126+
* @return bool True if the path is a symbolic link, false otherwise.
127+
*/
128+
public function isLink(): bool
129+
{
130+
return is_link($this->path);
131+
}
132+
133+
/**
134+
* Resolves and returns the target of a symbolic link
135+
*
136+
* @return string The resolved target of the symbolic link.
137+
*/
138+
public function linkTarget(): string
139+
{
140+
if (!$this->isLink()) {
141+
throw new RuntimeException('Not a symbolic link: ' . $this->path);
142+
}
143+
return (string)readlink($this->path);
144+
}
122145
}

tests/unit/Storage/FileTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Exception;
1515
use org\bovigo\vfs\vfsStream;
1616
use PHPUnit\Framework\TestCase;
17+
use RuntimeException;
1718

1819
class FileTest extends TestCase
1920
{
@@ -63,6 +64,36 @@ public function testWrite(): void
6364
$this->assertTrue(unlink($path));
6465
}
6566

67+
/**
68+
* Tests File::isLink
69+
*/
70+
public function testIsLink(): void
71+
{
72+
$tmpDir = sys_get_temp_dir();
73+
$link = $tmpDir . '/fooLink';
74+
$target = tempnam($tmpDir, 'bar');
75+
76+
symlink($target, $link);
77+
78+
$file = new File($link);
79+
$this->assertTrue($file->isLink());
80+
$this->assertEquals($target, $file->linkTarget());
81+
82+
$this->assertTrue(unlink($link));
83+
$this->assertTrue(unlink($target));
84+
}
85+
86+
/**
87+
* Tests File::linkTarget
88+
*/
89+
public function testLinkTarget(): void
90+
{
91+
$this->expectException(RuntimeException::class);
92+
93+
$file = new File(__FILE__);
94+
$file->linkTarget();
95+
}
96+
6697
/**
6798
* Tests File::write
6899
*/

0 commit comments

Comments
 (0)