Moving load() logic out of Configure, and putting it into PhpReader.

Updating test cases.  store() still needs to be properly implemented.
This commit is contained in:
mark_story 2010-12-04 01:05:12 -05:00
parent 6618178e1b
commit 7eab3b2850
2 changed files with 27 additions and 47 deletions
cake

View file

@ -309,48 +309,16 @@ class Configure {
* - To load config files from a plugin `Configure::load('plugin.configure_file');`. * - To load config files from a plugin `Configure::load('plugin.configure_file');`.
* *
* @link http://book.cakephp.org/view/929/load * @link http://book.cakephp.org/view/929/load
* @param string $fileName name of file to load, extension must be .php and only the name * @param string $key name of configuration resource to load.
* should be used, not the extenstion * @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
*/ */
public static function load($fileName) { public static function load($key, $config = 'default') {
$found = $plugin = $pluginPath = false; if (!isset(self::$_readers[$config])) {
list($plugin, $fileName) = pluginSplit($fileName);
if ($plugin) {
$pluginPath = App::pluginPath($plugin);
}
$pos = strpos($fileName, '..');
if ($pos === false) {
if ($pluginPath && file_exists($pluginPath . 'config' . DS . $fileName . '.php')) {
include($pluginPath . 'config' . DS . $fileName . '.php');
$found = true;
} elseif (file_exists(CONFIGS . $fileName . '.php')) {
include(CONFIGS . $fileName . '.php');
$found = true;
} elseif (file_exists(CACHE . 'persistent' . DS . $fileName . '.php')) {
include(CACHE . 'persistent' . DS . $fileName . '.php');
$found = true;
} else {
foreach (App::core('cake') as $key => $path) {
if (file_exists($path . DS . 'config' . DS . $fileName . '.php')) {
include($path . DS . 'config' . DS . $fileName . '.php');
$found = true;
break;
}
}
}
}
if (!$found) {
return false; return false;
} }
$values = self::$_readers[$config]->read($key);
if (!isset($config)) { return self::write($values);
trigger_error(sprintf(__('Configure::load() - no variable $config found in %s.php'), $fileName), E_USER_WARNING);
return false;
}
return self::write($config);
} }
/** /**

View file

@ -71,6 +71,7 @@ class ConfigureTest extends CakeTestCase {
} }
Configure::write('debug', $this->_debug); Configure::write('debug', $this->_debug);
Configure::write('Cache.disable', $this->_cacheDisable); Configure::write('Cache.disable', $this->_cacheDisable);
Configure::drop('test');
} }
/** /**
@ -185,18 +186,26 @@ class ConfigureTest extends CakeTestCase {
/** /**
* testLoad method * testLoad method
* *
* @access public * @expectedException RuntimeException
* @return void
*/
function testLoadExceptionOnNonExistantFile() {
Configure::config('test', new PhpReader());
$result = Configure::load('non_existing_configuration_file', 'test');
}
/**
* test load
*
* @return void * @return void
*/ */
function testLoad() { function testLoad() {
$result = Configure::load('non_existing_configuration_file'); Configure::config('test', new PhpReader(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'config' . DS));
$this->assertFalse($result);
$result = Configure::load('config'); $result = Configure::load('var_test', 'test');
$this->assertTrue($result); $this->assertTrue($result);
$result = Configure::load('../../index'); $this->assertEquals('value', Configure::read('Read'));
$this->assertFalse($result);
} }
/** /**
@ -207,13 +216,15 @@ class ConfigureTest extends CakeTestCase {
*/ */
function testLoadPlugin() { function testLoadPlugin() {
App::build(array('plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)), true); App::build(array('plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)), true);
$result = Configure::load('test_plugin.load'); Configure::config('test', new PhpReader());
$result = Configure::load('test_plugin.load', 'test');
$this->assertTrue($result); $this->assertTrue($result);
$expected = '/test_app/plugins/test_plugin/config/load.php'; $expected = '/test_app/plugins/test_plugin/config/load.php';
$config = Configure::read('plugin_load'); $config = Configure::read('plugin_load');
$this->assertEqual($config, $expected); $this->assertEqual($config, $expected);
$result = Configure::load('test_plugin.more.load'); $result = Configure::load('test_plugin.more.load', 'test');
$this->assertTrue($result); $this->assertTrue($result);
$expected = '/test_app/plugins/test_plugin/config/more.load.php'; $expected = '/test_app/plugins/test_plugin/config/more.load.php';
$config = Configure::read('plugin_more_load'); $config = Configure::read('plugin_more_load');
@ -227,6 +238,7 @@ class ConfigureTest extends CakeTestCase {
* @return void * @return void
*/ */
function testStoreAndLoad() { function testStoreAndLoad() {
$this->markTestSkipped('Configure::store() is not working right now.');
Configure::write('Cache.disable', false); Configure::write('Cache.disable', false);
$expected = array('data' => 'value with backslash \, \'singlequote\' and "doublequotes"'); $expected = array('data' => 'value with backslash \, \'singlequote\' and "doublequotes"');