Skip to content

Commit c23f4dc

Browse files
Rewrite PhpFormatter::format method, to avoid add extra spaces
1 parent cb93fe5 commit c23f4dc

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,48 @@ class PhpFormatter implements FormatterInterface
2121
public function format($data, array $comments = [])
2222
{
2323
if (!empty($comments) && is_array($data)) {
24-
$elements = [];
24+
return "<?php\nreturn array (\n" . $this->formatData($data, $comments, ' ') . "\n);\n";
25+
}
26+
return "<?php\nreturn " . var_export($data, true) . ";\n";
27+
}
28+
29+
/**
30+
* Format supplied data
31+
*
32+
* @param $data
33+
* @param $comments
34+
* @param string $prefix
35+
* @return string
36+
*/
37+
protected function formatData($data, $comments, $prefix = '')
38+
{
39+
$elements = [];
40+
41+
if (is_array($data)) {
2542
foreach ($data as $key => $value) {
26-
$comment = ' ';
2743
if (!empty($comments[$key])) {
28-
$section = " * For the section: " . $key . "\n";
29-
$exportedComment = is_string($comments[$key])
30-
? $comments[$key]
31-
: var_export($comments[$key], true);
32-
$comment = " /**\n" . $section . " * " . str_replace("\n", "\n * ", $exportedComment) . "\n */\n";
44+
$elements[] = $prefix . '/**';
45+
$elements[] = $prefix . ' * For the section: ' . $key;
46+
47+
foreach (explode("\n", $comments[$key]) as $commentLine) {
48+
$elements[] = $prefix . ' * ' . $commentLine;
49+
}
50+
51+
$elements[] = $prefix . " */";
52+
}
53+
54+
if (is_array($value)) {
55+
$elements[] = $prefix . var_export($key, true) . ' => ';
56+
$elements[] = $prefix . 'array (';
57+
$elements[] = $this->formatData($value, [], ' ' . $prefix);
58+
$elements[] = $prefix . '),';
59+
} else {
60+
$elements[] = $prefix . var_export($key, true) . ' => ' . var_export($value, true) . ',';
3361
}
34-
$space = is_array($value) ? " \n" : ' ';
35-
$elements[] = $comment . var_export($key, true) . ' =>' . $space . var_export($value, true);
3662
}
37-
return "<?php\nreturn array (\n" . implode(",\n", str_replace("\n", "\n ", $elements)) . "\n);\n";
63+
return implode("\n", $elements);
3864
}
39-
return "<?php\nreturn " . var_export($data, true) . ";\n";
65+
66+
return var_export($data, true);
4067
}
4168
}

0 commit comments

Comments
 (0)