From 1e569f509aba65f240f981f84d3556d25b647dd8 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 4 Dec 2010 12:15:47 -0500 Subject: [PATCH] Changing IniFile to be a ConfigReader for use with Configure. Test case updated. --- cake/libs/config/ini_file.php | 101 ------------------ cake/libs/config/ini_reader.php | 73 +++++++++++++ cake/libs/configure.php | 5 +- ...{ini_file.test.php => ini_reader.test.php} | 36 ++----- 4 files changed, 84 insertions(+), 131 deletions(-) delete mode 100644 cake/libs/config/ini_file.php create mode 100644 cake/libs/config/ini_reader.php rename cake/tests/cases/libs/config/{ini_file.test.php => ini_reader.test.php} (61%) diff --git a/cake/libs/config/ini_file.php b/cake/libs/config/ini_file.php deleted file mode 100644 index 722dbcb08..000000000 --- a/cake/libs/config/ini_file.php +++ /dev/null @@ -1,101 +0,0 @@ -_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]); - } -} \ No newline at end of file diff --git a/cake/libs/config/ini_reader.php b/cake/libs/config/ini_reader.php new file mode 100644 index 000000000..7163a47b5 --- /dev/null +++ b/cake/libs/config/ini_reader.php @@ -0,0 +1,73 @@ +_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; + } +} \ No newline at end of file diff --git a/cake/libs/configure.php b/cake/libs/configure.php index dd4fded79..f4623a7b3 100644 --- a/cake/libs/configure.php +++ b/cake/libs/configure.php @@ -313,9 +313,10 @@ class Configure { * `Configure::load('setup', 'default');` * * @link http://book.cakephp.org/view/929/load - * @param string $key name of configuration resource to 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])) { diff --git a/cake/tests/cases/libs/config/ini_file.test.php b/cake/tests/cases/libs/config/ini_reader.test.php similarity index 61% rename from cake/tests/cases/libs/config/ini_file.test.php rename to cake/tests/cases/libs/config/ini_reader.test.php index 566c1113b..c3da7edf0 100644 --- a/cake/tests/cases/libs/config/ini_file.test.php +++ b/cake/tests/cases/libs/config/ini_reader.test.php @@ -1,6 +1,6 @@ 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'; - } } \ No newline at end of file