Skip to content

Commit 40f7a3a

Browse files
committed
[Filesystem] Add the readFile() method
1 parent 6507cf0 commit 40f7a3a

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add the `Filesystem::readFile()` method
8+
49
7.0
510
---
611

Filesystem.php

+19
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,25 @@ public function appendToFile(string $filename, $content, bool $lock = false): vo
694694
}
695695
}
696696

697+
/**
698+
* Returns the content of a file as a string.
699+
*
700+
* @throws IOException If the file cannot be read
701+
*/
702+
public function readFile(string $filename): string
703+
{
704+
if (is_dir($filename)) {
705+
throw new IOException(sprintf('Failed to read file "%s": File is a directory.', $filename));
706+
}
707+
708+
$content = self::box('file_get_contents', $filename);
709+
if (false === $content) {
710+
throw new IOException(sprintf('Failed to read file "%s": ', $filename).self::$lastError, 0, null, $filename);
711+
}
712+
713+
return $content;
714+
}
715+
697716
private function toIterable(string|iterable $files): iterable
698717
{
699718
return is_iterable($files) ? $files : [$files];

Tests/FilesystemTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,43 @@ public function testDumpKeepsExistingPermissionsWhenOverwritingAnExistingFile()
18021802
$this->assertFilePermissions(745, $filename);
18031803
}
18041804

1805+
public function testReadFile()
1806+
{
1807+
$licenseFile = \dirname(__DIR__).'/LICENSE';
1808+
1809+
$this->assertStringEqualsFile($licenseFile, $this->filesystem->readFile($licenseFile));
1810+
}
1811+
1812+
public function testReadNonExistentFile()
1813+
{
1814+
$this->expectException(IOException::class);
1815+
$this->expectExceptionMessageMatches('#^Failed to read file ".+/Tests/invalid"\\: file_get_contents\\(.+/Tests/invalid\\)\\: Failed to open stream\\: No such file or directory$#');
1816+
1817+
$this->filesystem->readFile(__DIR__.'/invalid');
1818+
}
1819+
1820+
public function testReadDirectory()
1821+
{
1822+
$this->expectException(IOException::class);
1823+
$this->expectExceptionMessageMatches('#^Failed to read file ".+/Tests"\\: File is a directory\\.$#');
1824+
1825+
$this->filesystem->readFile(__DIR__);
1826+
}
1827+
1828+
public function testReadUnreadableFile()
1829+
{
1830+
$this->markAsSkippedIfChmodIsMissing();
1831+
1832+
$filename = $this->workspace.'/unreadable.txt';
1833+
file_put_contents($filename, 'Hello World');
1834+
chmod($filename, 0o000);
1835+
1836+
$this->expectException(IOException::class);
1837+
$this->expectExceptionMessageMatches('#^Failed to read file ".+/unreadable.txt"\\: file_get_contents\\(.+/unreadable.txt\\)\\: Failed to open stream\\: Permission denied$#');
1838+
1839+
$this->filesystem->readFile($filename);
1840+
}
1841+
18051842
public function testCopyShouldKeepExecutionPermission()
18061843
{
18071844
$this->markAsSkippedIfChmodIsMissing();

0 commit comments

Comments
 (0)