Skip to content

Commit

Permalink
Adding an interface to define the necessary public methods.
Browse files Browse the repository at this point in the history
Adding insertion of configure readers.
Adding tests.
  • Loading branch information
markstory committed Dec 4, 2010
1 parent 7863f14 commit 9b8456c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cake/libs/config/php_reader.php
Expand Up @@ -24,7 +24,7 @@
*
* @package cake.libs.config
*/
class PhpReader {
class PhpReader implements ConfigReaderInterface {
/**
* The path this reader finds files on.
*
Expand Down
52 changes: 52 additions & 0 deletions cake/libs/configure.php
Expand Up @@ -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:
Expand Down Expand Up @@ -249,6 +257,33 @@ public static function delete($var = null) {
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.
*
Expand Down Expand Up @@ -380,4 +415,21 @@ private static function __writeConfig($content, $name, $write = true) {
}
}

}

/**
* 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);
}
25 changes: 25 additions & 0 deletions cake/tests/cases/libs/configure.test.php
Expand Up @@ -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
Expand Down Expand Up @@ -256,5 +257,29 @@ function testVersion() {
$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);
}
}

0 comments on commit 9b8456c

Please sign in to comment.