Skip to content
Snippets Groups Projects
Commit 83fcafd0 authored by Demian Katz's avatar Demian Katz
Browse files

Added clear function to config writer.

parent 2810ddc4
Branches
Tags
No related merge requests found
......@@ -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.
*
......
......@@ -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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment