From 19612fdb6da722a2abf95b471d14e5e775072ba1 Mon Sep 17 00:00:00 2001 From: Lorenzo Losa Date: Wed, 22 Mar 2023 11:54:37 +0000 Subject: [PATCH 1/2] add FilesystemAdapter::json() method to read and decode a json file --- src/Illuminate/Filesystem/FilesystemAdapter.php | 16 ++++++++++++++++ tests/Filesystem/FilesystemAdapterTest.php | 14 ++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index 88ffe14dfdfe..588c31697c04 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -262,6 +262,22 @@ public function get($path) } } + /** + * Get the contents of a file as decoded JSON. + * + * @param string $path + * @return array|null + */ + public function json($path) + { + $content = $this->get($path); + if ($content === null) { + return null; + } + + return json_decode($content, true); + } + /** * Create a streamed response for a given file. * diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index 51d6d669c0a3..d6cc99c64dcd 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -184,6 +184,20 @@ public function testGetFileNotFound() $this->assertNull($filesystemAdapter->get('file.txt')); } + public function testJsonReturnsDecodedJsonData() + { + $this->filesystem->write('file.json', '{"foo": "bar"}'); + $filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter); + $this->assertSame(['foo' => 'bar'], $filesystemAdapter->json('file.json')); + } + + public function testJsonReturnsNullIfJsonDataIsInvalid() + { + $this->filesystem->write('file.json', '{"foo":'); + $filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter); + $this->assertNull($filesystemAdapter->json('file.json')); + } + public function testMimeTypeNotDetected() { $this->filesystem->write('unknown.mime-type', ''); From 3a614d1514e4966c4c96b98231eace63a1c3434e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Mar 2023 17:11:07 -0400 Subject: [PATCH 2/2] formatting --- src/Illuminate/Filesystem/Filesystem.php | 5 +++-- src/Illuminate/Filesystem/FilesystemAdapter.php | 8 +++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Filesystem/Filesystem.php b/src/Illuminate/Filesystem/Filesystem.php index b15ee1c6d633..2219b15d7a17 100644 --- a/src/Illuminate/Filesystem/Filesystem.php +++ b/src/Illuminate/Filesystem/Filesystem.php @@ -63,14 +63,15 @@ public function get($path, $lock = false) * Get the contents of a file as decoded JSON. * * @param string $path + * @param int $flags * @param bool $lock * @return array * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ - public function json($path, $lock = false) + public function json($path, $flags = 0, $lock = false) { - return json_decode($this->get($path, $lock), true); + return json_decode($this->get($path, $lock), true, 512, $flags); } /** diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index 588c31697c04..4a4fa1486b64 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -266,16 +266,14 @@ public function get($path) * Get the contents of a file as decoded JSON. * * @param string $path + * @param int $flags * @return array|null */ - public function json($path) + public function json($path, $flags = 0) { $content = $this->get($path); - if ($content === null) { - return null; - } - return json_decode($content, true); + return is_null($content) ? null : json_decode($content, true, 512, $flags); } /**