Skip to content
Permalink
Browse files

Make the configure adapters responsible for persistence.

Each adapter should be handling persistence.  This allows
different adapters to handle saving config files in different ways
or places.
  • Loading branch information...
markstory committed Apr 22, 2012
1 parent 9f37277 commit 6ba923d20fbb317567b88fc660f75fd6f4655a0f
@@ -140,10 +140,11 @@ protected function _parseNestedValues($values) {
/**
* Dumps the state of Configure data into an ini formatted string.
*
* @param string $filename The filename on $this->_path to save into.
* @param array $data The data to convert to ini file.
* @return string The converted configuration as an ini string
* @return int Bytes saved.
*/
public function dump($data) {
public function dump($filename, $data) {
$result = array();
foreach ($data as $key => $value) {
if ($key[0] != '[') {
@@ -156,6 +157,7 @@ public function dump($data) {
}
}
}
return join("\n", $result);
$contents = join("\n", $result);
return file_put_contents($this->_path . $filename, $contents);
}
}
@@ -91,11 +91,13 @@ public function read($key) {
* Converts the provided $data into a string of PHP code that can
* be used saved into a file and loaded later.
*
* @param string $filename The filename to create on $this->_path.
* @param array $data Data to dump.
* @return string String or PHP code.
* @return int Bytes saved.
*/
public function dump($data) {
return '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
public function dump($filename, $data) {
$contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
return file_put_contents($this->_path . $filename, $contents);
}
}
@@ -284,20 +284,20 @@ public static function load($key, $config = 'default', $merge = true) {
* 'default' adapter is a PhpReader, the generated file will be a PHP configuration
* file loadable by the PhpReader.
*
* @param string $filename The filename to save content into.
* @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.
* @return void
* @return boolean success
* @throws ConfigureException if the adapter does not implement a `dump` method.
*/
public static function dump($filename, $config = 'default') {
public static function dump($key, $config = 'default') {
if (empty(self::$_readers[$config])) {
throw new ConfigureException(__d('cake', 'There is no "%s" adapter.', $config));
}
if (!method_exists(self::$_readers[$config], 'dump')) {
throw new ConfigureException(__d('cake', 'The "%s" adapter, does not have a dump() method.', $config));
}
$content = self::$_readers[$config]->dump(self::$_values);
return file_put_contents($filename, $content);
return (bool)self::$_readers[$config]->dump($key, self::$_values);
}
/**
@@ -144,14 +144,20 @@ public function testDump() {
'timestamp' => 'force'
),
);
$result = $reader->dump($data);
$result = $reader->dump('test.ini', $data);
$this->assertTrue($result > 0);
$expected = <<<INI
[One]
two = value
three.four = value four
[Asset]
timestamp = force
INI;
$file = $this->path . 'test.ini';
$result = file_get_contents($file);
unlink($file);
$this->assertEquals($expected, $result);
}
@@ -116,7 +116,8 @@ public function testDump() {
'timestamp' => 'force'
),
);
$result = $reader->dump($data);
$result = $reader->dump('test.php', $data);
$this->assertTrue($result > 0);
$expected = <<<PHP
<?php
\$config = array (
@@ -135,7 +136,10 @@ public function testDump() {
),
);
PHP;
$this->assertEquals($expected, $result);
$file = $this->path . 'test.php';
$contents = file_get_contents($file);
@unlink($contents);
$this->assertEquals($expected, $contents);
}
}
@@ -378,7 +378,7 @@ public function testDumpNoAdapter() {
public function testDump() {
Configure::config('test_reader', new PhpReader(TMP));
$result = Configure::dump(TMP . 'config_test.php', 'test_reader');
$result = Configure::dump('config_test.php', 'test_reader');
$this->assertTrue($result > 0);
$result = file_get_contents(TMP . 'config_test.php');
$this->assertContains('<?php', $result);

0 comments on commit 6ba923d

Please sign in to comment.
You can’t perform that action at this time.