暫無描述

Yaml.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. class Yaml
  18. {
  19. const DUMP_OBJECT = 1;
  20. const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
  21. const PARSE_OBJECT = 4;
  22. const PARSE_OBJECT_FOR_MAP = 8;
  23. const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
  24. const PARSE_DATETIME = 32;
  25. const DUMP_OBJECT_AS_MAP = 64;
  26. const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
  27. const PARSE_CONSTANT = 256;
  28. /**
  29. * Parses YAML into a PHP value.
  30. *
  31. * Usage:
  32. * <code>
  33. * $array = Yaml::parse(file_get_contents('config.yml'));
  34. * print_r($array);
  35. * </code>
  36. *
  37. * @param string $input A string containing YAML
  38. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  39. *
  40. * @return mixed The YAML converted to a PHP value
  41. *
  42. * @throws ParseException If the YAML is not valid
  43. */
  44. public static function parse($input, $flags = 0)
  45. {
  46. if (is_bool($flags)) {
  47. @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
  48. if ($flags) {
  49. $flags = self::PARSE_EXCEPTION_ON_INVALID_TYPE;
  50. } else {
  51. $flags = 0;
  52. }
  53. }
  54. if (func_num_args() >= 3) {
  55. @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the PARSE_OBJECT flag instead.', E_USER_DEPRECATED);
  56. if (func_get_arg(2)) {
  57. $flags |= self::PARSE_OBJECT;
  58. }
  59. }
  60. if (func_num_args() >= 4) {
  61. @trigger_error('Passing a boolean flag to toggle object for map support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead.', E_USER_DEPRECATED);
  62. if (func_get_arg(3)) {
  63. $flags |= self::PARSE_OBJECT_FOR_MAP;
  64. }
  65. }
  66. $yaml = new Parser();
  67. return $yaml->parse($input, $flags);
  68. }
  69. /**
  70. * Dumps a PHP value to a YAML string.
  71. *
  72. * The dump method, when supplied with an array, will do its best
  73. * to convert the array into friendly YAML.
  74. *
  75. * @param mixed $input The PHP value
  76. * @param int $inline The level where you switch to inline YAML
  77. * @param int $indent The amount of spaces to use for indentation of nested nodes
  78. * @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
  79. *
  80. * @return string A YAML string representing the original PHP value
  81. */
  82. public static function dump($input, $inline = 2, $indent = 4, $flags = 0)
  83. {
  84. if (is_bool($flags)) {
  85. @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
  86. if ($flags) {
  87. $flags = self::DUMP_EXCEPTION_ON_INVALID_TYPE;
  88. } else {
  89. $flags = 0;
  90. }
  91. }
  92. if (func_num_args() >= 5) {
  93. @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
  94. if (func_get_arg(4)) {
  95. $flags |= self::DUMP_OBJECT;
  96. }
  97. }
  98. $yaml = new Dumper($indent);
  99. return $yaml->dump($input, $inline, 0, $flags);
  100. }
  101. }