Skip to content
Permalink
Browse files

Fleshing out IniFile a bit more.

  • Loading branch information...
markstory committed Nov 29, 2010
1 parent 071cd9b commit 35611d50c35896b4ff4880e14eedf6370450a94e
Showing with 79 additions and 7 deletions.
  1. +44 −7 cake/libs/config/ini_file.php
  2. +35 −0 cake/tests/cases/libs/config/ini_file.test.php
@@ -19,9 +19,12 @@
*/
/**
* Ini file configuration parser.
* Ini file configuration parser. Since IniFile uses parse_ini_file underneath,
* you should be aware that this class shares the same behavior, especially with
* regards to boolean and null values.
*
* @package cake.config
* @see http://php.net/parse_ini_file
*/
class IniFile implements ArrayAccess {
@@ -37,27 +40,61 @@ class IniFile implements ArrayAccess {
* file as an object.
*
* @param string $filename Full path to the file to parse.
* @param string $section Only get one section.
*/
public function __construct($filename) {
public function __construct($filename, $section = null) {
$contents = parse_ini_file($filename, true);
$this->_values = $contents;
if (!empty($section) && isset($contents[$section])) {
$this->_values = $contents[$section];
} else {
$this->_values = $contents;
}
}
/**
* Get the contents of the ini file as a plain array.
*
* @return array
*/
public function asArray() {
return $this->_values;
}
/**
* Part of ArrayAccess implementation.
*
* @param string $name
*/
public function offsetExists($name) {
return isset($this->_values[$name]);
}
/**
* Part of ArrayAccess implementation.
*
* @param string $name
*/
public function offsetGet($name) {
if (!isset($this->_values[$name])) {
return null;
}
return $this->_values[$name];
}
/**
* Part of ArrayAccess implementation.
*
* @param string $name
*/
public function offsetSet($name, $value) {
$this->_values[$name] = $value;
throw new LogicException('You cannot modify an IniFile parse result.');
}
/**
* Part of ArrayAccess implementation.
*
* @param string $name
*/
public function offsetUnset($name) {
unset($this->_values[$name]);
}
@@ -33,4 +33,39 @@ function testConstruct() {
$this->assertTrue(isset($config['paul']['groups']));
$this->assertEquals('ads', $config['admin']['deny']);
}
/**
* no other sections should exist.
*
* @return void
*/
function testReadingOnlyOneSection() {
$config = new IniFile($this->file, 'admin');
$this->assertTrue(isset($config['groups']));
$this->assertEquals('administrators', $config['groups']);
}
/**
* test getting all the values as an array
*
* @return void
*/
function testAsArray() {
$config = new IniFile($this->file);
$content = $config->asArray();
$this->assertTrue(isset($content['admin']['groups']));
$this->assertTrue(isset($content['paul']['groups']));
}
/**
* test that values cannot be modified
*
* @expectedException LogicException
*/
function testNoModification() {
$config = new IniFile($this->file);
$config['admin'] = 'something';
}
}

0 comments on commit 35611d5

Please sign in to comment.
You can’t perform that action at this time.