|
maxious
|
1 |
<?php |
|
|
2 |
/** |
|
|
3 |
* Spyc -- A Simple PHP YAML Class |
|
|
4 |
* @version 0.4.5 |
|
|
5 |
* @author Vlad Andersen <vlad.andersen@gmail.com> |
|
|
6 |
* @author Chris Wanstrath <chris@ozmm.org> |
|
|
7 |
* @link http://code.google.com/p/spyc/ |
|
|
8 |
* @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2009 Vlad Andersen |
|
|
9 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
|
|
10 |
* @package Spyc |
|
|
11 |
*/ |
|
|
12 |
|
|
|
13 |
if (!function_exists('spyc_load')) { |
|
|
14 |
/** |
|
|
15 |
* Parses YAML to array. |
|
|
16 |
* @param string $string YAML string. |
|
|
17 |
* @return array |
|
|
18 |
*/ |
|
|
19 |
function spyc_load ($string) { |
|
|
20 |
return Spyc::YAMLLoadString($string); |
|
|
21 |
} |
|
|
22 |
} |
|
|
23 |
|
|
|
24 |
if (!function_exists('spyc_load_file')) { |
|
|
25 |
/** |
|
|
26 |
* Parses YAML to array. |
|
|
27 |
* @param string $file Path to YAML file. |
|
|
28 |
* @return array |
|
|
29 |
*/ |
|
|
30 |
function spyc_load_file ($file) { |
|
|
31 |
return Spyc::YAMLLoad($file); |
|
|
32 |
} |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
/** |
|
|
36 |
* The Simple PHP YAML Class. |
|
|
37 |
* |
|
|
38 |
* This class can be used to read a YAML file and convert its contents |
|
|
39 |
* into a PHP array. It currently supports a very limited subsection of |
|
|
40 |
* the YAML spec. |
|
|
41 |
* |
|
|
42 |
* Usage: |
|
|
43 |
* <code> |
|
|
44 |
* $Spyc = new Spyc; |
|
|
45 |
* $array = $Spyc->load($file); |
|
|
46 |
* </code> |
|
|
47 |
* or: |
|
|
48 |
* <code> |
|
|
49 |
* $array = Spyc::YAMLLoad($file); |
|
|
50 |
* </code> |
|
|
51 |
* or: |
|
|
52 |
* <code> |
|
|
53 |
* $array = spyc_load_file($file); |
|
|
54 |
* </code> |
|
|
55 |
* @package Spyc |
|
|
56 |
*/ |
|
|
57 |
class Spyc { |
|
|
58 |
|
|
|
59 |
// SETTINGS |
|
|
60 |
|
|
|
61 |
/** |
|
|
62 |
* Setting this to true will force YAMLDump to enclose any string value in |
|
|
63 |
* quotes. False by default. |
|
|
64 |
* |
|
|
65 |
* @var bool |
|
|
66 |
*/ |
|
|
67 |
var $setting_dump_force_quotes = false; |
|
|
68 |
|
|
|
69 |
/** |
|
|
70 |
* Setting this to true will forse YAMLLoad to use syck_load function when |
|
|
71 |
* possible. False by default. |
|
|
72 |
* @var bool |
|
|
73 |
*/ |
|
|
74 |
var $setting_use_syck_is_possible = false; |
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
/**#@+ |
|
|
79 |
* @access private |
|
|
80 |
* @var mixed |
|
|
81 |
*/ |
|
|
82 |
var $_dumpIndent; |
|
|
83 |
var $_dumpWordWrap; |
|
|
84 |
var $_containsGroupAnchor = false; |
|
|
85 |
var $_containsGroupAlias = false; |
|
|
86 |
var $path; |
|
|
87 |
var $result; |
|
|
88 |
var $LiteralPlaceHolder = '___YAML_Literal_Block___'; |
|
|
89 |
var $SavedGroups = array(); |
|
|
90 |
var $indent; |
|
|
91 |
/** |
|
|
92 |
* Path modifier that should be applied after adding current element. |
|
|
93 |
* @var array |
|
|
94 |
*/ |
|
|
95 |
var $delayedPath = array(); |
|
|
96 |
|
|
|
97 |
/**#@+ |
|
|
98 |
* @access public |
|
|
99 |
* @var mixed |
|
|
100 |
*/ |
|
|
101 |
var $_nodeId; |
|
|
102 |
|
|
|
103 |
/** |
|
|
104 |
* Load a valid YAML string to Spyc. |
|
|
105 |
* @param string $input |
|
|
106 |
* @return array |
|
|
107 |
*/ |
|
|
108 |
function load ($input) { |
|
|
109 |
return $this->__loadString($input); |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
/** |
|
|
113 |
* Load a valid YAML file to Spyc. |
|
|
114 |
* @param string $file |
|
|
115 |
* @return array |
|
|
116 |
*/ |
|
|
117 |
function loadFile ($file) { |
|
|
118 |
return $this->__load($file); |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
/** |
|
|
122 |
* Load YAML into a PHP array statically |
|
|
123 |
* |
|
|
124 |
* The load method, when supplied with a YAML stream (string or file), |
|
|
125 |
* will do its best to convert YAML in a file into a PHP array. Pretty |
|
|
126 |
* simple. |
|
|
127 |
* Usage: |
|
|
128 |
* <code> |
|
|
129 |
* $array = Spyc::YAMLLoad('lucky.yaml'); |
|
|
130 |
* print_r($array); |
|
|
131 |
* </code> |
|
|
132 |
* @access public |
|
|
133 |
* @return array |
|
|
134 |
* @param string $input Path of YAML file or string containing YAML |
|
|
135 |
*/ |
|
|
136 |
function YAMLLoad($input) { |
|
|
137 |
$Spyc = new Spyc; |
|
|
138 |
return $Spyc->__load($input); |
|
|
139 |
} |
|
|
140 |
|
|
|
141 |
/** |
|
|
142 |
* Load a string of YAML into a PHP array statically |
|
|
143 |
* |
|
|
144 |
* The load method, when supplied with a YAML string, will do its best |
|
|
145 |
* to convert YAML in a string into a PHP array. Pretty simple. |
|
|
146 |
* |
|
|
147 |
* Note: use this function if you don't want files from the file system |
|
|
148 |
* loaded and processed as YAML. This is of interest to people concerned |
|
|
149 |
* about security whose input is from a string. |
|
|
150 |
* |
|
|
151 |
* Usage: |
|
|
152 |
* <code> |
|
|
153 |
* $array = Spyc::YAMLLoadString("---\n0: hello world\n"); |
|
|
154 |
* print_r($array); |
|
|
155 |
* </code> |
|
|
156 |
* @access public |
|
|
157 |
* @return array |
|
|
158 |
* @param string $input String containing YAML |
|
|
159 |
*/ |
|
|
160 |
function YAMLLoadString($input) { |
|
|
161 |
$Spyc = new Spyc; |
|
|
162 |
return $Spyc->__loadString($input); |
|
|
163 |
} |
|
|
164 |
|
|
|
165 |
/** |
|
|
166 |
* Dump YAML from PHP array statically |
|
|
167 |
* |
|
|
168 |
* The dump method, when supplied with an array, will do its best |
|
|
169 |
* to convert the array into friendly YAML. Pretty simple. Feel free to |
|
|
170 |
* save the returned string as nothing.yaml and pass it around. |
|
|
171 |
* |
|
|
172 |
* Oh, and you can decide how big the indent is and what the wordwrap |
|
|
173 |
* for folding is. Pretty cool -- just pass in 'false' for either if |
|
|
174 |
* you want to use the default. |
|
|
175 |
* |
|
|
176 |
* Indent's default is 2 spaces, wordwrap's default is 40 characters. And |
|
|
177 |
* you can turn off wordwrap by passing in 0. |
|
|
178 |
* |
|
|
179 |
* @access public |
|
|
180 |
* @return string |
|
|
181 |
* @param array $array PHP array |
|
|
182 |
* @param int $indent Pass in false to use the default, which is 2 |
|
|
183 |
* @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
|
|
184 |
*/ |
|
|
185 |
function YAMLDump($array,$indent = false,$wordwrap = false) { |
|
|
186 |
$spyc = new Spyc; |
|
|
187 |
return $spyc->dump($array,$indent,$wordwrap); |
|
|
188 |
} |
|
|
189 |
|
|
|
190 |
|
|
|
191 |
/** |
|
|
192 |
* Dump PHP array to YAML |
|
|
193 |
* |
|
|
194 |
* The dump method, when supplied with an array, will do its best |
|
|
195 |
* to convert the array into friendly YAML. Pretty simple. Feel free to |
|
|
196 |
* save the returned string as tasteful.yaml and pass it around. |
|
|
197 |
* |
|
|
198 |
* Oh, and you can decide how big the indent is and what the wordwrap |
|
|
199 |
* for folding is. Pretty cool -- just pass in 'false' for either if |
|
|
200 |
* you want to use the default. |
|
|
201 |
* |
|
|
202 |
* Indent's default is 2 spaces, wordwrap's default is 40 characters. And |
|
|
203 |
* you can turn off wordwrap by passing in 0. |
|
|
204 |
* |
|
|
205 |
* @access public |
|
|
206 |
* @return string |
|
|
207 |
* @param array $array PHP array |
|
|
208 |
* @param int $indent Pass in false to use the default, which is 2 |
|
|
209 |
* @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
|
|
210 |
*/ |
|
|
211 |
function dump($array,$indent = false,$wordwrap = false) { |
|
|
212 |
// Dumps to some very clean YAML. We'll have to add some more features |
|
|
213 |
// and options soon. And better support for folding. |
|
|
214 |
|
|
|
215 |
// New features and options. |
|
|
216 |
if ($indent === false or !is_numeric($indent)) { |
|
|
217 |
$this->_dumpIndent = 2; |
|
|
218 |
} else { |
|
|
219 |
$this->_dumpIndent = $indent; |
|
|
220 |
} |
|
|
221 |
|
|
|
222 |
if ($wordwrap === false or !is_numeric($wordwrap)) { |
|
|
223 |
$this->_dumpWordWrap = 40; |
|
|
224 |
} else { |
|
|
225 |
$this->_dumpWordWrap = $wordwrap; |
|
|
226 |
} |
|
|
227 |
|
|
|
228 |
// New YAML document |
|
|
229 |
$string = "---\n"; |
|
|
230 |
|
|
|
231 |
// Start at the base of the array and move through it. |
|
|
232 |
if ($array) { |
|
|
233 |
$array = (array)$array; |
|