Skip to content

Commit

Permalink
* add option to choose if sections should be processed
Browse files Browse the repository at this point in the history
* implement Countable interface
* add tests for count and get (without sections)
  • Loading branch information
mfonda committed Mar 9, 2011
1 parent ca80baa commit 66531f7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
43 changes: 37 additions & 6 deletions Config/Lite.php
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -193,9 +200,6 @@ protected function normalizeValue($value)
public function write($filename, $sectionsarray)
{
$content = '';
if ('.php' === substr($filename, -4)) {
$content .= ';<?php return; ?>' . $this->linebreak;
}
$sections = '';
$globals = '';
if (!empty($sectionsarray)) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
25 changes: 24 additions & 1 deletion tests/Config_LiteTest.php
Expand Up @@ -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);
}
}

0 comments on commit 66531f7

Please sign in to comment.