Skip to content

Commit eb128a6

Browse files
author
Oleksii Korshenko
authored
MAGETWO-82236: [Backport 2.2-develop] #11328 : app:config:dump adds extra space every time in multiline array value #11439
2 parents 4fd9da4 + 2bfa6b5 commit eb128a6

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
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+
$elements[] = $prefix . var_export($key, true) . ' => ' .
55+
(!is_array($value) ? var_export($value, true) . ',' : '');
56+
57+
if (is_array($value)) {
58+
$elements[] = $prefix . 'array (';
59+
$elements[] = $this->formatData($value, [], ' ' . $prefix);
60+
$elements[] = $prefix . '),';
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
}

lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Framework\App\Test\Unit\DeploymentConfig\Writer;
77

8-
use \Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
8+
use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
99

1010
class PhpFormatterTest extends \PHPUnit\Framework\TestCase
1111
{
@@ -81,7 +81,7 @@ public function formatWithCommentDataProvider()
8181
),
8282
),
8383
'ns3' => 'just text',
84-
'ns4' => 'just text'
84+
'ns4' => 'just text',
8585
);
8686
8787
TEXT;
@@ -126,7 +126,7 @@ public function formatWithCommentDataProvider()
126126
* For the section: ns4
127127
* comment for namespace 4
128128
*/
129-
'ns4' => 'just text'
129+
'ns4' => 'just text',
130130
);
131131
132132
TEXT;

0 commit comments

Comments
 (0)