From 83fcafd064d2d95929cb3af5fb9fe48d571e0958 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Tue, 10 Nov 2015 13:40:07 -0500 Subject: [PATCH] Added clear function to config writer. --- module/VuFind/src/VuFind/Config/Writer.php | 33 +++++++++++++++---- .../src/VuFindTest/Config/WriterTest.php | 15 +++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/module/VuFind/src/VuFind/Config/Writer.php b/module/VuFind/src/VuFind/Config/Writer.php index 769d9bb965f..a27ad406a40 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 ed3798c199e..cad733e216f 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 -- GitLab