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.
11 if (!defined('_ECRIRE_INC_VERSION')) return;
14 * sfYaml offers convenience methods to load and dump YAML.
18 * @author Fabien Potencier <fabien.potencier@symfony-project.com>
19 * @version SVN: $Id: sfYaml.class.php 8988 2008-05-15 20:24:26Z fabien $
27 * Sets the YAML specification version to use.
29 * @param string $version The YAML specification version
31 static public function setSpecVersion($version)
33 if (!in_array($version, array('1.1', '1.2')))
35 throw new InvalidArgumentException(sprintf('Version %s of the YAML specifications is not supported', $version));
38 self
::$spec = $version;
42 * Gets the YAML specification version to use.
44 * @return string The YAML specification version
46 static public function getSpecVersion()
52 * Loads YAML into a PHP array.
54 * The load method, when supplied with a YAML stream (string or file),
55 * will do its best to convert YAML in a file into a PHP array.
59 * $array = sfYaml::load('config.yml');
63 * @param string $input Path of YAML file or string containing YAML
65 * @return array The YAML converted to a PHP array
67 * @throws InvalidArgumentException If the YAML is not valid
69 public static function load($input)
73 // if input is a file, load it
74 if (strpos($input, "\n") === false && is_file($input))
78 $content = $yaml = file_get_contents($input);
80 // if the file contains valid PHP, process it
81 if (strpos($content, '<'.'?') !== false
82 AND !(defined('_YAML_EVAL_PHP') AND !_YAML_EVAL_PHP
))
85 $retval = eval('?'.'>'.$yaml);
86 $content = ob_get_clean();
88 if ($retval === FALSE)
92 // if an array is returned by the config file assume it's in plain php form else in YAML
93 $input = is_array($retval) ?
$retval : $content;
96 // if an array is returned by the config file assume it's in plain php form else in YAML
102 require_once dirname(__FILE__
).'/sfYamlParser.php';
104 $yaml = new sfYamlParser();
108 $ret = $yaml->parse($input);
112 throw new InvalidArgumentException(sprintf('Unable to parse %s: %s', $file ?
sprintf('file "%s"', $file) : 'string', $e->getMessage()));
119 * Dumps a PHP array to a YAML string.
121 * The dump method, when supplied with an array, will do its best
122 * to convert the array into friendly YAML.
124 * @param array $array PHP array
125 * @param integer $inline The level where you switch to inline YAML
127 * @return string A YAML string representing the original PHP array
129 public static function dump($array, $inline = 2)
131 require_once dirname(__FILE__
).'/sfYamlDumper.php';
133 $yaml = new sfYamlDumper();
135 return $yaml->dump($array, $inline);
140 * Wraps echo to automatically provide a newline.
142 * @param string $string The string to echo with new line
144 function echoln($string)