Adding an interface to define the necessary public methods.

Adding insertion of configure readers.
Adding tests.
This commit is contained in:
mark_story 2010-12-04 00:09:11 -05:00
parent 7863f14d79
commit 9b8456ce6f
3 changed files with 78 additions and 1 deletions

View file

@ -24,7 +24,7 @@
* *
* @package cake.libs.config * @package cake.libs.config
*/ */
class PhpReader { class PhpReader implements ConfigReaderInterface {
/** /**
* The path this reader finds files on. * The path this reader finds files on.
* *

View file

@ -40,6 +40,14 @@ class Configure {
'debug' => 0 'debug' => 0
); );
/**
* Configured reader classes, used to load config files from resources
*
* @var array
* @see Configure::load()
*/
protected static $_readers = array();
/** /**
* Initializes configure and runs the bootstrap process. * Initializes configure and runs the bootstrap process.
* Bootstrapping includes the following steps: * Bootstrapping includes the following steps:
@ -249,6 +257,33 @@ class Configure {
self::$_values[$names[0]] = Set::remove(self::$_values[$names[0]], $names[1]); self::$_values[$names[0]] = Set::remove(self::$_values[$names[0]], $names[1]);
} }
/**
* Add a new reader to Configure. Readers allow you to read configuration
* files in various formats/storage locations. CakePHP comes with two built-in readers
* PhpReader and IniReader. You can also implement your own reader classes in your application.
*
* To add a new reader to Configure:
*
* `Configure::reader('ini', new IniReader());`
*
* @param string $alias The name of the reader being configured. This alias is used later to
* read values from a specific reader.
* @param ConfigReaderInterface $reader The reader to append.
* @return void
*/
public static function reader($alias, ConfigReaderInterface $reader) {
self::$_readers[$alias] = $reader;
}
/**
* Gets the names of the configured reader objects.
*
* @return array Array of the configured reader objects.
*/
public static function configured() {
return array_keys(self::$_readers);
}
/** /**
* Loads a file from app/config/configure_file.php. * Loads a file from app/config/configure_file.php.
* *
@ -381,3 +416,20 @@ class Configure {
} }
} }
/**
* An interface for creating objects compatible with Configure::load()
*
* @package cake.libs
*/
interface ConfigReaderInterface {
/**
* Read method is used for reading configuration information from sources.
* These sources can either be static resources like files, or dynamic ones like
* a database, or other datasource.
*
* @param string $key
* @return array An array of data to merge into the runtime configuration
*/
function read($key);
}

View file

@ -19,6 +19,7 @@
* @since CakePHP(tm) v 1.2.0.5432 * @since CakePHP(tm) v 1.2.0.5432
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/ */
App::import('Core', 'config/PhpReader');
/** /**
* ConfigureTest * ConfigureTest
@ -256,5 +257,29 @@ class ConfigureTest extends CakeTestCase {
$result = Configure::version(); $result = Configure::version();
$this->assertTrue(version_compare($result, '1.2', '>=')); $this->assertTrue(version_compare($result, '1.2', '>='));
} }
/**
* test adding new readers.
*
* @return void
*/
function testReaderSetup() {
$reader = new PhpReader();
Configure::reader('test', $reader);
$configured = Configure::configured();
$this->assertTrue(in_array('test', $configured));
}
/**
* test reader() throwing exceptions on missing interface.
*
* @expectedException Exception
* @return void
*/
function testReaderExceptionOnIncorrectClass() {
$reader = new StdClass();
Configure::reader('test', $reader);
}
} }