Yaml.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\Exception\ParseException;
  12. /**
  13. * Yaml offers convenience methods to load and dump YAML.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @final since version 3.4
  18. */
  19. class Yaml
  20. {
  21. const DUMP_OBJECT = 1;
  22. const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
  23. const PARSE_OBJECT = 4;
  24. const PARSE_OBJECT_FOR_MAP = 8;
  25. const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
  26. const PARSE_DATETIME = 32;
  27. const DUMP_OBJECT_AS_MAP = 64;
  28. const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
  29. const PARSE_CONSTANT = 256;
  30. const PARSE_CUSTOM_TAGS = 512;
  31. const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
  32. /**
  33. * @deprecated since version 3.4, to be removed in 4.0. Quote your evaluable keys instead.
  34. */
  35. const PARSE_KEYS_AS_STRINGS = 2048;
  36. /**
  37. * Parses a YAML file into a PHP value.
  38. *
  39. * Usage:
  40. *
  41. * $array = Yaml::parseFile('config.yml');
  42. * print_r($array);
  43. *
  44. * @param string $filename The path to the YAML file to be parsed
  45. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  46. *
  47. * @return mixed The YAML converted to a PHP value
  48. *
  49. * @throws ParseException If the file could not be read or the YAML is not valid
  50. */
  51. public static function parseFile($filename, $flags = 0)
  52. {
  53. $yaml = new Parser();
  54. return $yaml->parseFile($filename, $flags);
  55. }
  56. /**
  57. * Parses YAML into a PHP value.
  58. *
  59. * Usage:
  60. * <code>
  61. * $array = Yaml::parse(file_get_contents('config.yml'));
  62. * print_r($array);
  63. * </code>
  64. *
  65. * @param string $input A string containing YAML
  66. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  67. *
  68. * @return mixed The YAML converted to a PHP value
  69. *
  70. * @throws ParseException If the YAML is not valid
  71. */
  72. public static function parse($input, $flags = 0)
  73. {
  74. if (\is_bool($flags)) {
  75. @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 PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
  76. if ($flags) {
  77. $flags = self::PARSE_EXCEPTION_ON_INVALID_TYPE;
  78. } else {
  79. $flags = 0;
  80. }
  81. }
  82. if (\func_num_args() >= 3) {
  83. @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 PARSE_OBJECT flag instead.', E_USER_DEPRECATED);
  84. if (func_get_arg(2)) {
  85. $flags |= self::PARSE_OBJECT;
  86. }
  87. }
  88. if (\func_num_args() >= 4) {
  89. @trigger_error('Passing a boolean flag to toggle object for map support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead.', E_USER_DEPRECATED);
  90. if (func_get_arg(3)) {
  91. $flags |= self::PARSE_OBJECT_FOR_MAP;
  92. }
  93. }
  94. $yaml = new Parser();
  95. return $yaml->parse($input, $flags);
  96. }
  97. /**
  98. * Dumps a PHP value to a YAML string.
  99. *
  100. * The dump method, when supplied with an array, will do its best
  101. * to convert the array into friendly YAML.
  102. *
  103. * @param mixed $input The PHP value
  104. * @param int $inline The level where you switch to inline YAML
  105. * @param int $indent The amount of spaces to use for indentation of nested nodes
  106. * @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
  107. *
  108. * @return string A YAML string representing the original PHP value
  109. */
  110. public static function dump($input, $inline = 2, $indent = 4, $flags = 0)
  111. {
  112. if (\is_bool($flags)) {
  113. @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 DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
  114. if ($flags) {
  115. $flags = self::DUMP_EXCEPTION_ON_INVALID_TYPE;
  116. } else {
  117. $flags = 0;
  118. }
  119. }
  120. if (\func_num_args() >= 5) {
  121. @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 DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
  122. if (func_get_arg(4)) {
  123. $flags |= self::DUMP_OBJECT;
  124. }
  125. }
  126. $yaml = new Dumper($indent);
  127. return $yaml->dump($input, $inline, 0, $flags);
  128. }
  129. }