Skip to content

Commit

Permalink
Make load() and dump() more consistent with each other, create new Ph…
Browse files Browse the repository at this point in the history
…pReader for default if not configured yet.

Fixes #2998
  • Loading branch information
ceeram committed Jul 3, 2012
1 parent 86a74e3 commit 19ba092
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions lib/Cake/Core/Configure.php
Expand Up @@ -261,15 +261,8 @@ public static function drop($name) {
* @throws ConfigureException Will throw any exceptions the reader raises.
*/
public static function load($key, $config = 'default', $merge = true) {
if (!isset(self::$_readers[$config])) {
if ($config === 'default') {
App::uses('PhpReader', 'Configure');
self::$_readers[$config] = new PhpReader();
} else {
return false;
}
}
$values = self::$_readers[$config]->read($key);
$reader = self::_getReader($config);
$values = $reader->read($key);

if ($merge) {
$keys = array_keys($values);
Expand All @@ -286,7 +279,7 @@ public static function load($key, $config = 'default', $merge = true) {
/**
* Dump data currently in Configure into $filename. The serialization format
* is decided by the config reader attached as $config. For example, if the
* 'default' adapter is a PhpReader, the generated file will be a PHP
* 'default' adapter is a PhpReader, the generated file will be a PHP
* configuration file loadable by the PhpReader.
*
* ## Usage
Expand All @@ -303,23 +296,40 @@ public static function load($key, $config = 'default', $merge = true) {
* @param string $key The identifier to create in the config adapter.
* This could be a filename or a cache key depending on the adapter being used.
* @param string $config The name of the configured adapter to dump data with.
* @param array $keys The name of the top-level keys you want to dump.
* @param array $keys The name of the top-level keys you want to dump.
* This allows you save only some data stored in Configure.
* @return boolean success
* @throws ConfigureException if the adapter does not implement a `dump` method.
*/
public static function dump($key, $config = 'default', $keys = array()) {
if (empty(self::$_readers[$config])) {
throw new ConfigureException(__d('cake', 'There is no "%s" adapter.', $config));
}
if (!method_exists(self::$_readers[$config], 'dump')) {
$reader = self::_getReader($config);
if (!method_exists($reader, 'dump')) {
throw new ConfigureException(__d('cake', 'The "%s" adapter, does not have a dump() method.', $config));
}
$values = self::$_values;
if (!empty($keys) && is_array($keys)) {
$values = array_intersect_key($values, array_flip($keys));
}
return (bool)self::$_readers[$config]->dump($key, $values);
return (bool)$reader->dump($key, $values);
}

/**
* Get the configured reader. Internally used by `Configure::load()` and `Configure::dump()`
* Will create new PhpReader for default if not configured yet.
*
* @param string $config The name of the configured adapter
* @return boolean success
* @throws ConfigureException if the adapter is not configured
*/
protected static function _getReader($config) {
if (!isset(self::$_readers[$config])) {
if ($config !== 'default') {
throw new ConfigureException(__d('cake', 'There is no "%s" adapter.', $config));
}
App::uses('PhpReader', 'Configure');
self::config($config, new PhpReader());
}
return self::$_readers[$config];
}

/**
Expand Down Expand Up @@ -381,7 +391,7 @@ public static function clear() {
}
/**
* Set the error and exception handlers.
*
*
* @param array $error The Error handling configuration.
* @param array $exception The exception handling configuration.
* @return void
Expand Down

0 comments on commit 19ba092

Please sign in to comment.