KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

YAML.php 4.7KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Horde YAML package
  4. *
  5. * This package is heavily inspired by the Spyc PHP YAML
  6. * implementation (http://spyc.sourceforge.net/), and portions are
  7. * copyright 2005-2006 Chris Wanstrath.
  8. *
  9. * @author Chris Wanstrath (chris@ozmm.org)
  10. * @author Chuck Hagenbuch (chuck@horde.org)
  11. * @author Mike Naberezny (mike@maintainable.com)
  12. * @license http://opensource.org/licenses/bsd-license.php BSD
  13. * @category Horde
  14. * @package Horde_Yaml
  15. */
  16. require "YAML/Dumper.php";
  17. require "YAML/Exception.php";
  18. require "YAML/Loader.php";
  19. require "YAML/Node.php";
  20. /**
  21. * Horde YAML parser.
  22. *
  23. * This class can be used to read a YAML file and convert its contents
  24. * into a PHP array. The native PHP parser supports a limited
  25. * subsection of the YAML spec, but if the syck extension is present,
  26. * that will be used for parsing.
  27. *
  28. * @category Horde
  29. * @package Horde_Yaml
  30. */
  31. class YAML
  32. {
  33. /**
  34. * Callback used for alternate YAML loader, typically exported
  35. * by a faster PHP extension. This function's first argument
  36. * must accept a string with YAML content.
  37. *
  38. * @var callback
  39. */
  40. public static $loadfunc = 'syck_load';
  41. /**
  42. * Whitelist of classes that can be instantiated automatically
  43. * when loading YAML docs that include serialized PHP objects.
  44. *
  45. * @var array
  46. */
  47. public static $allowedClasses = array('ArrayObject');
  48. /**
  49. * Load a string containing YAML and parse it into a PHP array.
  50. * Returns an empty array on failure.
  51. *
  52. * @param string $yaml String containing YAML
  53. * @return array PHP array representation of YAML content
  54. */
  55. public static function load($yaml)
  56. {
  57. if (@is_file($yaml))
  58. return self::loadFile($yaml);
  59. if (!is_string($yaml) || !strlen($yaml)) {
  60. $msg = 'YAML to parse must be a string and cannot be empty.';
  61. throw new InvalidArgumentException($msg);
  62. }
  63. if (is_callable(self::$loadfunc)) {
  64. $array = call_user_func(self::$loadfunc, $yaml);
  65. return is_array($array) ? $array : array();
  66. }
  67. if (strpos($yaml, "\r") !== false) {
  68. $yaml = str_replace(array("\r\n", "\r"), array("\n", "\n"), $yaml);
  69. }
  70. $lines = explode("\n", $yaml);
  71. $loader = new Horde_Yaml_Loader;
  72. while (list(,$line) = each($lines)) {
  73. $loader->parse($line);
  74. }
  75. return $loader->toArray();
  76. }
  77. /**
  78. * Load a file containing YAML and parse it into a PHP array.
  79. *
  80. * If the file cannot be opened, an exception is thrown. If the
  81. * file is read but parsing fails, an empty array is returned.
  82. *
  83. * @param string $filename Filename to load
  84. * @return array PHP array representation of YAML content
  85. * @throws IllegalArgumentException If $filename is invalid
  86. * @throws Horde_Yaml_Exception If the file cannot be opened.
  87. */
  88. public static function loadFile($filename)
  89. {
  90. if (!is_string($filename) || !strlen($filename)) {
  91. $msg = 'Filename must be a string and cannot be empty';
  92. throw new InvalidArgumentException($msg);
  93. }
  94. $stream = @fopen($filename, 'rb');
  95. if (!$stream) {
  96. throw new Horde_Yaml_Exception('Failed to open file: ', error_get_last());
  97. }
  98. return self::loadStream($stream);
  99. }
  100. /**
  101. * Load YAML from a PHP stream resource.
  102. *
  103. * @param resource $stream PHP stream resource
  104. * @return array PHP array representation of YAML content
  105. */
  106. public static function loadStream($stream)
  107. {
  108. if (! is_resource($stream) || get_resource_type($stream) != 'stream') {
  109. throw new InvalidArgumentException('Stream must be a stream resource');
  110. }
  111. if (is_callable(self::$loadfunc)) {
  112. $array = call_user_func(self::$loadfunc, stream_get_contents($stream));
  113. return is_array($array) ? $array : array();
  114. }
  115. $loader = new Horde_Yaml_Loader;
  116. while (!feof($stream)) {
  117. $loader->parse(stream_get_line($stream, 100000, "\n"));
  118. }
  119. return $loader->toArray();
  120. }
  121. /**
  122. * Dump a PHP array to YAML.
  123. *
  124. * The dump method, when supplied with an array, will do its best
  125. * to convert the array into friendly YAML.
  126. *
  127. * @param array|Traversable $array PHP array or traversable object
  128. * @param integer $options Options to pass to dumper
  129. * @return string YAML representation of $value
  130. */
  131. public static function dump($value, $options = array())
  132. {
  133. $dumper = new Horde_Yaml_Dumper;
  134. return $dumper->dump($value, $options);
  135. }
  136. }