Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/5880' into develop
Browse files Browse the repository at this point in the history
Close #5880
  • Loading branch information
weierophinney committed Feb 28, 2014
2 parents df6fa23 + 2b75e3b commit 20a9f88
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
49 changes: 49 additions & 0 deletions library/Zend/Config/Writer/PhpArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Zend\Config\Writer;

use Zend\Config\Exception;

class PhpArray extends AbstractWriter
{
/**
Expand Down Expand Up @@ -51,6 +53,53 @@ public function setUseBracketArraySyntax($value)
return $this;
}

/**
* toFile(): defined by Writer interface.
*
* @see WriterInterface::toFile()
* @param string $filename
* @param mixed $config
* @param bool $exclusiveLock
* @return void
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*/
public function toFile($filename, $config, $exclusiveLock = true)
{
if (empty($filename)) {
throw new Exception\InvalidArgumentException('No file name specified');
}

$flags = 0;
if ($exclusiveLock) {
$flags |= LOCK_EX;
}

set_error_handler(
function ($error, $message = '', $file = '', $line = 0) use ($filename) {
throw new Exception\RuntimeException(sprintf(
'Error writing to "%s": %s',
$filename, $message
), $error);
}, E_WARNING
);

try {
// for Windows, paths are escaped.
$dirname = str_replace('\\', '\\\\', dirname($filename));

$string = $this->toString($config);
$string = str_replace("'" . $dirname, "__DIR__ . '", $string);

file_put_contents($filename, $string, $flags);
} catch (\Exception $e) {
restore_error_handler();
throw $e;
}

restore_error_handler();
}

/**
* Recursively processes a PHP config array structure into a readable format.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/ZendTest/Config/Writer/PhpArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ public function testRenderWithQuotesInString()
$this->assertEquals($expected, $configString);
}

public function testWriteConvertsPathToDirWhenWritingBackToFile()
{
$filename = $this->getTestAssetFileName();
file_put_contents($filename, file_get_contents(__DIR__ . '/_files/array.php'));

$this->writer->toFile($filename, include $filename);

// Ensure file endings are same
$expected = trim(file_get_contents(__DIR__ . '/_files/array.php'));
$expected = preg_replace("~\r\n|\n|\r~", PHP_EOL, $expected);

$result = trim(file_get_contents($filename));
$result = preg_replace("~\r\n|\n|\r~", PHP_EOL, $result);

$this->assertSame($expected, $result);
}

public function testSetUseBracketArraySyntaxReturnsFluentInterface()
{
$this->assertSame($this->writer, $this->writer->setUseBracketArraySyntax(true));
Expand Down
4 changes: 4 additions & 0 deletions tests/ZendTest/Config/Writer/_files/array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
return array(
'dir' => __DIR__ . '/foobar.php',
);

0 comments on commit 20a9f88

Please sign in to comment.