Skip to content

Commit

Permalink
Add Configure::dump().
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 2, 2012
1 parent 578dac9 commit 9f37277
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/Cake/Core/Configure.php
Expand Up @@ -278,6 +278,28 @@ public static function load($key, $config = 'default', $merge = true) {
return self::write($values); return self::write($values);
} }


/**
* Dump data currently in Configure into $filename. The serialization format
* is decided by the config reader attached as $config. For example, if the
* '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 $config The name of the configured adapter to dump data with.
* @return void
* @throws ConfigureException if the adapter does not implement a `dump` method.
*/
public static function dump($filename, $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);
}

/** /**
* Used to determine the current version of CakePHP. * Used to determine the current version of CakePHP.
* *
Expand Down
24 changes: 24 additions & 0 deletions lib/Cake/Test/Case/Core/ConfigureTest.php
Expand Up @@ -362,4 +362,28 @@ public function testClear() {
$this->assertNull(Configure::read('debug')); $this->assertNull(Configure::read('debug'));
$this->assertNull(Configure::read('test')); $this->assertNull(Configure::read('test'));
} }

/**
* @expectedException ConfigureException
*/
public function testDumpNoAdapter() {
Configure::dump(TMP . 'test.php', 'does_not_exist');
}

/**
* test dump integrated with the PhpReader.
*
* @return void
*/
public function testDump() {
Configure::config('test_reader', new PhpReader(TMP));

$result = Configure::dump(TMP . 'config_test.php', 'test_reader');
$this->assertTrue($result > 0);
$result = file_get_contents(TMP . 'config_test.php');
$this->assertContains('<?php', $result);
$this->assertContains('$config = ', $result);
@unlink(TMP . 'config_test.php');
}

} }

0 comments on commit 9f37277

Please sign in to comment.