8000 Added pipe for multiline string (correctly indented), which makes them much more readable by mercmobily · Pull Request #17912 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Added pipe for multiline string (correctly indented), which makes them much more readable #17912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Multiline strings handled with correct tabs
  • Loading branch information
mercmobily committed Feb 24, 2016
commit 2d3b1fa84115fca5d1b0a91f2a6b969bbe014133
9 changes: 5 additions & 4 deletions src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function setIndentation($num)
*
* @return string The YAML representation of the PHP value
*/
public function dump($input, $inline = 0, $indent = 0, $flags = 0)
public function dump($input, $inline = 0, $indent = 0, $absIndent = 0, $flags = 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new argument must be placed after the flags, for BC reasons

{
if (is_bool($flags)) {
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
Expand All @@ -79,7 +79,7 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
$prefix = $indent ? str_repeat(' ', $indent) : '';

if ($inline <= 0 || !is_array($input) || empty($input)) {
$output .= $prefix.Inline::dump($input, $flags);
$output .= $prefix.Inline::dump($input, $flags, $absIndent);
} else {
$isAHash = array_keys($input) !== range(0, count($input) - 1);

Expand All @@ -88,9 +88,10 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)

$output .= sprintf('%s%s%s%s',
$prefix,
$isAHash ? Inline::dump($key, $flags).':' : '-',
$isAHash ? Inline::dump($key, $flags, $absIndent).':' : '-',
$willBeInlined ? ' ' : "\n",
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $inline - 1 <= 0 ? 0 : $absIndent + $this->indentation, $flags)

).($willBeInlined ? "\n" : '');
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static function parse($value, $flags = 0, $references = array())
*
* @throws DumpException When trying to dump PHP resource
*/
public static function dump($value, $flags = 0)
public static function dump($value, $flags = 0, $indent = 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this new argument will mess up with the BC layer for the old signature currently.

{
if (is_bool($flags)) {
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
Expand Down Expand Up @@ -165,7 +165,7 @@ public static function dump($value, $flags = 0)

return 'null';
case is_array($value):
return self::dumpArray($value, $flags);
return self::dumpArray($value, $flags, $indent);
case null === $value:
return 'null';
case true === $value:
Expand Down Expand Up @@ -197,6 +197,15 @@ public static function dump($value, $flags = 0)
return $repr;
case '' == $value:
return "''";
case strstr($value, "\n"):
if (Yaml::DUMP_MULTI_LINE_AS_BLOCK & $flags) {
if ($indent) {
$prefix = $indent ? str_repeat(' ', $indent) : '';
return "|\n$prefix". preg_replace( '/\n/',"\n$prefix", str_replace( "\r", '', $value ) );
}
}


case Escaper::requiresDoubleQuoting($value):
return Escaper::escapeWithDoubleQuotes($value);
case Escaper::requiresSingleQuoting($value):
Expand All @@ -216,7 +225,7 @@ public static function dump($value, $flags = 0)
*
* @return string The YAML string representing the PHP array
*/
private static function dumpArray($value, $flags)
private static function dumpArray($value, $flags, $indent)
{
// array
$keys = array_keys($value);
Expand All @@ -226,7 +235,7 @@ private static function dumpArray($value, $flags)
) {
$output = array();
foreach ($value as $val) {
$output[] = self::dump($val, $flags);
$output[] = self::dump($val, $flags, $indent);
}

return sprintf('[%s]', implode(', ', $output));
Expand All @@ -235,7 +244,7 @@ private static function dumpArray($value, $flags)
// mapping
$output = array();
foreach ($value as $key => $val) {
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
$output[] = sprintf('%s: %s', self::dump($key, $flags, $indent), self::dump($val, $flags, $indent));
}

return sprintf('{ %s }', implode(', ', $output));
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Yaml/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Yaml
const PARSE_OBJECT_FOR_MAP = 8;
const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
const PARSE_DATETIME = 32;
const DUMP_MULTI_LINE_AS_BLOCK = 64;

/**
* Parses YAML into a PHP value.
Expand Down Expand Up @@ -89,7 +90,7 @@ public static function parse($input, $flags = 0)
*
* @return string A YAML string representing the original PHP array
*/
public static function dump($array, $inline = 2, $indent = 4, $flags = 0)
public static function dump($array, $inline = 2, $indent = 4, $absInden = 0,$flags = 0)
{
if (is_bool($flags)) {
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
Expand All @@ -111,6 +112,6 @@ public static function dump($array, $inline = 2, $indent = 4, $flags = 0)

$yaml = new Dumper($indent);

return $yaml->dump($array, $inline, 0, $flags);
return $yaml->dump($array, $inline, 0, 0, $flags);
}
}
0