| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace WindPressDeps\Symfony\Component\Yaml; |
| 12 |
|
| 13 |
use WindPressDeps\Symfony\Component\Yaml\Tag\TaggedValue; |
| 14 |
/** |
| 15 |
* Dumper dumps PHP variables to YAML strings. |
| 16 |
* |
| 17 |
* @author Fabien Potencier <fabien@symfony.com> |
| 18 |
* |
| 19 |
* @final |
| 20 |
*/ |
| 21 |
class Dumper |
| 22 |
{ |
| 23 |
/** |
| 24 |
* The amount of spaces to use for indentation of nested nodes. |
| 25 |
* |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
protected $indentation; |
| 29 |
public function __construct(int $indentation = 4) |
| 30 |
{ |
| 31 |
if ($indentation < 1) { |
| 32 |
throw new \InvalidArgumentException('The indentation must be greater than zero.'); |
| 33 |
} |
| 34 |
$this->indentation = $indentation; |
| 35 |
} |
| 36 |
/** |
| 37 |
* Dumps a PHP value to YAML. |
| 38 |
* |
| 39 |
* @param mixed $input The PHP value |
| 40 |
* @param int $inline The level where you switch to inline YAML |
| 41 |
* @param int $indent The level of indentation (used internally) |
| 42 |
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string |
| 43 |
*/ |
| 44 |
public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string |
| 45 |
{ |
| 46 |
$output = ''; |
| 47 |
$prefix = $indent ? str_repeat(' ', $indent) : ''; |
| 48 |
$dumpObjectAsInlineMap = \true; |
| 49 |
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) { |
| 50 |
$dumpObjectAsInlineMap = empty((array) $input); |
| 51 |
} |
| 52 |
if ($inline <= 0 || !\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap || empty($input)) { |
| 53 |
$output .= $prefix . Inline::dump($input, $flags); |
| 54 |
} elseif ($input instanceof TaggedValue) { |
| 55 |
$output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix); |
| 56 |
} else { |
| 57 |
$dumpAsMap = Inline::isHash($input); |
| 58 |
foreach ($input as $key => $value) { |
| 59 |
if ('' !== $output && "\n" !== $output[-1]) { |
| 60 |
$output .= "\n"; |
| 61 |
} |
| 62 |
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && \false !== strpos($value, "\n") && \false === strpos($value, "\r")) { |
| 63 |
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value); |
| 64 |
if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) { |
| 65 |
$blockChompingIndicator = '+'; |
| 66 |
} elseif ("\n" === $value[-1]) { |
| 67 |
$blockChompingIndicator = ''; |
| 68 |
} else { |
| 69 |
$blockChompingIndicator = '-'; |
| 70 |
} |
| 71 |
$output .= sprintf('%s%s%s |%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags) . ':' : '-', '', $blockIndentationIndicator, $blockChompingIndicator); |
| 72 |
foreach (explode("\n", $value) as $row) { |
| 73 |
if ('' === $row) { |
| 74 |
$output .= "\n"; |
| 75 |
} else { |
| 76 |
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row); |
| 77 |
} |
| 78 |
} |
| 79 |
continue; |
| 80 |
} |
| 81 |
if ($value instanceof TaggedValue) { |
| 82 |
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags) . ':' : '-', $value->getTag()); |
| 83 |
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && \false !== strpos($value->getValue(), "\n") && \false === strpos($value->getValue(), "\r\n")) { |
| 84 |
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue()); |
| 85 |
$output .= sprintf(' |%s', $blockIndentationIndicator); |
| 86 |
foreach (explode("\n", $value->getValue()) as $row) { |
| 87 |
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row); |
| 88 |
} |
| 89 |
continue; |
| 90 |
} |
| 91 |
if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) { |
| 92 |
$output .= ' ' . $this->dump($value->getValue(), $inline - 1, 0, $flags) . "\n"; |
| 93 |
} else { |
| 94 |
$output .= "\n"; |
| 95 |
$output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags); |
| 96 |
} |
| 97 |
continue; |
| 98 |
} |
| 99 |
$dumpObjectAsInlineMap = \true; |
| 100 |
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) { |
| 101 |
$dumpObjectAsInlineMap = empty((array) $value); |
| 102 |
} |
| 103 |
$willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value); |
| 104 |
$output .= sprintf('%s%s%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags) . ':' : '-', $willBeInlined ? ' ' : "\n", $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)) . ($willBeInlined ? "\n" : ''); |
| 105 |
} |
| 106 |
} |
| 107 |
return $output; |
| 108 |
} |
| 109 |
private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix): string |
| 110 |
{ |
| 111 |
$output = sprintf('%s!%s', $prefix ? $prefix . ' ' : '', $value->getTag()); |
| 112 |
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && \false !== strpos($value->getValue(), "\n") && \false === strpos($value->getValue(), "\r\n")) { |
| 113 |
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue()); |
| 114 |
$output .= sprintf(' |%s', $blockIndentationIndicator); |
| 115 |
foreach (explode("\n", $value->getValue()) as $row) { |
| 116 |
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row); |
| 117 |
} |
| 118 |
return $output; |
| 119 |
} |
| 120 |
if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) { |
| 121 |
return $output . ' ' . $this->dump($value->getValue(), $inline - 1, 0, $flags) . "\n"; |
| 122 |
} |
| 123 |
return $output . "\n" . $this->dump($value->getValue(), $inline - 1, $indent, $flags); |
| 124 |
} |
| 125 |
private function getBlockIndentationIndicator(string $value): string |
| 126 |
{ |
| 127 |
$lines = explode("\n", $value); |
| 128 |
// If the first line (that is neither empty nor contains only spaces) |
| 129 |
// starts with a space character, the spec requires a block indentation indicator |
| 130 |
// http://www.yaml.org/spec/1.2/spec.html#id2793979 |
| 131 |
foreach ($lines as $line) { |
| 132 |
if ('' !== trim($line, ' ')) { |
| 133 |
return ' ' === substr($line, 0, 1) ? (string) $this->indentation : ''; |
| 134 |
} |
| 135 |
} |
| 136 |
return ''; |
| 137 |
} |
| 138 |
} |
| 139 |
|