Yaml.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * Parses YAML into a PHP array.
  23. *
  24. * The parse method, when supplied with a YAML stream (string or file),
  25. * will do its best to convert YAML in a file into a PHP array.
  26. *
  27. * Usage:
  28. * <code>
  29. * $array = Yaml::parse('config.yml');
  30. * print_r($array);
  31. * </code>
  32. *
  33. * As this method accepts both plain strings and file names as an input,
  34. * you must validate the input before calling this method. Passing a file
  35. * as an input is a deprecated feature and will be removed in 3.0.
  36. *
  37. * @param string $input Path to a YAML file or a string containing YAML
  38. * @param Boolean $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
  39. * @param Boolean $objectSupport True if object support is enabled, false otherwise
  40. *
  41. * @return array The YAML converted to a PHP array
  42. *
  43. * @throws ParseException If the YAML is not valid
  44. *
  45. * @api
  46. */
  47. public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false)
  48. {
  49. // if input is a file, process it
  50. $file = '';
  51. if (strpos($input, "\n") === false && is_file($input)) {
  52. if (false === is_readable($input)) {
  53. throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
  54. }
  55. $file = $input;
  56. $input = file_get_contents($file);
  57. }
  58. $yaml = new Parser();
  59. try {
  60. return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport);
  61. } catch (ParseException $e) {
  62. if ($file) {
  63. $e->setParsedFile($file);
  64. }
  65. throw $e;
  66. }
  67. }
  68. /**
  69. * Dumps a PHP array to a YAML string.
  70. *
  71. * The dump method, when supplied with an array, will do its best
  72. * to convert the array into friendly YAML.
  73. *
  74. * @param array $array PHP array
  75. * @param integer $inline The level where you switch to inline YAML
  76. * @param integer $indent The amount of spaces to use for indentation of nested nodes.
  77. * @param Boolean $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
  78. * @param Boolean $objectSupport true if object support is enabled, false otherwise
  79. *
  80. * @return string A YAML string representing the original PHP array
  81. *
  82. * @api
  83. */
  84. public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
  85. {
  86. $yaml = new Dumper();
  87. $yaml->setIndentation($indent);
  88. return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport);
  89. }
  90. }