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

Commit

Permalink
Modified PhpArray config writer to generate better readable array for…
Browse files Browse the repository at this point in the history
…mat.
  • Loading branch information
michaelmoussa committed Oct 12, 2013
1 parent dd327e7 commit dc75eea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
22 changes: 19 additions & 3 deletions library/Zend/Config/Writer/PhpArray.php
Expand Up @@ -19,9 +19,25 @@ class PhpArray extends AbstractWriter
*/
public function processConfig(array $config)
{
$arrayString = "<?php\n"
. "return " . var_export($config, true) . ";\n";
$arrayString = "<?php\n\n"
. "return";
$indentLevel = 0;

return $arrayString;
foreach (explode("\n", var_export($config, true)) as $line) {
$line = trim($line);

if ($line === '),' || $line === ')') {
$indentLevel--;
} else if (preg_match('/^\s*array \(/', $line)) {
$line = 'array(';
$indentLevel++;
$arrayString .= ' ' . $line;
continue;
}

$arrayString .= "\n" . str_repeat(' ', $indentLevel) . $line;
}

return $arrayString . ";\n";
}
}
15 changes: 7 additions & 8 deletions tests/ZendTest/Config/FactoryTest.php
Expand Up @@ -176,14 +176,13 @@ public function testFactoryWriteToFile()
$result = Factory::toFile($file, $config);

// build string line by line as we are trailing-whitespace sensitive.
$expected = "<?php\n";
$expected .= "return array (\n";
$expected .= " 'test' => 'foo',\n";
$expected .= " 'bar' => \n";
$expected .= " array (\n";
$expected .= " 0 => 'baz',\n";
$expected .= " 1 => 'foo',\n";
$expected .= " ),\n";
$expected = "<?php\n\n";
$expected .= "return array(\n";
$expected .= " 'test' => 'foo',\n";
$expected .= " 'bar' => array(\n";
$expected .= " 0 => 'baz',\n";
$expected .= " 1 => 'foo',\n";
$expected .= " ),\n";
$expected .= ");\n";

$this->assertEquals(true, $result);
Expand Down
15 changes: 7 additions & 8 deletions tests/ZendTest/Config/Writer/PhpArrayTest.php
Expand Up @@ -36,14 +36,13 @@ public function testRender()
$configString = $this->writer->toString($config);

// build string line by line as we are trailing-whitespace sensitive.
$expected = "<?php\n";
$expected .= "return array (\n";
$expected .= " 'test' => 'foo',\n";
$expected .= " 'bar' => \n";
$expected .= " array (\n";
$expected .= " 0 => 'baz',\n";
$expected .= " 1 => 'foo',\n";
$expected .= " ),\n";
$expected = "<?php\n\n";
$expected .= "return array(\n";
$expected .= " 'test' => 'foo',\n";
$expected .= " 'bar' => array(\n";
$expected .= " 0 => 'baz',\n";
$expected .= " 1 => 'foo',\n";
$expected .= " ),\n";
$expected .= ");\n";

$this->assertEquals($expected, $configString);
Expand Down

0 comments on commit dc75eea

Please sign in to comment.