Dumper.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 since version 3.4
  18. */
  19. class Dumper
  20. {
  21. /**
  22. * The amount of spaces to use for indentation of nested nodes.
  23. *
  24. * @var int
  25. */
  26. protected $indentation;
  27. /**
  28. * @param int $indentation
  29. */
  30. public function __construct($indentation = 4)
  31. {
  32. if ($indentation < 1) {
  33. throw new \InvalidArgumentException('The indentation must be greater than zero.');
  34. }
  35. $this->indentation = $indentation;
  36. }
  37. /**
  38. * Sets the indentation.
  39. *
  40. * @param int $num The amount of spaces to use for indentation of nested nodes
  41. *
  42. * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead.
  43. */
  44. public function setIndentation($num)
  45. {
  46. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 3.1 and will be removed in 4.0. Pass the indentation to the constructor instead.', E_USER_DEPRECATED);
  47. $this->indentation = (int) $num;
  48. }
  49. /**
  50. * Dumps a PHP value to YAML.
  51. *
  52. * @param mixed $input The PHP value
  53. * @param int $inline The level where you switch to inline YAML
  54. * @param int $indent The level of indentation (used internally)
  55. * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
  56. *
  57. * @return string The YAML representation of the PHP value
  58. */
  59. public function dump($input, $inline = 0, $indent = 0, $flags = 0)
  60. {
  61. if (\is_bool($flags)) {
  62. @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
  63. if ($flags) {
  64. $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
  65. } else {
  66. $flags = 0;
  67. }
  68. }
  69. if (\func_num_args() >= 5) {
  70. @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
  71. if (func_get_arg(4)) {
  72. $flags |= Yaml::DUMP_OBJECT;
  73. }
  74. }
  75. $output = '';
  76. $prefix = $indent ? str_repeat(' ', $indent) : '';
  77. $dumpObjectAsInlineMap = true;
  78. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
  79. $dumpObjectAsInlineMap = empty((array) $input);
  80. }
  81. if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || empty($input)) {
  82. $output .= $prefix.Inline::dump($input, $flags);
  83. } else {
  84. $dumpAsMap = Inline::isHash($input);
  85. foreach ($input as $key => $value) {
  86. if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) {
  87. // If the first line starts with a space character, the spec requires a blockIndicationIndicator
  88. // http://www.yaml.org/spec/1.2/spec.html#id2793979
  89. $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
  90. $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
  91. foreach (explode("\n", $value) as $row) {
  92. $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
  93. }
  94. continue;
  95. }
  96. if ($value instanceof TaggedValue) {
  97. $output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
  98. if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
  99. // If the first line starts with a space character, the spec requires a blockIndicationIndicator
  100. // http://www.yaml.org/spec/1.2/spec.html#id2793979
  101. $blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
  102. $output .= sprintf(" |%s\n", $blockIndentationIndicator);
  103. foreach (explode("\n", $value->getValue()) as $row) {
  104. $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
  105. }
  106. continue;
  107. }
  108. if ($inline - 1 <= 0 || null === $value->getValue() || is_scalar($value->getValue())) {
  109. $output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
  110. } else {
  111. $output .= "\n";
  112. $output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags);
  113. }
  114. continue;
  115. }
  116. $dumpObjectAsInlineMap = true;
  117. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
  118. $dumpObjectAsInlineMap = empty((array) $value);
  119. }
  120. $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
  121. $output .= sprintf('%s%s%s%s',
  122. $prefix,
  123. $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
  124. $willBeInlined ? ' ' : "\n",
  125. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
  126. ).($willBeInlined ? "\n" : '');
  127. }
  128. }
  129. return $output;
  130. }
  131. }