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