-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Modified PhpArray config writer to generate better readable array format. #5259
Changes from 9 commits
dc75eea
14260d6
99fa6cb
43bb44f
0af6a9c
fcfd2ce
35589ef
e489dd3
a9524cd
fd208c3
b117306
c2a1efa
a28df59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,16 @@ | |
|
||
class PhpArray extends AbstractWriter | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
const INDENT_STRING = ' '; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $useBracketArraySyntax = false; | ||
|
||
/** | ||
* processConfig(): defined by AbstractWriter. | ||
* | ||
|
@@ -19,8 +29,58 @@ class PhpArray extends AbstractWriter | |
*/ | ||
public function processConfig(array $config) | ||
{ | ||
$arrayString = "<?php\n" | ||
. "return " . var_export($config, true) . ";\n"; | ||
$arraySyntax = array( | ||
'open' => $this->useBracketArraySyntax ? '[' : 'array(', | ||
'close' => $this->useBracketArraySyntax ? ']' : ')' | ||
); | ||
|
||
return "<?php\n" . | ||
"return " . $arraySyntax['open'] . "\n" . $this->processIndented($config, $arraySyntax) . | ||
$arraySyntax['close'] . ";\n"; | ||
} | ||
|
||
/** | ||
* Sets whether or not to use the PHP 5.4+ "[]" array syntax. | ||
* | ||
* @param bool $value | ||
* @return void | ||
*/ | ||
public function setUseBracketArraySyntax($value) | ||
{ | ||
$this->useBracketArraySyntax = $value; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add docblock. |
||
/** | ||
* Recursively processes a PHP config array structure into a readable format. | ||
* | ||
* @param array $config | ||
* @param array $arraySyntax | ||
* @param int $indentLevel | ||
* @return string | ||
*/ | ||
protected function processIndented(array $config, array $arraySyntax, &$indentLevel = 1) | ||
{ | ||
$arrayString = ""; | ||
|
||
foreach ($config as $key => $value) { | ||
$arrayString .= str_repeat(self::INDENT_STRING, $indentLevel); | ||
$arrayString .= (is_int($key) ? $key : "'" . addslashes($key) . "'") . ' => '; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should contain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #5259 (comment) and The PHP Manual
|
||
|
||
if (is_array($value)) { | ||
if ($value === array()) { | ||
$arrayString .= $arraySyntax['open'] . $arraySyntax['close'] . ",\n"; | ||
} else { | ||
$indentLevel++; | ||
$arrayString .= $arraySyntax['open'] . "\n" | ||
. $this->processIndented($value, $arraySyntax, $indentLevel) | ||
. str_repeat(self::INDENT_STRING, --$indentLevel) . $arraySyntax['close'] . ",\n"; | ||
} | ||
} elseif (is_object($value)) { | ||
$arrayString .= var_export($value, true) . ",\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this actually work properly, coding standard wise? Please add a unit tests for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you be more specific? (there are several lines highlighted, so I'm not sure which part you're talking about). Are you talking about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to see a unit test for this type of value. I think that the only safe way to export an object is serializing it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgive my dubts about the export format. However the tests should cover this type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using It uses the magic That said, this is really a "bad practices edge case" IMO. Configuration files should contain configuration settings, not full-blown objects. This is only here for backwards compatibility. I hope that all makes sense. |
||
} else { | ||
$arrayString .= "'" . addslashes($value) . "',\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do namespaced classes turn out with this? I suppose that they'll end up like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existing implementation turns them into This could probably be done by checking for The aim of this PR, though, was to improve indentation and structure for readability. If merging of this PR is contingent upon prettying up namespaced classes as well then I'll see about doing so sometime early next week. Otherwise feel free to create a separate issue and refer me to it if there's no other reason to hold this up. |
||
} | ||
} | ||
|
||
return $arrayString; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a fluent interface here (return $this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. See updated PR