4 * This file is part of the symfony package.
5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * sfYaml offers convenience methods to load and dump YAML.
16 * @author Fabien Potencier <fabien.potencier@symfony-project.com>
17 * @version SVN: $Id: sfYaml.class.php 8988 2008-05-15 20:24:26Z fabien $
25 * Sets the YAML specification version to use.
27 * @param string $version The YAML specification version
29 static public function setSpecVersion($version)
31 if (!in_array($version, array('1.1', '1.2')))
33 throw new InvalidArgumentException(sprintf('Version %s of the YAML specifications is not supported', $version));
36 self
::$spec = $version;
40 * Gets the YAML specification version to use.
42 * @return string The YAML specification version
44 static public function getSpecVersion()
50 * Loads YAML into a PHP array.
52 * The load method, when supplied with a YAML stream (string or file),
53 * will do its best to convert YAML in a file into a PHP array.
57 * $array = sfYaml::load('config.yml');
61 * @param string $input Path of YAML file or string containing YAML
63 * @return array The YAML converted to a PHP array
65 * @throws InvalidArgumentException If the YAML is not valid
67 public static function load($input)
71 // if input is a file, load it
72 if (strpos($input, "\n") === false && is_file($input))
76 $content = $yaml = file_get_contents($input);
78 // if the file contains valid PHP, process it
79 if (strpos($content, '<'.'?') !== false
80 AND !(defined('_YAML_EVAL_PHP') AND !_YAML_EVAL_PHP
))
83 $retval = eval('?'.'>'.$yaml);
84 $content = ob_get_clean();
86 if ($retval === FALSE)
90 // if an array is returned by the config file assume it's in plain php form else in YAML
91 $input = is_array($retval) ?
$retval : $content;
94 // if an array is returned by the config file assume it's in plain php form else in YAML
100 require_once dirname(__FILE__
).'/sfYamlParser.php';
102 $yaml = new sfYamlParser();
106 $ret = $yaml->parse($input);
110 throw new InvalidArgumentException(sprintf('Unable to parse %s: %s', $file ?
sprintf('file "%s"', $file) : 'string', $e->getMessage()));
117 * Dumps a PHP array to a YAML string.
119 * The dump method, when supplied with an array, will do its best
120 * to convert the array into friendly YAML.
122 * @param array $array PHP array
123 * @param integer $inline The level where you switch to inline YAML
125 * @return string A YAML string representing the original PHP array
127 public static function dump($array, $inline = 2)
129 require_once dirname(__FILE__
).'/sfYamlDumper.php';
131 $yaml = new sfYamlDumper();
133 return $yaml->dump($array, $inline);
138 * Wraps echo to automatically provide a newline.
140 * @param string $string The string to echo with new line
142 function echoln($string)