Skip to content

Commit 262b4f3

Browse files
bug #24532 [DI] Fix possible incorrect php-code when dumped strings contains newlines (Strate)
This PR was squashed before being merged into the 2.7 branch (closes #24532). Discussion ---------- [DI] Fix possible incorrect php-code when dumped strings contains newlines | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | ? | License | MIT | Doc PR | no See discussion #24517 <!-- - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the 3.4, legacy code removals go to the master branch. - Please fill in this template according to the PR you're about to submit. - Replace this comment by a description of what your PR is solving. --> Commits ------- 345f2fc [DI] Fix possible incorrect php-code when dumped strings contains newlines
2 parents 5e0bb5f + 345f2fc commit 262b4f3

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,13 @@ private function export($value)
15741574
return $dirname;
15751575
}
15761576

1577+
if (is_string($value) && false !== strpos($value, "\n")) {
1578+
$cleanParts = explode("\n", $value);
1579+
$cleanParts = array_map(function ($part) { return var_export($part, true); }, $cleanParts);
1580+
1581+
return implode('."\n".', $cleanParts);
1582+
}
1583+
15771584
return var_export($value, true);
15781585
}
15791586
}

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function testDumpOptimizationString()
5555
'optimize concatenation with empty string' => 'string1%empty_value%string2',
5656
'optimize concatenation from the start' => '%empty_value%start',
5757
'optimize concatenation at the end' => 'end%empty_value%',
58+
'new line' => "string with \nnew line",
5859
));
5960

6061
$container = new ContainerBuilder();

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function isFrozen()
5656
*/
5757
protected function getTestService()
5858
{
59-
return $this->services['test'] = new \stdClass(array('only dot' => '.', 'concatenation as value' => '.\'\'.', 'concatenation from the start value' => '\'\'.', '.' => 'dot as a key', '.\'\'.' => 'concatenation as a key', '\'\'.' => 'concatenation from the start key', 'optimize concatenation' => 'string1-string2', 'optimize concatenation with empty string' => 'string1string2', 'optimize concatenation from the start' => 'start', 'optimize concatenation at the end' => 'end'));
59+
return $this->services['test'] = new \stdClass(array('only dot' => '.', 'concatenation as value' => '.\'\'.', 'concatenation from the start value' => '\'\'.', '.' => 'dot as a key', '.\'\'.' => 'concatenation as a key', '\'\'.' => 'concatenation from the start key', 'optimize concatenation' => 'string1-string2', 'optimize concatenation with empty string' => 'string1string2', 'optimize concatenation from the start' => 'start', 'optimize concatenation at the end' => 'end', 'new line' => 'string with '."\n".'new line'));
6060
}
6161

6262
/**

0 commit comments

Comments
 (0)