From 66531f7e55152238eadec17c6befc13b050fa170 Mon Sep 17 00:00:00 2001 From: Matthew Fonda Date: Wed, 9 Mar 2011 10:13:13 -0800 Subject: [PATCH] * add option to choose if sections should be processed * implement Countable interface * add tests for count and get (without sections) --- Config/Lite.php | 43 +++++++++++++++++++++++++++++++++------ tests/Config_LiteTest.php | 25 ++++++++++++++++++++++- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/Config/Lite.php b/Config/Lite.php index cf70c65..be0da10 100644 --- a/Config/Lite.php +++ b/Config/Lite.php @@ -38,7 +38,7 @@ * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @link https://github.com/pce/config_lite */ -class Config_Lite implements ArrayAccess, IteratorAggregate +class Config_Lite implements ArrayAccess, IteratorAggregate, Countable { /** * sections, holds the config sections @@ -68,6 +68,13 @@ class Config_Lite implements ArrayAccess, IteratorAggregate * @var string */ protected $linebreak = "\n"; + + /** + * parseSections - if true, sections will be processed + * + * @var bool + */ + protected $processSections = true; /** * the read method parses the optional given filename @@ -98,7 +105,7 @@ public function read($filename = null) . $filename ); } - $this->sections = parse_ini_file($filename, true); + $this->sections = parse_ini_file($filename, $this->processSections); if (false === $this->sections) { throw new Config_Lite_Exception_Runtime( 'failure, can not parse the file: ' . $filename @@ -193,9 +200,6 @@ protected function normalizeValue($value) public function write($filename, $sectionsarray) { $content = ''; - if ('.php' === substr($filename, -4)) { - $content .= ';' . $this->linebreak; - } $sections = ''; $globals = ''; if (!empty($sectionsarray)) { @@ -452,7 +456,7 @@ public function getSection($sec, $default = null) */ public function hasSection($sec) { - if (isset($this->sections[$sec])) { + if (isset($this->sections[$sec]) && is_array($this->sections[$sec])) { return true; } return false; @@ -629,6 +633,22 @@ public function setLinebreak($linebreakchars) $this->linebreak = $linebreakchars; return $this; } + + /** + * Sets whether or not sections should be processed + * + * If true, values for each section will be placed into + * a sub-array for the section. If false, all values will + * be placed in the global scope. + * + * @param bool $processSections + * @return $this + */ + public function setProcessSections($processSections) + { + $this->processSections = $processSections; + return $this; + } /** * text presentation of the config object @@ -718,6 +738,17 @@ public function getIterator() { return new ArrayIterator($this->sections); } + + /** + * implemented for interface Countable + * + * @see http://php.net.countable + * @return int + */ + public function count() + { + return count($this->sections); + } /** * takes an optional filename, if the file exists, also reads it. diff --git a/tests/Config_LiteTest.php b/tests/Config_LiteTest.php index d609b4e..a1d1fbe 100755 --- a/tests/Config_LiteTest.php +++ b/tests/Config_LiteTest.php @@ -335,5 +335,28 @@ public function testNormalizeValue() $s = $m->invokeArgs($obj, array('String')); $this->assertEquals('"String"', $s); - } + } + + public function testCountSections() + { + $count = count($this->config); + $this->assertEquals($count, 6); + } + + public function testCountGlobal() + { + $this->config->setProcessSections(false)->read(dirname(__FILE__).'/test.cfg'); + $count = count($this->config); + $this->assertEquals($count, 9); + } + + public function testDoNotProcessSectionsGet() + { + $this->config->setProcessSections(false)->read(dirname(__FILE__).'/test.cfg'); + $counter = $this->config->get(null, 'count'); + $this->assertEquals(2, $counter); + // fallback to default given value 3 + $counter = $this->config->get(null, 'nonexisting_counter_option', 3); + $this->assertEquals(3, $counter); + } }