From f9a4ea1e67217c9e130c0e77e651887c5c26bed7 Mon Sep 17 00:00:00 2001 From: Basil Suter Date: Sat, 30 Nov 2024 07:08:24 +0000 Subject: [PATCH 1/2] feat: Enhance ComposerReader to accept string or array input and update read/write logic --- src/ComposerReader.php | 24 ++++++++++++++++++++++-- tests/ComposerReaderTest.php | 12 ++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/ComposerReader.php b/src/ComposerReader.php index c71b1cf..92ef1b5 100755 --- a/src/ComposerReader.php +++ b/src/ComposerReader.php @@ -18,10 +18,15 @@ class ComposerReader implements ComposerReaderInterface * * @param string $file The path to the composer.json file. */ - public function __construct(public $file) + public function __construct(public string|array $file) { } + protected function isProvidedAsJsonArray(): bool + { + return is_array($this->file); + } + /** * Whether current composer.json file is readable or not. * @@ -29,6 +34,10 @@ public function __construct(public $file) */ public function canRead(): bool { + if ($this->isProvidedAsJsonArray()) { + return true; + } + return is_file($this->file) && is_readable($this->file); } @@ -39,6 +48,9 @@ public function canRead(): bool */ public function canWrite(): bool { + if ($this->isProvidedAsJsonArray()) { + return false; + } return is_file($this->file) && is_writable($this->file); } @@ -60,8 +72,12 @@ public function canReadAndWrite() * @throws Exception * @return array The composer.json file as array. */ - public function getContent() + public function getContent(): array { + if ($this->isProvidedAsJsonArray()) { + return $this->file; + } + if ($this->_content === null) { if (!$this->canRead()) { throw new Exception(sprintf("Unable to read config file '%s'.", $this->file)); @@ -83,6 +99,10 @@ public function getContent() */ public function writeContent(array $content) { + if ($this->isProvidedAsJsonArray()) { + throw new Exception("Unable to write content as file is provided as array."); + } + if (!$this->canWrite()) { throw new Exception(sprintf("Unable to write config file '%s'.", $this->file)); } diff --git a/tests/ComposerReaderTest.php b/tests/ComposerReaderTest.php index 2f42c7b..113f9c7 100755 --- a/tests/ComposerReaderTest.php +++ b/tests/ComposerReaderTest.php @@ -129,4 +129,16 @@ public function testWriteContentException() $this->expectException('\Exception'); $reader->writeContent(['foo' => 'bar']); } + + public function testUseArrayInputAsContent() + { + $json = $this->getValidJson(); + $array = json_decode(file_get_contents($json), true); + + $reader = new ComposerReader($array); + $this->assertTrue($reader->canRead()); + $this->assertFalse($reader->canWrite()); + $this->assertFalse($reader->canReadAndWrite()); + $this->assertArrayHasKey('name', $reader->getContent()); + } } From 4708ac9893bfd39b95add4730199fe13c6c8d367 Mon Sep 17 00:00:00 2001 From: nadar Date: Sat, 30 Nov 2024 07:09:08 +0000 Subject: [PATCH 2/2] Apply rector changes --- src/ComposerReader.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ComposerReader.php b/src/ComposerReader.php index 92ef1b5..9511bcb 100755 --- a/src/ComposerReader.php +++ b/src/ComposerReader.php @@ -51,6 +51,7 @@ public function canWrite(): bool if ($this->isProvidedAsJsonArray()) { return false; } + return is_file($this->file) && is_writable($this->file); }