Make file extension optional for Configure reader adapter's dump function.

This commit is contained in:
ADmad 2012-08-15 19:38:58 +05:30
parent 76f93178a8
commit 18c5a4aee5
4 changed files with 24 additions and 0 deletions

View file

@ -142,6 +142,7 @@ class IniReader implements ConfigReaderInterface {
* Dumps the state of Configure data into an ini formatted string. * Dumps the state of Configure data into an ini formatted string.
* *
* @param string $filename The filename on $this->_path to save into. * @param string $filename The filename on $this->_path to save into.
* Extension ".ini" will be automatically appended if not included in filename.
* @param array $data The data to convert to ini file. * @param array $data The data to convert to ini file.
* @return int Bytes saved. * @return int Bytes saved.
*/ */
@ -159,6 +160,10 @@ class IniReader implements ConfigReaderInterface {
} }
} }
$contents = join("\n", $result); $contents = join("\n", $result);
if (substr($filename, -4) !== '.ini') {
$filename .= '.ini';
}
return file_put_contents($this->_path . $filename, $contents); return file_put_contents($this->_path . $filename, $contents);
} }

View file

@ -92,11 +92,16 @@ class PhpReader implements ConfigReaderInterface {
* be used saved into a file and loaded later. * be used saved into a file and loaded later.
* *
* @param string $filename The filename to create on $this->_path. * @param string $filename The filename to create on $this->_path.
* Extension ".php" will be automatically appended if not included in filename.
* @param array $data Data to dump. * @param array $data Data to dump.
* @return int Bytes saved. * @return int Bytes saved.
*/ */
public function dump($filename, $data) { public function dump($filename, $data) {
$contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';'; $contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
if (substr($filename, -4) !== '.php') {
$filename .= '.php';
}
return file_put_contents($this->_path . $filename, $contents); return file_put_contents($this->_path . $filename, $contents);
} }

View file

@ -166,6 +166,13 @@ INI;
unlink($file); unlink($file);
$this->assertTextEquals($expected, $result); $this->assertTextEquals($expected, $result);
$result = $reader->dump('test', $this->testData);
$this->assertTrue($result > 0);
$contents = file_get_contents($file);
$this->assertTextEquals($expected, $contents);
unlink($file);
} }
/** /**

View file

@ -151,6 +151,13 @@ PHP;
unlink($file); unlink($file);
$this->assertTextEquals($expected, $contents); $this->assertTextEquals($expected, $contents);
$result = $reader->dump('test', $this->testData);
$this->assertTrue($result > 0);
$contents = file_get_contents($file);
$this->assertTextEquals($expected, $contents);
unlink($file);
} }
/** /**