diff --git a/module/VuFind/src/VuFind/Config/Writer.php b/module/VuFind/src/VuFind/Config/Writer.php index 769d9bb965f82e2f659840030aa779f0761cd565..a27ad406a403b4a68b2aad5d368a67cac1900edd 100644 --- a/module/VuFind/src/VuFind/Config/Writer.php +++ b/module/VuFind/src/VuFind/Config/Writer.php @@ -84,7 +84,7 @@ class Writer * * @param string $section Section to change/add * @param string $setting Setting within section to change/add - * @param string $value Value to set + * @param string $value Value to set (or null to unset) * * @return void */ @@ -109,20 +109,26 @@ class Writer if (preg_match('/^\[(.+)\]$/', trim($content), $matches)) { // If we just left the target section and didn't find the // desired setting, we should write it to the end. - if ($currentSection == $section && !$settingSet) { + if ($currentSection == $section && !$settingSet + && $value !== null + ) { $line = $setting . ' = "' . $value . '"' . "\n\n" . $line; $settingSet = true; } $currentSection = $matches[1]; } else if (strstr($content, '=')) { $contentParts = explode('=', $content, 2); - $key = reset($contentParts); - if ($currentSection == $section && trim($key) == $setting) { - $line = $setting . ' = "' . $value . '"'; + $key = trim($contentParts[0]); + if ($currentSection == $section && $key == $setting) { + $settingSet = true; + if ($value === null) { + continue; + } else { + $line = $setting . ' = "' . $value . '"'; + } if (!empty($comment)) { $line .= ' ;' . $comment; } - $settingSet = true; } } @@ -131,7 +137,7 @@ class Writer } // Did we loop through everything without finding a place to put the setting? - if (!$settingSet) { + if (!$settingSet && $value !== null) { // We never found the target section? if ($currentSection != $section) { $this->content .= '[' . $section . "]\n"; @@ -140,6 +146,19 @@ class Writer } } + /** + * Remove a setting (convenience wrapper around set to null). + * + * @param string $section Section to change/add + * @param string $setting Setting within section to change/add + * + * @return void + */ + public function clear($section, $setting) + { + $this->set($section, $setting, null); + } + /** * Get the modified file's contents as a string. * diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Config/WriterTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Config/WriterTest.php index ed3798c199e6c2d57f0b2dae74dac26ff2fef5f3..cad733e216fbf5caf2d9241fd4c91956883dc45a 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/Config/WriterTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Config/WriterTest.php @@ -224,4 +224,19 @@ class WriterTest extends \VuFindTest\Unit\TestCase $expected = "[general]\nfoo = \"bar\"\nfoofoofoofoofoofo = \"baz\"\n"; $this->assertEquals($expected, $test->getContent()); } + + /** + * Test clearing values. + * + * @return void + */ + public function testClear() + { + $cfg = "[a]\nb[]=1\nb[]=2\n[b]\nc=3\n"; + $test = new Writer('fake.ini', $cfg); + $test->clear('a', 'b[]'); // clear array + $test->clear('b', 'c'); // clear single value + $test->clear('z', 'z'); // clear value that does not exist + $this->assertEquals("[a]\n[b]", trim($test->getContent())); + } } \ No newline at end of file