diff --git a/src/ConfigResource.php b/src/ConfigResource.php index d4b50d2..876b34e 100644 --- a/src/ConfigResource.php +++ b/src/ConfigResource.php @@ -39,8 +39,6 @@ class ConfigResource /** * @param array $config - * @param string $fileName - * @param ConfigWriter $writer */ public function __construct(array $config, $fileName, ConfigWriter $writer) { @@ -110,25 +108,9 @@ public function patch($data, $tree = false) */ public function patchKey($key, $value) { - // Get local config file - $config = []; - if (file_exists($this->fileName)) { - $config = include $this->fileName; - if (! is_array($config)) { - $config = []; - } - } - $config = $this->replaceKey($key, $value, $config); - - // Write to configuration file - $this->writer->toFile($this->fileName, $config); - $this->invalidateCache($this->fileName); + $this->patch([$key => $value]); - // Reseed configuration - $this->config = $config; - - // Return written values - return $config; + return $this->config; } /** @@ -192,8 +174,10 @@ public function replaceKey($keys, $value, array $config) $key = array_shift($keys); + $haveKeys = (count($keys) > 0) ? true : false; + // If no more keys, overwrite and return - if (! $keys) { + if (! $haveKeys) { $config[$key] = $value; return $config; } @@ -280,7 +264,6 @@ public function traverseArray(array $array, $currentKey = '') * @param array $patchValues * @param string $key * @param mixed $value - * @throws Exception\InvalidArgumentException */ public function createNestedKeyValuePair(&$patchValues, $key, $value) { @@ -305,7 +288,7 @@ public function createNestedKeyValuePair(&$patchValues, $key, $value) protected function extractAndSet(array $keys, $value, &$array) { $key = array_shift($keys); - if ($keys) { + if (count($keys)) { if (! isset($array[$key]) || ! is_array($array[$key])) { $array[$key] = []; } @@ -329,7 +312,7 @@ protected function deleteByKey(&$array, array $keys) return; } - if (! $keys) { + if (1 > count($keys)) { unset($array[$key]); return; } diff --git a/test/ConfigResourceTest.php b/test/ConfigResourceTest.php index a2703ee..94e8bc4 100644 --- a/test/ConfigResourceTest.php +++ b/test/ConfigResourceTest.php @@ -9,6 +9,7 @@ use PHPUnit\Framework\TestCase; use stdClass; use Zend\Config\Writer\PhpArray; +use Zend\Stdlib\ArrayUtils; use ZF\Configuration\ConfigResource; class ConfigResourceTest extends TestCase @@ -86,6 +87,8 @@ public function testPatchListUpdatesFileWithMergedConfig() ], 'baz' => 'not what you think', ]; + $writer = new PhpArray(); + $writer->toFile($this->file, $config); $configResource = new ConfigResource($config, $this->file, $this->writer); $patch = [ @@ -97,8 +100,10 @@ public function testPatchListUpdatesFileWithMergedConfig() $this->assertEquals($patch, $response); $expected = [ + 'foo' => 'bar', 'bar' => [ 'baz' => 'UPDATED', + 'bat' => 'bogus', ], 'baz' => 'what you think', ]; @@ -402,4 +407,127 @@ public function testDeleteNonexistentKeyShouldDoNothing() $test = include $this->file; $this->assertEquals($expected, $test); } + + public function patchKey() + { + return [ + 'scalar-top-level' => [ + 'top', + 'updated', + ['top' => 'updated'] + ], + + 'overwrite-hash' => [ + 'sub', + 'updated', + ['sub' => 'updated'], + ], + + 'nested-scalar' => [ + 'sub.level', + 'updated', + [ + 'sub' => [ + 'level' => 'updated', + ], + ], + ], + 'nested-list' => [ + 'sub.list', + ['three', 'four'], + [ + 'sub' => [ + 'list' => ['three', 'four'], + ], + ], + ], + 'nested-hash' => [ + 'sub.hash.two', + 'updated', + [ + 'sub' => [ + 'hash' => [ + 'two' => 'updated', + ], + ], + ], + ], + 'overwrite-nested-null' => [ + 'sub.null', + 'updated', + [ + 'sub' => [ + 'null' => 'updated', + ], + ], + ], + 'overwrite-nested-object' => [ + 'sub.object', + 'updated', + [ + 'sub' => [ + 'object' => 'updated', + ], + ], + ], + 'merge-nested' => [ + 'sub.hash', + [ + 'two' => 'two-updated', + 'three' => 'three-updated', + ], + [ + 'sub' => [ + 'hash' => [ + 'one' => 1, + 'two' => 'two-updated', + 'three' => 'three-updated', + ], + ], + ], + ], + 'add-new' => [ + 'sub', + ['new' => 'added'], + [ + 'sub' => [ + 'new' => 'added', + ], + ], + ], + ]; + } + + /** + * @dataProvider patchKey + * + * @param string $key + * @param mixed $value + * @param mixed $expected + */ + public function testPatchKey($key, $value, $expected) + { + $config = [ + 'top' => 'level', + 'sub' => [ + 'level' => 2, + 'list' => [ + 'one', + 'two', + ], + 'hash' => [ + 'one' => 1, + 'two' => 2, + ], + 'null' => null, + 'object' => stdClass::class, + ], + ]; + $writer = new PhpArray(); + $writer->toFile($this->file, $config); + + $updated = $this->configResource->patchKey($key, $value); + $expected = ArrayUtils::merge($config, $expected); + $this->assertEquals($expected, $updated); + } }