|
|
1
|
+<?php
|
|
|
2
|
+/**
|
|
|
3
|
+ * Spyc -- A Simple PHP YAML Class
|
|
|
4
|
+ * @version 0.5.1
|
|
|
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-2011 Vlad Andersen
|
|
|
9
|
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
10
|
+ * @package Spyc
|
|
|
11
|
+ */
|
|
|
12
|
+namespace Plugin;
|
|
|
13
|
+
|
|
|
14
|
+if (!function_exists('spyc_load')) {
|
|
|
15
|
+ /**
|
|
|
16
|
+ * Parses YAML to array.
|
|
|
17
|
+ * @param string $string YAML string.
|
|
|
18
|
+ * @return array
|
|
|
19
|
+ */
|
|
|
20
|
+ function spyc_load ($string) {
|
|
|
21
|
+ return Spyc::YAMLLoadString($string);
|
|
|
22
|
+ }
|
|
|
23
|
+}
|
|
|
24
|
+
|
|
|
25
|
+if (!function_exists('spyc_load_file')) {
|
|
|
26
|
+ /**
|
|
|
27
|
+ * Parses YAML to array.
|
|
|
28
|
+ * @param string $file Path to YAML file.
|
|
|
29
|
+ * @return array
|
|
|
30
|
+ */
|
|
|
31
|
+ function spyc_load_file ($file) {
|
|
|
32
|
+ return Spyc::YAMLLoad($file);
|
|
|
33
|
+ }
|
|
|
34
|
+}
|
|
|
35
|
+
|
|
|
36
|
+/**
|
|
|
37
|
+ * The Simple PHP YAML Class.
|
|
|
38
|
+ *
|
|
|
39
|
+ * This class can be used to read a YAML file and convert its contents
|
|
|
40
|
+ * into a PHP array. It currently supports a very limited subsection of
|
|
|
41
|
+ * the YAML spec.
|
|
|
42
|
+ *
|
|
|
43
|
+ * Usage:
|
|
|
44
|
+ * <code>
|
|
|
45
|
+ * $Spyc = new Spyc;
|
|
|
46
|
+ * $array = $Spyc->load($file);
|
|
|
47
|
+ * </code>
|
|
|
48
|
+ * or:
|
|
|
49
|
+ * <code>
|
|
|
50
|
+ * $array = Spyc::YAMLLoad($file);
|
|
|
51
|
+ * </code>
|
|
|
52
|
+ * or:
|
|
|
53
|
+ * <code>
|
|
|
54
|
+ * $array = spyc_load_file($file);
|
|
|
55
|
+ * </code>
|
|
|
56
|
+ * @package Spyc
|
|
|
57
|
+ */
|
|
|
58
|
+class Spyc {
|
|
|
59
|
+
|
|
|
60
|
+ // SETTINGS
|
|
|
61
|
+
|
|
|
62
|
+ const REMPTY = "\0\0\0\0\0";
|
|
|
63
|
+
|
|
|
64
|
+ /**
|
|
|
65
|
+ * Setting this to true will force YAMLDump to enclose any string value in
|
|
|
66
|
+ * quotes. False by default.
|
|
|
67
|
+ *
|
|
|
68
|
+ * @var bool
|
|
|
69
|
+ */
|
|
|
70
|
+ public $setting_dump_force_quotes = false;
|
|
|
71
|
+
|
|
|
72
|
+ /**
|
|
|
73
|
+ * Setting this to true will forse YAMLLoad to use syck_load function when
|
|
|
74
|
+ * possible. False by default.
|
|
|
75
|
+ * @var bool
|
|
|
76
|
+ */
|
|
|
77
|
+ public $setting_use_syck_is_possible = false;
|
|
|
78
|
+
|
|
|
79
|
+
|
|
|
80
|
+
|
|
|
81
|
+ /**#@+
|
|
|
82
|
+ * @access private
|
|
|
83
|
+ * @var mixed
|
|
|
84
|
+ */
|
|
|
85
|
+ private $_dumpIndent;
|
|
|
86
|
+ private $_dumpWordWrap;
|
|
|
87
|
+ private $_containsGroupAnchor = false;
|
|
|
88
|
+ private $_containsGroupAlias = false;
|
|
|
89
|
+ private $path;
|
|
|
90
|
+ private $result;
|
|
|
91
|
+ private $LiteralPlaceHolder = '___YAML_Literal_Block___';
|
|
|
92
|
+ private $SavedGroups = array();
|
|
|
93
|
+ private $indent;
|
|
|
94
|
+ /**
|
|
|
95
|
+ * Path modifier that should be applied after adding current element.
|
|
|
96
|
+ * @var array
|
|
|
97
|
+ */
|
|
|
98
|
+ private $delayedPath = array();
|
|
|
99
|
+
|
|
|
100
|
+ /**#@+
|
|
|
101
|
+ * @access public
|
|
|
102
|
+ * @var mixed
|
|
|
103
|
+ */
|
|
|
104
|
+ public $_nodeId;
|
|
|
105
|
+
|
|
|
106
|
+/**
|
|
|
107
|
+ * Load a valid YAML string to Spyc.
|
|
|
108
|
+ * @param string $input
|
|
|
109
|
+ * @return array
|
|
|
110
|
+ */
|
|
|
111
|
+ public function load ($input) {
|
|
|
112
|
+ return $this->__loadString($input);
|
|
|
113
|
+ }
|
|
|
114
|
+
|
|
|
115
|
+ /**
|
|
|
116
|
+ * Load a valid YAML file to Spyc.
|
|
|
117
|
+ * @param string $file
|
|
|
118
|
+ * @return array
|
|
|
119
|
+ */
|
|
|
120
|
+ public function loadFile ($file) {
|
|
|
121
|
+ return $this->__load($file);
|
|
|
122
|
+ }
|
|
|
123
|
+
|
|
|
124
|
+ /**
|
|
|
125
|
+ * Load YAML into a PHP array statically
|
|
|
126
|
+ *
|
|
|
127
|
+ * The load method, when supplied with a YAML stream (string or file),
|
|
|
128
|
+ * will do its best to convert YAML in a file into a PHP array. Pretty
|
|
|
129
|
+ * simple.
|
|
|
130
|
+ * Usage:
|
|
|
131
|
+ * <code>
|
|
|
132
|
+ * $array = Spyc::YAMLLoad('lucky.yaml');
|
|
|
133
|
+ * print_r($array);
|
|
|
134
|
+ * </code>
|
|
|
135
|
+ * @access public
|
|
|
136
|
+ * @return array
|
|
|
137
|
+ * @param string $input Path of YAML file or string containing YAML
|
|
|
138
|
+ */
|
|
|
139
|
+ public static function YAMLLoad($input) {
|
|
|
140
|
+ $Spyc = new Spyc;
|
|
|
141
|
+ return $Spyc->__load($input);
|
|
|
142
|
+ }
|
|
|
143
|
+
|
|
|
144
|
+ /**
|
|
|
145
|
+ * Load a string of YAML into a PHP array statically
|
|
|
146
|
+ *
|
|
|
147
|
+ * The load method, when supplied with a YAML string, will do its best
|
|
|
148
|
+ * to convert YAML in a string into a PHP array. Pretty simple.
|
|
|
149
|
+ *
|
|
|
150
|
+ * Note: use this function if you don't want files from the file system
|
|
|
151
|
+ * loaded and processed as YAML. This is of interest to people concerned
|
|
|
152
|
+ * about security whose input is from a string.
|
|
|
153
|
+ *
|
|
|
154
|
+ * Usage:
|
|
|
155
|
+ * <code>
|
|
|
156
|
+ * $array = Spyc::YAMLLoadString("---\n0: hello world\n");
|
|
|
157
|
+ * print_r($array);
|
|
|
158
|
+ * </code>
|
|
|
159
|
+ * @access public
|
|
|
160
|
+ * @return array
|
|
|
161
|
+ * @param string $input String containing YAML
|
|
|
162
|
+ */
|
|
|
163
|
+ public static function YAMLLoadString($input) {
|
|
|
164
|
+ $Spyc = new Spyc;
|
|
|
165
|
+ return $Spyc->__loadString($input);
|
|
|
166
|
+ }
|
|
|
167
|
+
|
|
|
168
|
+ /**
|
|
|
169
|
+ * Dump YAML from PHP array statically
|
|
|
170
|
+ *
|
|
|
171
|
+ * The dump method, when supplied with an array, will do its best
|
|
|
172
|
+ * to convert the array into friendly YAML. Pretty simple. Feel free to
|
|
|
173
|
+ * save the returned string as nothing.yaml and pass it around.
|
|
|
174
|
+ *
|
|
|
175
|
+ * Oh, and you can decide how big the indent is and what the wordwrap
|
|
|
176
|
+ * for folding is. Pretty cool -- just pass in 'false' for either if
|
|
|
177
|
+ * you want to use the default.
|
|
|
178
|
+ *
|
|
|
179
|
+ * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
|
|
|
180
|
+ * you can turn off wordwrap by passing in 0.
|
|
|
181
|
+ *
|
|
|
182
|
+ * @access public
|
|
|
183
|
+ * @return string
|
|
|
184
|
+ * @param array $array PHP array
|
|
|
185
|
+ * @param int $indent Pass in false to use the default, which is 2
|
|
|
186
|
+ * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
|
|
|
187
|
+ */
|
|
|
188
|
+ public static function YAMLDump($array,$indent = false,$wordwrap = false) {
|
|
|
189
|
+ $spyc = new Spyc;
|
|
|
190
|
+ return $spyc->dump($array,$indent,$wordwrap);
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+
|
|
|
194
|
+ /**
|
|
|
195
|
+ * Dump PHP array to YAML
|
|
|
196
|
+ *
|
|
|
197
|
+ * The dump method, when supplied with an array, will do its best
|
|
|
198
|
+ * to convert the array into friendly YAML. Pretty simple. Feel free to
|
|
|
199
|
+ * save the returned string as tasteful.yaml and pass it around.
|
|
|
200
|
+ *
|
|
|
201
|
+ * Oh, and you can decide how big the indent is and what the wordwrap
|
|
|
202
|
+ * for folding is. Pretty cool -- just pass in 'false' for either if
|
|
|
203
|
+ * you want to use the default.
|
|
|
204
|
+ *
|
|
|
205
|
+ * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
|
|
|
206
|
+ * you can turn off wordwrap by passing in 0.
|
|
|
207
|
+ *
|
|
|
208
|
+ * @access public
|
|
|
209
|
+ * @return string
|
|
|
210
|
+ * @param array $array PHP array
|
|
|
211
|
+ * @param int $indent Pass in false to use the default, which is 2
|
|
|
212
|
+ * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
|
|
|
213
|
+ */
|
|
|
214
|
+ public function dump($array,$indent = false,$wordwrap = false) {
|
|
|
215
|
+ // Dumps to some very clean YAML. We'll have to add some more features
|
|
|
216
|
+ // and options soon. And better support for folding.
|
|
|
217
|
+
|
|
|
218
|
+ // New features and options.
|
|
|
219
|
+ if ($indent === false or !is_numeric($indent)) {
|
|
|
220
|
+ $this->_dumpIndent = 2;
|
|
|
221
|
+ } else {
|
|
|
222
|
+ $this->_dumpIndent = $indent;
|
|
|
223
|
+ }
|
|
|
224
|
+
|
|
|
225
|
+ if ($wordwrap === false or !is_numeric($wordwrap)) {
|
|
|
226
|
+ $this->_dumpWordWrap = 40;
|
|
|
227
|
+ } else {
|
|
|
228
|
+ $this->_dumpWordWrap = $wordwrap;
|
|
|
229
|
+ }
|
|
|
230
|
+
|
|
|
231
|
+ // New YAML document
|
|
|
232
|
+ $string = "---\n";
|
|
|
233
|
+
|
|
|
234
|
+ // Start at the base of the array and move through it.
|
|
|
235
|
+ if ($array) {
|
|
|
236
|
+ $array = (array)$array;
|
|
|
237
|
+ $previous_key = -1;
|
|
|
238
|
+ foreach ($array as $key => $value) {
|
|
|
239
|
+ if (!isset($first_key)) $first_key = $key;
|
|
|
240
|
+ $string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array);
|
|
|
241
|
+ $previous_key = $key;
|
|
|
242
|
+ }
|
|
|
243
|
+ }
|
|
|
244
|
+ return $string;
|
|
|
245
|
+ }
|
|
|
246
|
+
|
|
|
247
|
+ /**
|
|
|
248
|
+ * Attempts to convert a key / value array item to YAML
|
|
|
249
|
+ * @access private
|
|
|
250
|
+ * @return string
|
|
|
251
|
+ * @param $key The name of the key
|
|
|
252
|
+ * @param $value The value of the item
|
|
|
253
|
+ * @param $indent The indent of the current node
|
|
|
254
|
+ */
|
|
|
255
|
+ private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) {
|
|
|
256
|
+ if (is_array($value)) {
|
|
|
257
|
+ if (empty ($value))
|
|
|
258
|
+ return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array);
|
|
|
259
|
+ // It has children. What to do?
|
|
|
260
|
+ // Make it the right kind of item
|
|
|
261
|
+ $string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array);
|
|
|
262
|
+ // Add the indent
|
|
|
263
|
+ $indent += $this->_dumpIndent;
|
|
|
264
|
+ // Yamlize the array
|
|
|
265
|
+ $string .= $this->_yamlizeArray($value,$indent);
|
|
|
266
|
+ } elseif (!is_array($value)) {
|
|
|
267
|
+ // It doesn't have children. Yip.
|
|
|
268
|
+ $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array);
|
|
|
269
|
+ }
|
|
|
270
|
+ return $string;
|
|
|
271
|
+ }
|
|
|
272
|
+
|
|
|
273
|
+ /**
|
|
|
274
|
+ * Attempts to convert an array to YAML
|
|
|
275
|
+ * @access private
|
|
|
276
|
+ * @return string
|
|
|
277
|
+ * @param $array The array you want to convert
|
|
|
278
|
+ * @param $indent The indent of the current level
|
|
|
279
|
+ */
|
|
|
280
|
+ private function _yamlizeArray($array,$indent) {
|
|
|
281
|
+ if (is_array($array)) {
|
|
|
282
|
+ $string = '';
|
|
|
283
|
+ $previous_key = -1;
|
|
|
284
|
+ foreach ($array as $key => $value) {
|
|
|
285
|
+ if (!isset($first_key)) $first_key = $key;
|
|
|
286
|
+ $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array);
|
|
|
287
|
+ $previous_key = $key;
|
|
|
288
|
+ }
|
|
|
289
|
+ return $string;
|
|
|
290
|
+ } else {
|
|
|
291
|
+ return false;
|
|
|
292
|
+ }
|
|
|
293
|
+ }
|
|
|
294
|
+
|
|
|
295
|
+ /**
|
|
|
296
|
+ * Returns YAML from a key and a value
|
|
|
297
|
+ * @access private
|
|
|
298
|
+ * @return string
|
|
|
299
|
+ * @param $key The name of the key
|
|
|
300
|
+ * @param $value The value of the item
|
|
|
301
|
+ * @param $indent The indent of the current node
|
|
|
302
|
+ */
|
|
|
303
|
+ private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) {
|
|
|
304
|
+ // do some folding here, for blocks
|
|
|
305
|
+ if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false ||
|
|
|
306
|
+ strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false || strpos ($value, ' ') !== false ||
|
|
|
307
|
+ strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || strpos($value,"&") !== false || strpos($value, "'") !== false || strpos($value, "!") === 0 ||
|
|
|
308
|
+ substr ($value, -1, 1) == ':')
|
|
|
309
|
+ ) {
|
|
|
310
|
+ $value = $this->_doLiteralBlock($value,$indent);
|
|
|
311
|
+ } else {
|
|
|
312
|
+ $value = $this->_doFolding($value,$indent);
|
|
|
313
|
+ }
|
|
|
314
|
+
|
|
|
315
|
+ if ($value === array()) $value = '[ ]';
|
|
|
316
|
+ if (in_array ($value, array ('true', 'TRUE', 'false', 'FALSE', 'y', 'Y', 'n', 'N', 'null', 'NULL'), true)) {
|
|
|
317
|
+ $value = $this->_doLiteralBlock($value,$indent);
|
|
|
318
|
+ }
|
|
|
319
|
+ if (trim ($value) != $value)
|
|
|
320
|
+ $value = $this->_doLiteralBlock($value,$indent);
|
|
|
321
|
+
|
|
|
322
|
+ if (is_bool($value)) {
|
|
|
323
|
+ $value = ($value) ? "true" : "false";
|
|
|
324
|
+ }
|
|
|
325
|
+
|
|
|
326
|
+ if ($value === null) $value = 'null';
|
|
|
327
|
+ if ($value === "'" . self::REMPTY . "'") $value = null;
|
|
|
328
|
+
|
|
|
329
|
+ $spaces = str_repeat(' ',$indent);
|
|
|
330
|
+
|
|
|
331
|
+ //if (is_int($key) && $key - 1 == $previous_key && $first_key===0) {
|
|
|
332
|
+ if (is_array ($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) {
|
|
|
333
|
+ // It's a sequence
|
|
|
334
|
+ $string = $spaces.'- '.$value."\n";
|
|
|
335
|
+ } else {
|
|
|
336
|
+ // if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"');
|
|
|
337
|
+ // It's mapped
|
|
|
338
|
+ if (strpos($key, ":") !== false || strpos($key, "#") !== false) { $key = '"' . $key . '"'; }
|
|
|
339
|
+ $string = rtrim ($spaces.$key.': '.$value)."\n";
|
|
|
340
|
+ }
|
|
|
341
|
+ return $string;
|
|
|
342
|
+ }
|
|
|
343
|
+
|
|
|
344
|
+ /**
|
|
|
345
|
+ * Creates a literal block for dumping
|
|
|
346
|
+ * @access private
|
|
|
347
|
+ * @return string
|
|
|
348
|
+ * @param $value
|
|
|
349
|
+ * @param $indent int The value of the indent
|
|
|
350
|
+ */
|
|
|
351
|
+ private function _doLiteralBlock($value,$indent) {
|
|
|
352
|
+ if ($value === "\n") return '\n';
|
|
|
353
|
+ if (strpos($value, "\n") === false && strpos($value, "'") === false) {
|
|
|
354
|
+ return sprintf ("'%s'", $value);
|
|
|
355
|
+ }
|
|
|
356
|
+ if (strpos($value, "\n") === false && strpos($value, '"') === false) {
|
|
|
357
|
+ return sprintf ('"%s"', $value);
|
|
|
358
|
+ }
|
|
|
359
|
+ $exploded = explode("\n",$value);
|
|
|
360
|
+ $newValue = '|';
|
|
|
361
|
+ $indent += $this->_dumpIndent;
|
|
|
362
|
+ $spaces = str_repeat(' ',$indent);
|
|
|
363
|
+ foreach ($exploded as $line) {
|
|
|
364
|
+ $newValue .= "\n" . $spaces . ($line);
|
|
|
365
|
+ }
|
|
|
366
|
+ return $newValue;
|
|
|
367
|
+ }
|
|
|
368
|
+
|
|
|
369
|
+ /**
|
|
|
370
|
+ * Folds a string of text, if necessary
|
|
|
371
|
+ * @access private
|
|
|
372
|
+ * @return string
|
|
|
373
|
+ * @param $value The string you wish to fold
|
|
|
374
|
+ */
|
|
|
375
|
+ private function _doFolding($value,$indent) {
|
|
|
376
|
+ // Don't do anything if wordwrap is set to 0
|
|
|
377
|
+
|
|
|
378
|
+ if ($this->_dumpWordWrap !== 0 && is_string ($value) && strlen($value) > $this->_dumpWordWrap) {
|
|
|
379
|
+ $indent += $this->_dumpIndent;
|
|
|
380
|
+ $indent = str_repeat(' ',$indent);
|
|
|
381
|
+ $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent");
|
|
|
382
|
+ $value = ">\n".$indent.$wrapped;
|
|
|
383
|
+ } else {
|
|
|
384
|
+ if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY)
|
|
|
385
|
+ $value = '"' . $value . '"';
|
|
|
386
|
+ }
|
|
|
387
|
+
|
|
|
388
|
+
|
|
|
389
|
+ return $value;
|
|
|
390
|
+ }
|
|
|
391
|
+
|
|
|
392
|
+// LOADING FUNCTIONS
|
|
|
393
|
+
|
|
|
394
|
+ private function __load($input) {
|
|
|
395
|
+ $Source = $this->loadFromSource($input);
|
|
|
396
|
+ return $this->loadWithSource($Source);
|
|
|
397
|
+ }
|
|
|
398
|
+
|
|
|
399
|
+ private function __loadString($input) {
|
|
|
400
|
+ $Source = $this->loadFromString($input);
|
|
|
401
|
+ return $this->loadWithSource($Source);
|
|
|
402
|
+ }
|
|
|
403
|
+
|
|
|
404
|
+ private function loadWithSource($Source) {
|
|
|
405
|
+ if (empty ($Source)) return array();
|
|
|
406
|
+ if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) {
|
|
|
407
|
+ $array = syck_load (implode ('', $Source));
|
|
|
408
|
+ return is_array($array) ? $array : array();
|
|
|
409
|
+ }
|
|
|
410
|
+
|
|
|
411
|
+ $this->path = array();
|
|
|
412
|
+ $this->result = array();
|
|
|
413
|
+
|
|
|
414
|
+ $cnt = count($Source);
|
|
|
415
|
+ for ($i = 0; $i < $cnt; $i++) {
|
|
|
416
|
+ $line = $Source[$i];
|
|
|
417
|
+
|
|
|
418
|
+ $this->indent = strlen($line) - strlen(ltrim($line));
|
|
|
419
|
+ $tempPath = $this->getParentPathByIndent($this->indent);
|
|
|
420
|
+ $line = self::stripIndent($line, $this->indent);
|
|
|
421
|
+ if (self::isComment($line)) continue;
|
|
|
422
|
+ if (self::isEmpty($line)) continue;
|
|
|
423
|
+ $this->path = $tempPath;
|
|
|
424
|
+
|
|
|
425
|
+ $literalBlockStyle = self::startsLiteralBlock($line);
|
|
|
426
|
+ if ($literalBlockStyle) {
|
|
|
427
|
+ $line = rtrim ($line, $literalBlockStyle . " \n");
|
|
|
428
|
+ $literalBlock = '';
|
|
|
429
|
+ $line .= $this->LiteralPlaceHolder;
|
|
|
430
|
+ $literal_block_indent = strlen($Source[$i+1]) - strlen(ltrim($Source[$i+1]));
|
|
|
431
|
+ while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) {
|
|
|
432
|
+ $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent);
|
|
|
433
|
+ }
|
|
|
434
|
+ $i--;
|
|
|
435
|
+ }
|
|
|
436
|
+
|
|
|
437
|
+ // Strip out comments
|
|
|
438
|
+ if (strpos ($line, '#')) {
|
|
|
439
|
+ $line = preg_replace('/\s*#([^"\']+)$/','',$line);
|
|
|
440
|
+ }
|
|
|
441
|
+
|
|
|
442
|
+ while (++$i < $cnt && self::greedilyNeedNextLine($line)) {
|
|
|
443
|
+ $line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t");
|
|
|
444
|
+ }
|
|
|
445
|
+ $i--;
|
|
|
446
|
+
|
|
|
447
|
+ $lineArray = $this->_parseLine($line);
|
|
|
448
|
+
|
|
|
449
|
+ if ($literalBlockStyle)
|
|
|
450
|
+ $lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock);
|
|
|
451
|
+
|
|
|
452
|
+ $this->addArray($lineArray, $this->indent);
|
|
|
453
|
+
|
|
|
454
|
+ foreach ($this->delayedPath as $indent => $delayedPath)
|
|
|
455
|
+ $this->path[$indent] = $delayedPath;
|
|
|
456
|
+
|
|
|
457
|
+ $this->delayedPath = array();
|
|
|
458
|
+
|
|
|
459
|
+ }
|
|
|
460
|
+ return $this->result;
|
|
|
461
|
+ }
|
|
|
462
|
+
|
|
|
463
|
+ private function loadFromSource ($input) {
|
|
|
464
|
+ if (!empty($input) && strpos($input, "\n") === false && file_exists($input))
|
|
|
465
|
+ return file($input);
|
|
|
466
|
+
|
|
|
467
|
+ return $this->loadFromString($input);
|
|
|
468
|
+ }
|
|
|
469
|
+
|
|
|
470
|
+ private function loadFromString ($input) {
|
|
|
471
|
+ $lines = explode("\n",$input);
|
|
|
472
|
+ foreach ($lines as $k => $_) {
|
|
|
473
|
+ $lines[$k] = rtrim ($_, "\r");
|
|
|
474
|
+ }
|
|
|
475
|
+ return $lines;
|
|
|
476
|
+ }
|
|
|
477
|
+
|
|
|
478
|
+ /**
|
|
|
479
|
+ * Parses YAML code and returns an array for a node
|
|
|
480
|
+ * @access private
|
|
|
481
|
+ * @return array
|
|
|
482
|
+ * @param string $line A line from the YAML file
|
|
|
483
|
+ */
|
|
|
484
|
+ private function _parseLine($line) {
|
|
|
485
|
+ if (!$line) return array();
|
|
|
486
|
+ $line = trim($line);
|
|
|
487
|
+ if (!$line) return array();
|
|
|
488
|
+
|
|
|
489
|
+ $array = array();
|
|
|
490
|
+
|
|
|
491
|
+ $group = $this->nodeContainsGroup($line);
|
|
|
492
|
+ if ($group) {
|
|
|
493
|
+ $this->addGroup($line, $group);
|
|
|
494
|
+ $line = $this->stripGroup ($line, $group);
|
|
|
495
|
+ }
|
|
|
496
|
+
|
|
|
497
|
+ if ($this->startsMappedSequence($line))
|
|
|
498
|
+ return $this->returnMappedSequence($line);
|
|
|
499
|
+
|
|
|
500
|
+ if ($this->startsMappedValue($line))
|
|
|
501
|
+ return $this->returnMappedValue($line);
|
|
|
502
|
+
|
|
|
503
|
+ if ($this->isArrayElement($line))
|
|
|
504
|
+ return $this->returnArrayElement($line);
|
|
|
505
|
+
|
|
|
506
|
+ if ($this->isPlainArray($line))
|
|
|
507
|
+ return $this->returnPlainArray($line);
|
|
|
508
|
+
|
|
|
509
|
+
|
|
|
510
|
+ return $this->returnKeyValuePair($line);
|
|
|
511
|
+
|
|
|
512
|
+ }
|
|
|
513
|
+
|
|
|
514
|
+ /**
|
|
|
515
|
+ * Finds the type of the passed value, returns the value as the new type.
|
|
|
516
|
+ * @access private
|
|
|
517
|
+ * @param string $value
|
|
|
518
|
+ * @return mixed
|
|
|
519
|
+ */
|
|
|
520
|
+ private function _toType($value) {
|
|
|
521
|
+ if ($value === '') return null;
|
|
|
522
|
+ $first_character = $value[0];
|
|
|
523
|
+ $last_character = substr($value, -1, 1);
|
|
|
524
|
+
|
|
|
525
|
+ $is_quoted = false;
|
|
|
526
|
+ do {
|
|
|
527
|
+ if (!$value) break;
|
|
|
528
|
+ if ($first_character != '"' && $first_character != "'") break;
|
|
|
529
|
+ if ($last_character != '"' && $last_character != "'") break;
|
|
|
530
|
+ $is_quoted = true;
|
|
|
531
|
+ } while (0);
|
|
|
532
|
+
|
|
|
533
|
+ if ($is_quoted)
|
|
|
534
|
+ return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\''));
|
|
|
535
|
+
|
|
|
536
|
+ if (strpos($value, ' #') !== false && !$is_quoted)
|
|
|
537
|
+ $value = preg_replace('/\s+#(.+)$/','',$value);
|
|
|
538
|
+
|
|
|
539
|
+ if (!$is_quoted) $value = str_replace('\n', "\n", $value);
|
|
|
540
|
+
|
|
|
541
|
+ if ($first_character == '[' && $last_character == ']') {
|
|
|
542
|
+ // Take out strings sequences and mappings
|
|
|
543
|
+ $innerValue = trim(substr ($value, 1, -1));
|
|
|
544
|
+ if ($innerValue === '') return array();
|
|
|
545
|
+ $explode = $this->_inlineEscape($innerValue);
|
|
|
546
|
+ // Propagate value array
|
|
|
547
|
+ $value = array();
|
|
|
548
|
+ foreach ($explode as $v) {
|
|
|
549
|
+ $value[] = $this->_toType($v);
|
|
|
550
|
+ }
|
|
|
551
|
+ return $value;
|
|
|
552
|
+ }
|
|
|
553
|
+
|
|
|
554
|
+ if (strpos($value,': ')!==false && $first_character != '{') {
|
|
|
555
|
+ $array = explode(': ',$value);
|
|
|
556
|
+ $key = trim($array[0]);
|
|
|
557
|
+ array_shift($array);
|
|
|
558
|
+ $value = trim(implode(': ',$array));
|
|
|
559
|
+ $value = $this->_toType($value);
|
|
|
560
|
+ return array($key => $value);
|
|
|
561
|
+ }
|
|
|
562
|
+
|
|
|
563
|
+ if ($first_character == '{' && $last_character == '}') {
|
|
|
564
|
+ $innerValue = trim(substr ($value, 1, -1));
|
|
|
565
|
+ if ($innerValue === '') return array();
|
|
|
566
|
+ // Inline Mapping
|
|
|
567
|
+ // Take out strings sequences and mappings
|
|
|
568
|
+ $explode = $this->_inlineEscape($innerValue);
|
|
|
569
|
+ // Propagate value array
|
|
|
570
|
+ $array = array();
|
|
|
571
|
+ foreach ($explode as $v) {
|
|
|
572
|
+ $SubArr = $this->_toType($v);
|
|
|
573
|
+ if (empty($SubArr)) continue;
|
|
|
574
|
+ if (is_array ($SubArr)) {
|
|
|
575
|
+ $array[key($SubArr)] = $SubArr[key($SubArr)]; continue;
|
|
|
576
|
+ }
|
|
|
577
|
+ $array[] = $SubArr;
|
|
|
578
|
+ }
|
|
|
579
|
+ return $array;
|
|
|
580
|
+ }
|
|
|
581
|
+
|
|
|
582
|
+ if ($value == 'null' || $value == 'NULL' || $value == 'Null' || $value == '' || $value == '~') {
|
|
|
583
|
+ return null;
|
|
|
584
|
+ }
|
|
|
585
|
+
|
|
|
586
|
+ if ( is_numeric($value) && preg_match ('/^(-|)[1-9]+[0-9]*$/', $value) ){
|
|
|
587
|
+ $intvalue = (int)$value;
|
|
|
588
|
+ if ($intvalue != PHP_INT_MAX)
|
|
|
589
|
+ $value = $intvalue;
|
|
|
590
|
+ return $value;
|
|
|
591
|
+ }
|
|
|
592
|
+
|
|
|
593
|
+ if (in_array($value,
|
|
|
594
|
+ array('true', 'on', '+', 'yes', 'y', 'True', 'TRUE', 'On', 'ON', 'YES', 'Yes', 'Y'))) {
|
|
|
595
|
+ return true;
|
|
|
596
|
+ }
|
|
|
597
|
+
|
|
|
598
|
+ if (in_array(strtolower($value),
|
|
|
599
|
+ array('false', 'off', '-', 'no', 'n'))) {
|
|
|
600
|
+ return false;
|
|
|
601
|
+ }
|
|
|
602
|
+
|
|
|
603
|
+ if (is_numeric($value)) {
|
|
|
604
|
+ if ($value === '0') return 0;
|
|
|
605
|
+ if (rtrim ($value, 0) === $value)
|
|
|
606
|
+ $value = (float)$value;
|
|
|
607
|
+ return $value;
|
|
|
608
|
+ }
|
|
|
609
|
+
|
|
|
610
|
+ return $value;
|
|
|
611
|
+ }
|
|
|
612
|
+
|
|
|
613
|
+ /**
|
|
|
614
|
+ * Used in inlines to check for more inlines or quoted strings
|
|
|
615
|
+ * @access private
|
|
|
616
|
+ * @return array
|
|
|
617
|
+ */
|
|
|
618
|
+ private function _inlineEscape($inline) {
|
|
|
619
|
+ // There's gotta be a cleaner way to do this...
|
|
|
620
|
+ // While pure sequences seem to be nesting just fine,
|
|
|
621
|
+ // pure mappings and mappings with sequences inside can't go very
|
|
|
622
|
+ // deep. This needs to be fixed.
|
|
|
623
|
+
|
|
|
624
|
+ $seqs = array();
|
|
|
625
|
+ $maps = array();
|
|
|
626
|
+ $saved_strings = array();
|
|
|
627
|
+
|
|
|
628
|
+ // Check for strings
|
|
|
629
|
+ $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/';
|
|
|
630
|
+ if (preg_match_all($regex,$inline,$strings)) {
|
|
|
631
|
+ $saved_strings = $strings[0];
|
|
|
632
|
+ $inline = preg_replace($regex,'YAMLString',$inline);
|
|
|
633
|
+ }
|
|
|
634
|
+ unset($regex);
|
|
|
635
|
+
|
|
|
636
|
+ $i = 0;
|
|
|
637
|
+ do {
|
|
|
638
|
+
|
|
|
639
|
+ // Check for sequences
|
|
|
640
|
+ while (preg_match('/\[([^{}\[\]]+)\]/U',$inline,$matchseqs)) {
|
|
|
641
|
+ $seqs[] = $matchseqs[0];
|
|
|
642
|
+ $inline = preg_replace('/\[([^{}\[\]]+)\]/U', ('YAMLSeq' . (count($seqs) - 1) . 's'), $inline, 1);
|
|
|
643
|
+ }
|
|
|
644
|
+
|
|
|
645
|
+ // Check for mappings
|
|
|
646
|
+ while (preg_match('/{([^\[\]{}]+)}/U',$inline,$matchmaps)) {
|
|
|
647
|
+ $maps[] = $matchmaps[0];
|
|
|
648
|
+ $inline = preg_replace('/{([^\[\]{}]+)}/U', ('YAMLMap' . (count($maps) - 1) . 's'), $inline, 1);
|
|
|
649
|
+ }
|
|
|
650
|
+
|
|
|
651
|
+ if ($i++ >= 10) break;
|
|
|
652
|
+
|
|
|
653
|
+ } while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false);
|
|
|
654
|
+
|
|
|
655
|
+ $explode = explode(',',$inline);
|
|
|
656
|
+ $explode = array_map('trim', $explode);
|
|
|
657
|
+ $stringi = 0; $i = 0;
|
|
|
658
|
+
|
|
|
659
|
+ while (1) {
|
|
|
660
|
+
|
|
|
661
|
+ // Re-add the sequences
|
|
|
662
|
+ if (!empty($seqs)) {
|
|
|
663
|
+ foreach ($explode as $key => $value) {
|
|
|
664
|
+ if (strpos($value,'YAMLSeq') !== false) {
|
|
|
665
|
+ foreach ($seqs as $seqk => $seq) {
|
|
|
666
|
+ $explode[$key] = str_replace(('YAMLSeq'.$seqk.'s'),$seq,$value);
|
|
|
667
|
+ $value = $explode[$key];
|
|
|
668
|
+ }
|
|
|
669
|
+ }
|
|
|
670
|
+ }
|
|
|
671
|
+ }
|
|
|
672
|
+
|
|
|
673
|
+ // Re-add the mappings
|
|
|
674
|
+ if (!empty($maps)) {
|
|
|
675
|
+ foreach ($explode as $key => $value) {
|
|
|
676
|
+ if (strpos($value,'YAMLMap') !== false) {
|
|
|
677
|
+ foreach ($maps as $mapk => $map) {
|
|
|
678
|
+ $explode[$key] = str_replace(('YAMLMap'.$mapk.'s'), $map, $value);
|
|
|
679
|
+ $value = $explode[$key];
|
|
|
680
|
+ }
|
|
|
681
|
+ }
|
|
|
682
|
+ }
|
|
|
683
|
+ }
|
|
|
684
|
+
|
|
|
685
|
+
|
|
|
686
|
+ // Re-add the strings
|
|
|
687
|
+ if (!empty($saved_strings)) {
|
|
|
688
|
+ foreach ($explode as $key => $value) {
|
|
|
689
|
+ while (strpos($value,'YAMLString') !== false) {
|
|
|
690
|
+ $explode[$key] = preg_replace('/YAMLString/',$saved_strings[$stringi],$value, 1);
|
|
|
691
|
+ unset($saved_strings[$stringi]);
|
|
|
692
|
+ ++$stringi;
|
|
|
693
|
+ $value = $explode[$key];
|
|
|
694
|
+ }
|
|
|
695
|
+ }
|
|
|
696
|
+ }
|
|
|
697
|
+
|
|
|
698
|
+ $finished = true;
|
|
|
699
|
+ foreach ($explode as $key => $value) {
|
|
|
700
|
+ if (strpos($value,'YAMLSeq') !== false) {
|
|
|
701
|
+ $finished = false; break;
|
|
|
702
|
+ }
|
|
|
703
|
+ if (strpos($value,'YAMLMap') !== false) {
|
|
|
704
|
+ $finished = false; break;
|
|
|
705
|
+ }
|
|
|
706
|
+ if (strpos($value,'YAMLString') !== false) {
|
|
|
707
|
+ $finished = false; break;
|
|
|
708
|
+ }
|
|
|
709
|
+ }
|
|
|
710
|
+ if ($finished) break;
|
|
|
711
|
+
|
|
|
712
|
+ $i++;
|
|
|
713
|
+ if ($i > 10)
|
|
|
714
|
+ break; // Prevent infinite loops.
|
|
|
715
|
+ }
|
|
|
716
|
+
|
|
|
717
|
+ return $explode;
|
|
|
718
|
+ }
|
|
|
719
|
+
|
|
|
720
|
+ private function literalBlockContinues ($line, $lineIndent) {
|
|
|
721
|
+ if (!trim($line)) return true;
|
|
|
722
|
+ if (strlen($line) - strlen(ltrim($line)) > $lineIndent) return true;
|
|
|
723
|
+ return false;
|
|
|
724
|
+ }
|
|
|
725
|
+
|
|
|
726
|
+ private function referenceContentsByAlias ($alias) {
|
|
|
727
|
+ do {
|
|
|
728
|
+ if (!isset($this->SavedGroups[$alias])) { echo "Bad group name: $alias."; break; }
|
|
|
729
|
+ $groupPath = $this->SavedGroups[$alias];
|
|
|
730
|
+ $value = $this->result;
|
|
|
731
|
+ foreach ($groupPath as $k) {
|
|
|
732
|
+ $value = $value[$k];
|
|
|
733
|
+ }
|
|
|
734
|
+ } while (false);
|
|
|
735
|
+ return $value;
|
|
|
736
|
+ }
|
|
|
737
|
+
|
|
|
738
|
+ private function addArrayInline ($array, $indent) {
|
|
|
739
|
+ $CommonGroupPath = $this->path;
|
|
|
740
|
+ if (empty ($array)) return false;
|
|
|
741
|
+
|
|
|
742
|
+ foreach ($array as $k => $_) {
|
|
|
743
|
+ $this->addArray(array($k => $_), $indent);
|
|
|
744
|
+ $this->path = $CommonGroupPath;
|
|
|
745
|
+ }
|
|
|
746
|
+ return true;
|
|
|
747
|
+ }
|
|
|
748
|
+
|
|
|
749
|
+ private function addArray ($incoming_data, $incoming_indent) {
|
|
|
750
|
+
|
|
|
751
|
+ // print_r ($incoming_data);
|
|
|
752
|
+
|
|
|
753
|
+ if (count ($incoming_data) > 1)
|
|
|
754
|
+ return $this->addArrayInline ($incoming_data, $incoming_indent);
|
|
|
755
|
+
|
|
|
756
|
+ $key = key ($incoming_data);
|
|
|
757
|
+ $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null;
|
|
|
758
|
+ if ($key === '__!YAMLZero') $key = '0';
|
|
|
759
|
+
|
|
|
760
|
+ if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values.
|
|
|
761
|
+ if ($key || $key === '' || $key === '0') {
|
|
|
762
|
+ $this->result[$key] = $value;
|
|
|
763
|
+ } else {
|
|
|
764
|
+ $this->result[] = $value; end ($this->result); $key = key ($this->result);
|
|
|
765
|
+ }
|
|
|
766
|
+ $this->path[$incoming_indent] = $key;
|
|
|
767
|
+ return;
|
|
|
768
|
+ }
|
|
|
769
|
+
|
|
|
770
|
+
|
|
|
771
|
+
|
|
|
772
|
+ $history = array();
|
|
|
773
|
+ // Unfolding inner array tree.
|
|
|
774
|
+ $history[] = $_arr = $this->result;
|
|
|
775
|
+ foreach ($this->path as $k) {
|
|
|
776
|
+ $history[] = $_arr = $_arr[$k];
|
|
|
777
|
+ }
|
|
|
778
|
+
|
|
|
779
|
+ if ($this->_containsGroupAlias) {
|
|
|
780
|
+ $value = $this->referenceContentsByAlias($this->_containsGroupAlias);
|
|
|
781
|
+ $this->_containsGroupAlias = false;
|
|
|
782
|
+ }
|
|
|
783
|
+
|
|
|
784
|
+
|
|
|
785
|
+ // Adding string or numeric key to the innermost level or $this->arr.
|
|
|
786
|
+ if (is_string($key) && $key == '<<') {
|
|
|
787
|
+ if (!is_array ($_arr)) { $_arr = array (); }
|
|
|
788
|
+
|
|
|
789
|
+ $_arr = array_merge ($_arr, $value);
|
|
|
790
|
+ } else if ($key || $key === '' || $key === '0') {
|
|
|
791
|
+ if (!is_array ($_arr))
|
|
|
792
|
+ $_arr = array ($key=>$value);
|
|
|
793
|
+ else
|
|
|
794
|
+ $_arr[$key] = $value;
|
|
|
795
|
+ } else {
|
|
|
796
|
+ if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; }
|
|
|
797
|
+ else { $_arr[] = $value; end ($_arr); $key = key ($_arr); }
|
|
|
798
|
+ }
|
|
|
799
|
+
|
|
|
800
|
+ $reverse_path = array_reverse($this->path);
|
|
|
801
|
+ $reverse_history = array_reverse ($history);
|
|
|
802
|
+ $reverse_history[0] = $_arr;
|
|
|
803
|
+ $cnt = count($reverse_history) - 1;
|
|
|
804
|
+ for ($i = 0; $i < $cnt; $i++) {
|
|
|
805
|
+ $reverse_history[$i+1][$reverse_path[$i]] = $reverse_history[$i];
|
|
|
806
|
+ }
|
|
|
807
|
+ $this->result = $reverse_history[$cnt];
|
|
|
808
|
+
|
|
|
809
|
+ $this->path[$incoming_indent] = $key;
|
|
|
810
|
+
|
|
|
811
|
+ if ($this->_containsGroupAnchor) {
|
|
|
812
|
+ $this->SavedGroups[$this->_containsGroupAnchor] = $this->path;
|
|
|
813
|
+ if (is_array ($value)) {
|
|
|
814
|
+ $k = key ($value);
|
|
|
815
|
+ if (!is_int ($k)) {
|
|
|
816
|
+ $this->SavedGroups[$this->_containsGroupAnchor][$incoming_indent + 2] = $k;
|
|
|
817
|
+ }
|
|
|
818
|
+ }
|
|
|
819
|
+ $this->_containsGroupAnchor = false;
|
|
|
820
|
+ }
|
|
|
821
|
+
|
|
|
822
|
+ }
|
|
|
823
|
+
|
|
|
824
|
+ private static function startsLiteralBlock ($line) {
|
|
|
825
|
+ $lastChar = substr (trim($line), -1);
|
|
|
826
|
+ if ($lastChar != '>' && $lastChar != '|') return false;
|
|
|
827
|
+ if ($lastChar == '|') return $lastChar;
|
|
|
828
|
+ // HTML tags should not be counted as literal blocks.
|
|
|
829
|
+ if (preg_match ('#<.*?>$#', $line)) return false;
|
|
|
830
|
+ return $lastChar;
|
|
|
831
|
+ }
|
|
|
832
|
+
|
|
|
833
|
+ private static function greedilyNeedNextLine($line) {
|
|
|
834
|
+ $line = trim ($line);
|
|
|
835
|
+ if (!strlen($line)) return false;
|
|
|
836
|
+ if (substr ($line, -1, 1) == ']') return false;
|
|
|
837
|
+ if ($line[0] == '[') return true;
|
|
|
838
|
+ if (preg_match ('#^[^:]+?:\s*\[#', $line)) return true;
|
|
|
839
|
+ return false;
|
|
|
840
|
+ }
|
|
|
841
|
+
|
|
|
842
|
+ private function addLiteralLine ($literalBlock, $line, $literalBlockStyle, $indent = -1) {
|
|
|
843
|
+ $line = self::stripIndent($line, $indent);
|
|
|
844
|
+ if ($literalBlockStyle !== '|') {
|
|
|
845
|
+ $line = self::stripIndent($line);
|
|
|
846
|
+ }
|
|
|
847
|
+ $line = rtrim ($line, "\r\n\t ") . "\n";
|
|
|
848
|
+ if ($literalBlockStyle == '|') {
|
|
|
849
|
+ return $literalBlock . $line;
|
|
|
850
|
+ }
|
|
|
851
|
+ if (strlen($line) == 0)
|
|
|
852
|
+ return rtrim($literalBlock, ' ') . "\n";
|
|
|
853
|
+ if ($line == "\n" && $literalBlockStyle == '>') {
|
|
|
854
|
+ return rtrim ($literalBlock, " \t") . "\n";
|
|
|
855
|
+ }
|
|
|
856
|
+ if ($line != "\n")
|
|
|
857
|
+ $line = trim ($line, "\r\n ") . " ";
|
|
|
858
|
+ return $literalBlock . $line;
|
|
|
859
|
+ }
|
|
|
860
|
+
|
|
|
861
|
+ function revertLiteralPlaceHolder ($lineArray, $literalBlock) {
|
|
|
862
|
+ foreach ($lineArray as $k => $_) {
|
|
|
863
|
+ if (is_array($_))
|
|
|
864
|
+ $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock);
|
|
|
865
|
+ else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder)
|
|
|
866
|
+ $lineArray[$k] = rtrim ($literalBlock, " \r\n");
|
|
|
867
|
+ }
|
|
|
868
|
+ return $lineArray;
|
|
|
869
|
+ }
|
|
|
870
|
+
|
|
|
871
|
+ private static function stripIndent ($line, $indent = -1) {
|
|
|
872
|
+ if ($indent == -1) $indent = strlen($line) - strlen(ltrim($line));
|
|
|
873
|
+ return substr ($line, $indent);
|
|
|
874
|
+ }
|
|
|
875
|
+
|
|
|
876
|
+ private function getParentPathByIndent ($indent) {
|
|
|
877
|
+ if ($indent == 0) return array();
|
|
|
878
|
+ $linePath = $this->path;
|
|
|
879
|
+ do {
|
|
|
880
|
+ end($linePath); $lastIndentInParentPath = key($linePath);
|
|
|
881
|
+ if ($indent <= $lastIndentInParentPath) array_pop ($linePath);
|
|
|
882
|
+ } while ($indent <= $lastIndentInParentPath);
|
|
|
883
|
+ return $linePath;
|
|
|
884
|
+ }
|
|
|
885
|
+
|
|
|
886
|
+
|
|
|
887
|
+ private function clearBiggerPathValues ($indent) {
|
|
|
888
|
+
|
|
|
889
|
+
|
|
|
890
|
+ if ($indent == 0) $this->path = array();
|
|
|
891
|
+ if (empty ($this->path)) return true;
|
|
|
892
|
+
|
|
|
893
|
+ foreach ($this->path as $k => $_) {
|
|
|
894
|
+ if ($k > $indent) unset ($this->path[$k]);
|
|
|
895
|
+ }
|
|
|
896
|
+
|
|
|
897
|
+ return true;
|
|
|
898
|
+ }
|
|
|
899
|
+
|
|
|
900
|
+
|
|
|
901
|
+ private static function isComment ($line) {
|
|
|
902
|
+ if (!$line) return false;
|
|
|
903
|
+ if ($line[0] == '#') return true;
|
|
|
904
|
+ if (trim($line, " \r\n\t") == '---') return true;
|
|
|
905
|
+ return false;
|
|
|
906
|
+ }
|
|
|
907
|
+
|
|
|
908
|
+ private static function isEmpty ($line) {
|
|
|
909
|
+ return (trim ($line) === '');
|
|
|
910
|
+ }
|
|
|
911
|
+
|
|
|
912
|
+
|
|
|
913
|
+ private function isArrayElement ($line) {
|
|
|
914
|
+ if (!$line) return false;
|
|
|
915
|
+ if ($line[0] != '-') return false;
|
|
|
916
|
+ if (strlen ($line) > 3)
|
|
|
917
|
+ if (substr($line,0,3) == '---') return false;
|
|
|
918
|
+
|
|
|
919
|
+ return true;
|
|
|
920
|
+ }
|
|
|
921
|
+
|
|
|
922
|
+ private function isHashElement ($line) {
|
|
|
923
|
+ return strpos($line, ':');
|
|
|
924
|
+ }
|
|
|
925
|
+
|
|
|
926
|
+ private function isLiteral ($line) {
|
|
|
927
|
+ if ($this->isArrayElement($line)) return false;
|
|
|
928
|
+ if ($this->isHashElement($line)) return false;
|
|
|
929
|
+ return true;
|
|
|
930
|
+ }
|
|
|
931
|
+
|
|
|
932
|
+
|
|
|
933
|
+ private static function unquote ($value) {
|
|
|
934
|
+ if (!$value) return $value;
|
|
|
935
|
+ if (!is_string($value)) return $value;
|
|
|
936
|
+ if ($value[0] == '\'') return trim ($value, '\'');
|
|
|
937
|
+ if ($value[0] == '"') return trim ($value, '"');
|
|
|
938
|
+ return $value;
|
|
|
939
|
+ }
|
|
|
940
|
+
|
|
|
941
|
+ private function startsMappedSequence ($line) {
|
|
|
942
|
+ return ($line[0] == '-' && substr ($line, -1, 1) == ':');
|
|
|
943
|
+ }
|
|
|
944
|
+
|
|
|
945
|
+ private function returnMappedSequence ($line) {
|
|
|
946
|
+ $array = array();
|
|
|
947
|
+ $key = self::unquote(trim(substr($line,1,-1)));
|
|
|
948
|
+ $array[$key] = array();
|
|
|
949
|
+ $this->delayedPath = array(strpos ($line, $key) + $this->indent => $key);
|
|
|
950
|
+ return array($array);
|
|
|
951
|
+ }
|
|
|
952
|
+
|
|
|
953
|
+ private function returnMappedValue ($line) {
|
|
|
954
|
+ $array = array();
|
|
|
955
|
+ $key = self::unquote (trim(substr($line,0,-1)));
|
|
|
956
|
+ $array[$key] = '';
|
|
|
957
|
+ return $array;
|
|
|
958
|
+ }
|
|
|
959
|
+
|
|
|
960
|
+ private function startsMappedValue ($line) {
|
|
|
961
|
+ return (substr ($line, -1, 1) == ':');
|
|
|
962
|
+ }
|
|
|
963
|
+
|
|
|
964
|
+ private function isPlainArray ($line) {
|
|
|
965
|
+ return ($line[0] == '[' && substr ($line, -1, 1) == ']');
|
|
|
966
|
+ }
|
|
|
967
|
+
|
|
|
968
|
+ private function returnPlainArray ($line) {
|
|
|
969
|
+ return $this->_toType($line);
|
|
|
970
|
+ }
|
|
|
971
|
+
|
|
|
972
|
+ private function returnKeyValuePair ($line) {
|
|
|
973
|
+ $array = array();
|
|
|
974
|
+ $key = '';
|
|
|
975
|
+ if (strpos ($line, ':')) {
|
|
|
976
|
+ // It's a key/value pair most likely
|
|
|
977
|
+ // If the key is in double quotes pull it out
|
|
|
978
|
+ if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) {
|
|
|
979
|
+ $value = trim(str_replace($matches[1],'',$line));
|
|
|
980
|
+ $key = $matches[2];
|
|
|
981
|
+ } else {
|
|
|
982
|
+ // Do some guesswork as to the key and the value
|
|
|
983
|
+ $explode = explode(':',$line);
|
|
|
984
|
+ $key = trim($explode[0]);
|
|
|
985
|
+ array_shift($explode);
|
|
|
986
|
+ $value = trim(implode(':',$explode));
|
|
|
987
|
+ }
|
|
|
988
|
+ // Set the type of the value. Int, string, etc
|
|
|
989
|
+ $value = $this->_toType($value);
|
|
|
990
|
+ if ($key === '0') $key = '__!YAMLZero';
|
|
|
991
|
+ $array[$key] = $value;
|
|
|
992
|
+ } else {
|
|
|
993
|
+ $array = array ($line);
|
|
|
994
|
+ }
|
|
|
995
|
+ return $array;
|
|
|
996
|
+
|
|
|
997
|
+ }
|
|
|
998
|
+
|
|
|
999
|
+
|
|
|
1000
|
+ private function returnArrayElement ($line) {
|
|
|
1001
|
+ if (strlen($line) <= 1) return array(array()); // Weird %)
|
|
|
1002
|
+ $array = array();
|
|
|
1003
|
+ $value = trim(substr($line,1));
|
|
|
1004
|
+ $value = $this->_toType($value);
|
|
|
1005
|
+ $array[] = $value;
|
|
|
1006
|
+ return $array;
|
|
|
1007
|
+ }
|
|
|
1008
|
+
|
|
|
1009
|
+
|
|
|
1010
|
+ private function nodeContainsGroup ($line) {
|
|
|
1011
|
+ $symbolsForReference = 'A-z0-9_\-';
|
|
|
1012
|
+ if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-)
|
|
|
1013
|
+ if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1];
|
|
|
1014
|
+ if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1];
|
|
|
1015
|
+ if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) return $matches[1];
|
|
|
1016
|
+ if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1];
|
|
|
1017
|
+ if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1];
|
|
|
1018
|
+ return false;
|
|
|
1019
|
+
|
|
|
1020
|
+ }
|
|
|
1021
|
+
|
|
|
1022
|
+ private function addGroup ($line, $group) {
|
|
|
1023
|
+ if ($group[0] == '&') $this->_containsGroupAnchor = substr ($group, 1);
|
|
|
1024
|
+ if ($group[0] == '*') $this->_containsGroupAlias = substr ($group, 1);
|
|
|
1025
|
+ //print_r ($this->path);
|
|
|
1026
|
+ }
|
|
|
1027
|
+
|
|
|
1028
|
+ private function stripGroup ($line, $group) {
|
|
|
1029
|
+ $line = trim(str_replace($group, '', $line));
|
|
|
1030
|
+ return $line;
|
|
|
1031
|
+ }
|
|
|
1032
|
+}
|
|
|
1033
|
+
|
|
|
1034
|
+// Enable use of Spyc from command line
|
|
|
1035
|
+// The syntax is the following: php Spyc.php spyc.yaml
|
|
|
1036
|
+
|
|
|
1037
|
+define ('SPYC_FROM_COMMAND_LINE', false);
|
|
|
1038
|
+
|
|
|
1039
|
+do {
|
|
|
1040
|
+ if (!SPYC_FROM_COMMAND_LINE) break;
|
|
|
1041
|
+ if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break;
|
|
|
1042
|
+ if (empty ($_SERVER['PHP_SELF']) || $_SERVER['PHP_SELF'] != 'Spyc.php') break;
|
|
|
1043
|
+ $file = $argv[1];
|
|
|
1044
|
+ printf ("Spyc loading file: %s\n", $file);
|
|
|
1045
|
+ print_r (spyc_load_file ($file));
|
|
|
1046
|
+} while (0); |