[PLUGINS] +yaml
[ptitvelo/web/www.git] / www / plugins / yaml / sfyaml / sfYaml.php
1 <?php
2
3 /*
4 * This file is part of the symfony package.
5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.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
11 /**
12 * sfYaml offers convenience methods to load and dump YAML.
13 *
14 * @package symfony
15 * @subpackage 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 $
18 */
19 class sfYaml
20 {
21 static protected
22 $spec = '1.2';
23
24 /**
25 * Sets the YAML specification version to use.
26 *
27 * @param string $version The YAML specification version
28 */
29 static public function setSpecVersion($version)
30 {
31 if (!in_array($version, array('1.1', '1.2')))
32 {
33 throw new InvalidArgumentException(sprintf('Version %s of the YAML specifications is not supported', $version));
34 }
35
36 self::$spec = $version;
37 }
38
39 /**
40 * Gets the YAML specification version to use.
41 *
42 * @return string The YAML specification version
43 */
44 static public function getSpecVersion()
45 {
46 return self::$spec;
47 }
48
49 /**
50 * Loads YAML into a PHP array.
51 *
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.
54 *
55 * Usage:
56 * <code>
57 * $array = sfYaml::load('config.yml');
58 * print_r($array);
59 * </code>
60 *
61 * @param string $input Path of YAML file or string containing YAML
62 *
63 * @return array The YAML converted to a PHP array
64 *
65 * @throws InvalidArgumentException If the YAML is not valid
66 */
67 public static function load($input)
68 {
69 $file = '';
70
71 // if input is a file, load it
72 if (strpos($input, "\n") === false && is_file($input))
73 {
74 $file = $input;
75
76 $content = $yaml = file_get_contents($input);
77
78 // if the file contains valid PHP, process it
79 if (strpos($content, '<'.'?') !== false
80 AND !(defined('_YAML_EVAL_PHP') AND !_YAML_EVAL_PHP))
81 {
82 ob_start();
83 $retval = eval('?'.'>'.$yaml);
84 $content = ob_get_clean();
85 // syntax error?
86 if ($retval === FALSE)
87 $content = $yaml;
88 }
89
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;
92 }
93
94 // if an array is returned by the config file assume it's in plain php form else in YAML
95 if (is_array($input))
96 {
97 return $input;
98 }
99
100 require_once dirname(__FILE__).'/sfYamlParser.php';
101
102 $yaml = new sfYamlParser();
103
104 try
105 {
106 $ret = $yaml->parse($input);
107 }
108 catch (Exception $e)
109 {
110 throw new InvalidArgumentException(sprintf('Unable to parse %s: %s', $file ? sprintf('file "%s"', $file) : 'string', $e->getMessage()));
111 }
112
113 return $ret;
114 }
115
116 /**
117 * Dumps a PHP array to a YAML string.
118 *
119 * The dump method, when supplied with an array, will do its best
120 * to convert the array into friendly YAML.
121 *
122 * @param array $array PHP array
123 * @param integer $inline The level where you switch to inline YAML
124 *
125 * @return string A YAML string representing the original PHP array
126 */
127 public static function dump($array, $inline = 2)
128 {
129 require_once dirname(__FILE__).'/sfYamlDumper.php';
130
131 $yaml = new sfYamlDumper();
132
133 return $yaml->dump($array, $inline);
134 }
135 }
136
137 /**
138 * Wraps echo to automatically provide a newline.
139 *
140 * @param string $string The string to echo with new line
141 */
142 function echoln($string)
143 {
144 echo $string."\n";
145 }