Skip to content

Commit

Permalink
Fixed symlinks not being properly resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Dec 19, 2022
1 parent 3855a0f commit f0f2862
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Fixed symlinks not being properly resolved

## [1.2.0] - 2022-12-01

* Added more checks when decoding JSON Files
Expand Down
21 changes: 7 additions & 14 deletions src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,22 @@ public static function decode(string $json, ?bool $forceArray = null): mixed
*/
public static function decodeFile(string $path, bool $forceArray = null): mixed
{
$fileInfo = new SplFileInfo($path);

if ($fileInfo->isLink() && $linkTarget = $fileInfo->getLinkTarget()) {
$fileInfo = new SplFileInfo($linkTarget);
}

if (!$fileInfo->isFile()) {
throw new UnexpectedValueException("`$path` does not point to a file.");
if (!is_readable($path)) {
throw new UnexpectedValueException("The file at '$path' is not readable");
}

if (!$fileInfo->isReadable()) {
throw new UnexpectedValueException("`$path` is not readable.");
if (is_dir($path)) {
throw new UnexpectedValueException("'$path' points to a directory");
}

$file = $fileInfo->openFile();
$contents = $file->fread($file->getSize());
$contents = file_get_contents($path);

if ($contents === false) {
throw new UnexpectedValueException("Unable to read contents of `$path`");
throw new UnexpectedValueException("The file at '$path' is not readable");
}

if ($contents === '') {
throw new UnexpectedValueException("The file at `$path` is empty");
throw new UnexpectedValueException("The file at '$path' is empty");
}

return self::decode($contents, $forceArray);
Expand Down
7 changes: 7 additions & 0 deletions tests/DecodeJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public function it_resolves_links(): void
} finally {
unlink($symlinkPath);
}
}

/** @test */
public function it_resolves_relative_links(): void
{
$path = __DIR__.'/symlinked/symlinked.json';

$this->assertIsObject(Json::decodeFile($path));
}
}
1 change: 1 addition & 0 deletions tests/symlinked/symlinked.json

0 comments on commit f0f2862

Please sign in to comment.