Browse Source

Make load() and dump() more consistent with each other, create new PhpReader for default if not configured yet.
Fixes #2998

Ceeram 13 years ago
parent
commit
19ba09286d
1 changed files with 27 additions and 17 deletions
  1. 27 17
      lib/Cake/Core/Configure.php

+ 27 - 17
lib/Cake/Core/Configure.php

@@ -261,15 +261,8 @@ class Configure {
  * @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);
@@ -286,7 +279,7 @@ class Configure {
 /**
  * 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
@@ -303,23 +296,40 @@ class Configure {
  * @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];
 	}
 
 /**
@@ -381,7 +391,7 @@ class Configure {
 	}
 /**
  * Set the error and exception handlers.
- * 
+ *
  * @param array $error The Error handling configuration.
  * @param array $exception The exception handling configuration.
  * @return void