diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 6516e4cb22d6a..61f0458c0560a 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -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) { 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); @@ -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); @@ -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" : ''); } } diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index b11e03147cd75..fbee784a5cb86 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -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) { 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); @@ -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: @@ -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): @@ -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); @@ -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)); @@ -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)); diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index 9c321a81322e1..d65dee9facac3 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -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. @@ -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, $absIndent = 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); @@ -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); } }