mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
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.
This commit is contained in:
parent
9f37277dab
commit
6ba923d20f
6 changed files with 29 additions and 15 deletions
|
@ -140,10 +140,11 @@ class IniReader implements ConfigReaderInterface {
|
|||
/**
|
||||
* 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 @@ class IniReader implements ConfigReaderInterface {
|
|||
}
|
||||
}
|
||||
}
|
||||
return join("\n", $result);
|
||||
$contents = join("\n", $result);
|
||||
return file_put_contents($this->_path . $filename, $contents);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,11 +91,13 @@ class PhpReader implements ConfigReaderInterface {
|
|||
* 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 @@ class Configure {
|
|||
* '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,7 +144,9 @@ class IniReaderTest extends CakeTestCase {
|
|||
'timestamp' => 'force'
|
||||
),
|
||||
);
|
||||
$result = $reader->dump($data);
|
||||
$result = $reader->dump('test.ini', $data);
|
||||
$this->assertTrue($result > 0);
|
||||
|
||||
$expected = <<<INI
|
||||
[One]
|
||||
two = value
|
||||
|
@ -152,6 +154,10 @@ 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 @@ class PhpReaderTest extends CakeTestCase {
|
|||
'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 @@ class PhpReaderTest extends CakeTestCase {
|
|||
),
|
||||
);
|
||||
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 @@ class ConfigureTest extends CakeTestCase {
|
|||
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);
|
||||
|
|
Loading…
Add table
Reference in a new issue