Yaml.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <[email protected]>
  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 <[email protected]>
  16. *
  17. * @api
  18. */
  19. class Yaml
  20. {
  21. public static $enablePhpParsing = false;
  22. public static function enablePhpParsing()
  23. {
  24. self::$enablePhpParsing = true;
  25. }
  26. /**
  27. * Parses YAML into a PHP array.
  28. *
  29. * The parse method, when supplied with a YAML stream (string or file),
  30. * will do its best to convert YAML in a file into a PHP array.
  31. *
  32. * Usage:
  33. * <code>
  34. * $array = Yaml::parse('config.yml');
  35. * print_r($array);
  36. * </code>
  37. *
  38. * @param string $input Path to a YAML file or a string containing YAML
  39. *
  40. * @return array The YAML converted to a PHP array
  41. *
  42. * @throws ParseException If the YAML is not valid
  43. *
  44. * @api
  45. */
  46. public static function parse($input)
  47. {
  48. // if input is a file, process it
  49. $file = '';
  50. if (strpos($input, "\n") === false && is_file($input)) {
  51. if (false === is_readable($input)) {
  52. throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
  53. }
  54. $file = $input;
  55. if (self::$enablePhpParsing) {
  56. ob_start();
  57. $retval = include($file);
  58. $content = ob_get_clean();
  59. // if an array is returned by the config file assume it's in plain php form else in YAML
  60. $input = is_array($retval) ? $retval : $content;
  61. // if an array is returned by the config file assume it's in plain php form else in YAML
  62. if (is_array($input)) {
  63. return $input;
  64. }
  65. } else {
  66. $input = file_get_contents($file);
  67. }
  68. }
  69. $yaml = new Parser();
  70. try {
  71. return $yaml->parse($input);
  72. } catch (ParseException $e) {
  73. if ($file) {
  74. $e->setParsedFile($file);
  75. }
  76. throw $e;
  77. }
  78. }
  79. /**
  80. * Dumps a PHP array to a YAML string.
  81. *
  82. * The dump method, when supplied with an array, will do its best
  83. * to convert the array into friendly YAML.
  84. *
  85. * @param array $array PHP array
  86. * @param integer $inline The level where you switch to inline YAML
  87. * @param integer $indent The amount of spaces to use for indentation of nested nodes.
  88. *
  89. * @return string A YAML string representing the original PHP array
  90. *
  91. * @api
  92. */
  93. public static function dump($array, $inline = 2, $indent = 4)
  94. {
  95. $yaml = new Dumper();
  96. $yaml->setIndentation($indent);
  97. return $yaml->dump($array, $inline);
  98. }
  99. }