Skip to content

Commit

Permalink
Merge branch '5.4' into 6.2
Browse files Browse the repository at this point in the history
* 5.4:
  trim(): Argument #1 () must be of type string, bool given
  [Dumper] Trim leading newlines when checking if value begins with a space
  Fix the list of supported shells for completions in a phar
  • Loading branch information
nicolas-grekas committed Apr 28, 2023
2 parents 7743777 + 176d1de commit c161e05
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
16 changes: 13 additions & 3 deletions src/Symfony/Component/Console/Command/DumpCompletionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,18 @@ private function tailDebugLog(string $commandName, OutputInterface $output): voi
*/
private function getSupportedShells(): array
{
return $this->supportedShells ??= array_map(function ($f) {
return pathinfo($f, \PATHINFO_EXTENSION);
}, glob(__DIR__.'/../Resources/completion.*'));
if (null !== $this->supportedShells) {
return $this->supportedShells;
}

$shells = [];

foreach (new \DirectoryIterator(__DIR__.'/../Resources/') as $file) {
if (str_starts_with($file->getBasename(), 'completion.') && $file->isFile()) {
$shells[] = $file->getExtension();
}
}

return $this->supportedShells = $shells;
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public static function hasSttyAvailable(): bool
private static function initDimensions()
{
if ('\\' === \DIRECTORY_SEPARATOR) {
if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
$ansicon = getenv('ANSICON');
if (false !== $ansicon && preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim($ansicon), $matches)) {
// extract [w, H] from "wxh (WxH)"
// or [w, h] from "wxh"
self::$width = (int) $matches[1];
Expand Down
28 changes: 19 additions & 9 deletions src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
}

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && str_contains($value, "\n") && !str_contains($value, "\r")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = str_starts_with($value, ' ') ? (string) $this->indentation : '';
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value);

if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
$blockChompingIndicator = '+';
Expand All @@ -96,9 +94,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = str_starts_with($value->getValue(), ' ') ? (string) $this->indentation : '';
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
$output .= sprintf(' |%s', $blockIndentationIndicator);

foreach (explode("\n", $value->getValue()) as $row) {
Expand Down Expand Up @@ -143,9 +139,7 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
$output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
$output .= sprintf(' |%s', $blockIndentationIndicator);

foreach (explode("\n", $value->getValue()) as $row) {
Expand All @@ -161,4 +155,20 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i

return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
}

private function getBlockIndentationIndicator(string $value): string
{
$lines = explode("\n", $value);

// If the first line (that is neither empty nor contains only spaces)
// starts with a space character, the spec requires a block indentation indicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
foreach ($lines as $line) {
if ('' !== trim($line, ' ')) {
return (' ' === substr($line, 0, 1)) ? (string) $this->indentation : '';
}
}

return '';
}
}
36 changes: 36 additions & 0 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,42 @@ public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace
$this->assertSame($data, $this->parser->parse($yml));
}

public function testDumpMultiLineStringAsScalarBlockWhenFirstLineIsEmptyAndSecondLineHasLeadingSpace()
{
$data = [
'data' => [
'multi_line' => "\n the second line has leading spaces\nThe third line does not.",
],
];

$expected = "data:\n multi_line: |4-\n\n the second line has leading spaces\n The third line does not.";

$yml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
$this->assertSame($expected, $yml);
$this->assertSame($data, $this->parser->parse($yml));
}

public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasOnlySpaces()
{
$data = [
'data' => [
'multi_line' => " \nthe second line\nThe third line.",
],
];

$expectedData = [
'data' => [
'multi_line' => "\nthe second line\nThe third line.",
],
];

$expectedYml = "data:\n multi_line: |-\n \n the second line\n The third line.";

$yml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
$this->assertSame($expectedYml, $yml);
$this->assertSame($expectedData, $this->parser->parse($yml));
}

public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
{
$data = ["a\r\nb\nc"];
Expand Down

0 comments on commit c161e05

Please sign in to comment.