Changing IniFile to be a ConfigReader for use with Configure. Test case updated.

This commit is contained in:
mark_story 2010-12-04 12:15:47 -05:00
parent 9e32c13cac
commit 1e569f509a
4 changed files with 84 additions and 131 deletions

View file

@ -1,101 +0,0 @@
<?php
/**
* IniFile
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.controller.components
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* 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 {
/**
* Values inside the ini file.
*
* @var array
*/
protected $_values = array();
/**
* Build and construct a new ini file parser, the parser will be a representation of the ini
* 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, $section = null) {
$contents = parse_ini_file($filename, true);
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) {
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]);
}
}

View file

@ -0,0 +1,73 @@
<?php
/**
* IniReader
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.controller.components
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Ini file configuration parser. Since IniReader 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 IniReader implements ConfigReaderInterface {
/**
* The path to read ini files from.
*
* @var array
*/
protected $_path;
/**
* The section to read, if null all sections will be read.
*
* @var string
*/
protected $_section;
/**
* Build and construct a new ini file parser. The parser can be used to read
* ini files that are on the filesystem.
*
* @param string $path Path to load ini config files from.
* @param string $section Only get one section.
*/
public function __construct($path, $section = null) {
$this->_path = $path;
$this->_section = $section;
}
/**
* Read an ini file and return the results as an array.
*
* @param string $file Name of the file to read.
* @return array
*/
public function read($file) {
$filename = $this->_path . $file;
$contents = parse_ini_file($filename, true);
if (!empty($this->_section) && isset($contents[$this->_section])) {
$values = $contents[$this->_section];
} else {
$values = $contents;
}
return $values;
}
}

View file

@ -315,7 +315,8 @@ class Configure {
* @link http://book.cakephp.org/view/929/load
* @param string $key name of configuration resource to load.
* @param string $config Name of the configured reader to use to read the resource identfied by $key.
* @return mixed false if file not found, void if load successful
* @return mixed false if file not found, void if load successful.
* @throws Exception Will throw any exceptions the reader raises.
*/
public static function load($key, $config = 'default') {
if (!isset(self::$_readers[$config])) {

View file

@ -1,6 +1,6 @@
<?php
/**
* IniFileTest
* IniReaderTest
*
* PHP 5
*
@ -17,9 +17,9 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Core', 'config/IniFile');
App::import('Core', 'config/IniReader');
class IniFileTest extends CakeTestCase {
class IniReaderTest extends CakeTestCase {
/**
* The test file that will be read.
@ -35,7 +35,7 @@ class IniFileTest extends CakeTestCase {
*/
function setup() {
parent::setup();
$this->file = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'config'. DS . 'acl.ini.php';
$this->path = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'config'. DS;
}
/**
@ -44,7 +44,8 @@ class IniFileTest extends CakeTestCase {
* @return void
*/
function testConstruct() {
$config = new IniFile($this->file);
$reader = new IniReader($this->path);
$config = $reader->read('acl.ini.php');
$this->assertTrue(isset($config['admin']));
$this->assertTrue(isset($config['paul']['groups']));
@ -57,32 +58,11 @@ class IniFileTest extends CakeTestCase {
* @return void
*/
function testReadingOnlyOneSection() {
$config = new IniFile($this->file, 'admin');
$reader = new IniReader($this->path, 'admin');
$config = $reader->read('acl.ini.php');
$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';
}
}