PageRenderTime 36ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/lib/yaml/lib/sfYaml.php

http://github.com/bobthecow/mustache.php
PHP | 135 lines | 55 code | 18 blank | 62 comment | 4 complexity | 2b7116a9f3bf6f9e8cd914c81bf79830 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfYaml offers convenience methods to load and dump YAML.
  11. *
  12. * @package symfony
  13. * @subpackage yaml
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfYaml.class.php 8988 2008-05-15 20:24:26Z fabien $
  16. */
  17. class sfYaml
  18. {
  19. static protected
  20. $spec = '1.2';
  21. /**
  22. * Sets the YAML specification version to use.
  23. *
  24. * @param string $version The YAML specification version
  25. */
  26. static public function setSpecVersion($version)
  27. {
  28. if (!in_array($version, array('1.1', '1.2')))
  29. {
  30. throw new InvalidArgumentException(sprintf('Version %s of the YAML specifications is not supported', $version));
  31. }
  32. self::$spec = $version;
  33. }
  34. /**
  35. * Gets the YAML specification version to use.
  36. *
  37. * @return string The YAML specification version
  38. */
  39. static public function getSpecVersion()
  40. {
  41. return self::$spec;
  42. }
  43. /**
  44. * Loads YAML into a PHP array.
  45. *
  46. * The load method, when supplied with a YAML stream (string or file),
  47. * will do its best to convert YAML in a file into a PHP array.
  48. *
  49. * Usage:
  50. * <code>
  51. * $array = sfYaml::load('config.yml');
  52. * print_r($array);
  53. * </code>
  54. *
  55. * @param string $input Path of YAML file or string containing YAML
  56. *
  57. * @return array The YAML converted to a PHP array
  58. *
  59. * @throws InvalidArgumentException If the YAML is not valid
  60. */
  61. public static function load($input)
  62. {
  63. $file = '';
  64. // if input is a file, process it
  65. if (strpos($input, "\n") === false && is_file($input))
  66. {
  67. $file = $input;
  68. ob_start();
  69. $retval = include($input);
  70. $content = ob_get_clean();
  71. // if an array is returned by the config file assume it's in plain php form else in YAML
  72. $input = is_array($retval) ? $retval : $content;
  73. }
  74. // if an array is returned by the config file assume it's in plain php form else in YAML
  75. if (is_array($input))
  76. {
  77. return $input;
  78. }
  79. require_once dirname(__FILE__).'/sfYamlParser.php';
  80. $yaml = new sfYamlParser();
  81. try
  82. {
  83. $ret = $yaml->parse($input);
  84. }
  85. catch (Exception $e)
  86. {
  87. throw new InvalidArgumentException(sprintf('Unable to parse %s: %s', $file ? sprintf('file "%s"', $file) : 'string', $e->getMessage()));
  88. }
  89. return $ret;
  90. }
  91. /**
  92. * Dumps a PHP array to a YAML string.
  93. *
  94. * The dump method, when supplied with an array, will do its best
  95. * to convert the array into friendly YAML.
  96. *
  97. * @param array $array PHP array
  98. * @param integer $inline The level where you switch to inline YAML
  99. *
  100. * @return string A YAML string representing the original PHP array
  101. */
  102. public static function dump($array, $inline = 2)
  103. {
  104. require_once dirname(__FILE__).'/sfYamlDumper.php';
  105. $yaml = new sfYamlDumper();
  106. return $yaml->dump($array, $inline);
  107. }
  108. }
  109. /**
  110. * Wraps echo to automatically provide a newline.
  111. *
  112. * @param string $string The string to echo with new line
  113. */
  114. function echoln($string)
  115. {
  116. echo $string."\n";
  117. }