Dumper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. /**
  12. * Dumper dumps PHP variables to YAML strings.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @final since version 3.4
  17. */
  18. class Dumper
  19. {
  20. /**
  21. * The amount of spaces to use for indentation of nested nodes.
  22. *
  23. * @var int
  24. */
  25. protected $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. * Sets the indentation.
  38. *
  39. * @param int $num The amount of spaces to use for indentation of nested nodes
  40. *
  41. * @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead.
  42. */
  43. public function setIndentation($num)
  44. {
  45. @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);
  46. $this->indentation = (int) $num;
  47. }
  48. /**
  49. * Dumps a PHP value to YAML.
  50. *
  51. * @param mixed $input The PHP value
  52. * @param int $inline The level where you switch to inline YAML
  53. * @param int $indent The level of indentation (used internally)
  54. * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
  55. *
  56. * @return string The YAML representation of the PHP value
  57. */
  58. public function dump($input, $inline = 0, $indent = 0, $flags = 0)
  59. {
  60. if (\is_bool($flags)) {
  61. @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);
  62. if ($flags) {
  63. $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
  64. } else {
  65. $flags = 0;
  66. }
  67. }
  68. if (\func_num_args() >= 5) {
  69. @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);
  70. if (func_get_arg(4)) {
  71. $flags |= Yaml::DUMP_OBJECT;
  72. }
  73. }
  74. $output = '';
  75. $prefix = $indent ? str_repeat(' ', $indent) : '';
  76. $dumpObjectAsInlineMap = true;
  77. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
  78. $dumpObjectAsInlineMap = empty((array) $input);
  79. }
  80. if ($inline <= 0 || (!\is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
  81. $output .= $prefix.Inline::dump($input, $flags);
  82. } else {
  83. $dumpAsMap = Inline::isHash($input);
  84. foreach ($input as $key => $value) {
  85. if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r\n")) {
  86. // If the first line starts with a space character, the spec requires a blockIndicationIndicator
  87. // http://www.yaml.org/spec/1.2/spec.html#id2793979
  88. $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
  89. $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);
  90. foreach (preg_split('/\n|\r\n/', $value) as $row) {
  91. $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
  92. }
  93. continue;
  94. }
  95. $dumpObjectAsInlineMap = true;
  96. if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
  97. $dumpObjectAsInlineMap = empty((array) $value);
  98. }
  99. $willBeInlined = $inline - 1 <= 0 || !\is_array($value) && $dumpObjectAsInlineMap || empty($value);
  100. $output .= sprintf('%s%s%s%s',
  101. $prefix,
  102. $dumpAsMap ? Inline::dump($key, $flags).':' : '-',
  103. $willBeInlined ? ' ' : "\n",
  104. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
  105. ).($willBeInlined ? "\n" : '');
  106. }
  107. }
  108. return $output;
  109. }
  110. }