diff --git a/cake/libs/config/php_reader.php b/cake/libs/config/php_reader.php index 3cf5e35ee..dafea6da0 100644 --- a/cake/libs/config/php_reader.php +++ b/cake/libs/config/php_reader.php @@ -24,7 +24,7 @@ * * @package cake.libs.config */ -class PhpReader { +class PhpReader implements ConfigReaderInterface { /** * The path this reader finds files on. * diff --git a/cake/libs/configure.php b/cake/libs/configure.php index 34b6bbc52..941c0b3c6 100644 --- a/cake/libs/configure.php +++ b/cake/libs/configure.php @@ -40,6 +40,14 @@ class Configure { '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. * Bootstrapping includes the following steps: @@ -249,6 +257,33 @@ class Configure { 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. * @@ -380,4 +415,21 @@ 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); } \ No newline at end of file diff --git a/cake/tests/cases/libs/configure.test.php b/cake/tests/cases/libs/configure.test.php index a99790291..20e8100f3 100644 --- a/cake/tests/cases/libs/configure.test.php +++ b/cake/tests/cases/libs/configure.test.php @@ -19,6 +19,7 @@ * @since CakePHP(tm) v 1.2.0.5432 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +App::import('Core', 'config/PhpReader'); /** * ConfigureTest @@ -256,5 +257,29 @@ class ConfigureTest extends CakeTestCase { $result = Configure::version(); $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); + } }