Dumper.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml;
  11. use Symfony\Component\Yaml\Tag\TaggedValue;
  12. /**
  13. * Dumper dumps PHP variables to YAML strings.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @final
  18. */
  19. class Dumper
  20. {
  21. /**
  22. * The amount of spaces to use for indentation of nested nodes.
  23. * @var int
  24. */
  25. private $indentation;
  26. /**
  27. * @param int $indentation
  28. */
  29. public function __construct($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)
  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 (\is_int($key) && Yaml::DUMP_NUMERIC_KEY_AS_STRING & $flags) {
  63. $key = (string) $key;
  64. }
  65. if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && strpos($value, "\n") !== false && strpos($value, "\r") === false) {
  66. $blockIndentationIndicator = $this->getBlockIndentationIndicator($value);
  67. if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
  68. $blockChompingIndicator = '+';
  69. } elseif ("\n" === $value[-1]) {
  70. $blockChompingIndicator = '';
  71. } else {
  72. $blockChompingIndicator = '-';
  73. }
  74. $output .= sprintf('%s%s%s |%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator, $blockChompingIndicator);
  75. foreach (explode("\n", $value) as $row) {
  76. if ('' === $row) {
  77. $output .= "\n";
  78. } else {
  79. $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
  80. }
  81. }
  82. continue;
  83. }
  84. if ($value instanceof TaggedValue) {
  85. $output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
  86. if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && strpos($value->getValue(), "\n") !== false && strpos($value->getValue(), "\r\n") === false) {
  87. $blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
  88. $output .= sprintf(' |%s', $blockIndentationIndicator);
  89. foreach (explode("\n", $value->getValue()) as $row) {
  90. $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
  91. }
  92. continue;
  93. }
  94. if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
  95. $output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
  96. } else {
  97. $output .= "\n";
  98. $output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
  99. }
  100. continue;
  101. }
  102. $dumpObjectAsInlineMap = true;
  103. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
  104. $dumpObjectAsInlineMap = empty((array) $value);
  105. }
  106. $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
  107. $output .= sprintf('%s%s%s%s',
  108. $prefix,
  109. $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
  110. $willBeInlined ? ' ' : "\n",
  111. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
  112. ).($willBeInlined ? "\n" : '');
  113. }
  114. }
  115. return $output;
  116. }
  117. /**
  118. * @param \Symfony\Component\Yaml\Tag\TaggedValue $value
  119. * @param int $inline
  120. * @param int $indent
  121. * @param int $flags
  122. * @param string $prefix
  123. */
  124. private function dumpTaggedValue($value, $inline, $indent, $flags, $prefix)
  125. {
  126. $output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());
  127. if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && strpos($value->getValue(), "\n") !== false && strpos($value->getValue(), "\r\n") === false) {
  128. $blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
  129. $output .= sprintf(' |%s', $blockIndentationIndicator);
  130. foreach (explode("\n", $value->getValue()) as $row) {
  131. $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
  132. }
  133. return $output;
  134. }
  135. if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
  136. return $output.' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
  137. }
  138. return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
  139. }
  140. /**
  141. * @param string $value
  142. */
  143. private function getBlockIndentationIndicator($value)
  144. {
  145. $lines = explode("\n", $value);
  146. // If the first line (that is neither empty nor contains only spaces)
  147. // starts with a space character, the spec requires a block indentation indicator
  148. // http://www.yaml.org/spec/1.2/spec.html#id2793979
  149. foreach ($lines as $line) {
  150. if ('' !== trim($line, ' ')) {
  151. return (' ' === substr($line, 0, 1)) ? (string) $this->indentation : '';
  152. }
  153. }
  154. return '';
  155. }
  156. }