Yaml.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * @api
  18. */
  19. class Yaml
  20. {
  21. /**
  22. * Be warned that PHP support will be removed in Symfony 2.3.
  23. *
  24. * @deprecated Deprecated since version 2.0, to be removed in 2.3.
  25. */
  26. public static $enablePhpParsing = false;
  27. /**
  28. * Enables PHP support when parsing YAML files.
  29. *
  30. * Be warned that PHP support will be removed in Symfony 2.3.
  31. *
  32. * @deprecated Deprecated since version 2.0, to be removed in 2.3.
  33. */
  34. public static function enablePhpParsing()
  35. {
  36. self::$enablePhpParsing = true;
  37. }
  38. /**
  39. * Sets the PHP support flag when parsing YAML files.
  40. *
  41. * Be warned that PHP support will be removed in Symfony 2.3.
  42. *
  43. * @param Boolean $boolean true if PHP parsing support is enabled, false otherwise
  44. *
  45. * @deprecated Deprecated since version 2.0, to be removed in 2.3.
  46. */
  47. public static function setPhpParsing($boolean)
  48. {
  49. self::$enablePhpParsing = (Boolean) $boolean;
  50. }
  51. /**
  52. * Checks if PHP support is enabled when parsing YAML files.
  53. *
  54. * Be warned that PHP support will be removed in Symfony 2.3.
  55. *
  56. * @return Boolean true if PHP parsing support is enabled, false otherwise
  57. *
  58. * @deprecated Deprecated since version 2.0, to be removed in 2.3.
  59. */
  60. public static function supportsPhpParsing()
  61. {
  62. return self::$enablePhpParsing;
  63. }
  64. /**
  65. * Parses YAML into a PHP array.
  66. *
  67. * The parse method, when supplied with a YAML stream (string or file),
  68. * will do its best to convert YAML in a file into a PHP array.
  69. *
  70. * Usage:
  71. * <code>
  72. * $array = Yaml::parse('config.yml');
  73. * print_r($array);
  74. * </code>
  75. *
  76. * @param string $input Path to a YAML file or a string containing YAML
  77. *
  78. * @return array The YAML converted to a PHP array
  79. *
  80. * @throws \InvalidArgumentException If the YAML is not valid
  81. *
  82. * @api
  83. */
  84. public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false)
  85. {
  86. // if input is a file, process it
  87. $file = '';
  88. if (strpos($input, "\n") === false && is_file($input)) {
  89. if (false === is_readable($input)) {
  90. throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
  91. }
  92. $file = $input;
  93. if (self::$enablePhpParsing) {
  94. ob_start();
  95. $retval = include($file);
  96. $content = ob_get_clean();
  97. // if an array is returned by the config file assume it's in plain php form else in YAML
  98. $input = is_array($retval) ? $retval : $content;
  99. // if an array is returned by the config file assume it's in plain php form else in YAML
  100. if (is_array($input)) {
  101. return $input;
  102. }
  103. } else {
  104. $input = file_get_contents($file);
  105. }
  106. }
  107. $yaml = new Parser();
  108. try {
  109. return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport);
  110. } catch (ParseException $e) {
  111. if ($file) {
  112. $e->setParsedFile($file);
  113. }
  114. throw $e;
  115. }
  116. }
  117. /**
  118. * Dumps a PHP array to a YAML string.
  119. *
  120. * The dump method, when supplied with an array, will do its best
  121. * to convert the array into friendly YAML.
  122. *
  123. * @param array $array PHP array
  124. * @param integer $inline The level where you switch to inline YAML
  125. * @param integer $indent The amount of spaces to use for indentation of nested nodes.
  126. * @param Boolean $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
  127. * @param Boolean $objectSupport true if object support is enabled, false otherwise
  128. *
  129. * @return string A YAML string representing the original PHP array
  130. *
  131. * @api
  132. */
  133. public static function dump($array, $inline = 2, $indent = 2, $exceptionOnInvalidType = false, $objectSupport = false)
  134. {
  135. $yaml = new Dumper();
  136. $yaml->setIndentation($indent);
  137. return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport);
  138. }
  139. }