Skip to content

Commit

Permalink
Ini/Php readers now read files with/without extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Jan 23, 2011
1 parent 55c557d commit be98491
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cake/libs/config/ini_reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public function __construct($path, $section = null) {
*/
public function read($file) {
$filename = $this->_path . $file;
if (!file_exists($filename)) {
$filename .= '.ini';
if (!file_exists($filename)) {
throw new ConfigureException(__('Could not load configuration files: %s or %s', substr($filename, 0, -4), $filename));
}
}
$contents = parse_ini_file($filename, true);
if (!empty($this->_section) && isset($contents[$this->_section])) {
$values = $this->_parseNestedValues($contents[$this->_section]);
Expand Down
12 changes: 9 additions & 3 deletions cake/libs/config/php_reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,21 @@ public function read($key) {
if (strpos($key, '..') !== false) {
throw new ConfigureException(__('Cannot load configuration files with ../ in them.'));
}
if (substr($key, -4) === '.php') {
$key = substr($key, 0, -4);
}
list($plugin, $key) = pluginSplit($key);

if ($plugin) {
$file = App::pluginPath($plugin) . 'config' . DS . $key . '.php';
$file = App::pluginPath($plugin) . 'config' . DS . $key;
} else {
$file = $this->_path . $key . '.php';
$file = $this->_path . $key;
}
if (!file_exists($file)) {
throw new ConfigureException(__('Could not load configuration file: ') . $file);
$file .= '.php';
if (!file_exists($file)) {
throw new ConfigureException(__('Could not load configuration files: %s or %s', substr($file, 0, -4), $file));
}
}
include $file;
if (!isset($config)) {
Expand Down
11 changes: 11 additions & 0 deletions cake/tests/cases/libs/config/ini_reader.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,15 @@ function testBooleanReading() {

$this->assertFalse($config['bools']['test_null']);
}

/**
* test read file without extension
*
* @return void
*/
public function testReadingWithoutExtension() {
$reader = new IniReader($this->path);
$config = $reader->read('nested');
$this->assertTrue($config['bools']['test_on']);
}
}
5 changes: 5 additions & 0 deletions cake/tests/cases/libs/config/php_reader.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ function testRead() {
$values = $reader->read('var_test');
$this->assertEquals('value', $values['Read']);
$this->assertEquals('buried', $values['Deep']['Deeper']['Deepest']);

$values = $reader->read('var_test.php');
$this->assertEquals('value', $values['Read']);
}

/**
Expand Down Expand Up @@ -84,7 +87,9 @@ function testReadPluginValue() {
), true);
$reader = new PhpReader($this->path);
$result = $reader->read('TestPlugin.load');
$this->assertTrue(isset($result['plugin_load']));

$result = $reader->read('TestPlugin.load.php');
$this->assertTrue(isset($result['plugin_load']));
}
}

0 comments on commit be98491

Please sign in to comment.