Skip to content
Permalink
Browse files

Adding an interface to define the necessary public methods.

Adding insertion of configure readers.
Adding tests.
  • Loading branch information...
markstory committed Dec 4, 2010
1 parent 7863f14 commit 9b8456ce6f9349db54b06153247ecd64e7659101
Showing with 78 additions and 1 deletion.
  1. +1 −1 cake/libs/config/php_reader.php
  2. +52 −0 cake/libs/configure.php
  3. +25 −0 cake/tests/cases/libs/configure.test.php
@@ -24,7 +24,7 @@
*
* @package cake.libs.config
*/
class PhpReader {
class PhpReader implements ConfigReaderInterface {
/**
* The path this reader finds files on.
*
@@ -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 @@ 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.
*
@@ -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);
}
@@ -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 @@ 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.
You can’t perform that action at this time.