Index: trunk/extensions/OpenStackManager/Spyc.php |
— | — | @@ -1,236 +0,0 @@ |
2 | | -<?php |
3 | | -/** |
4 | | - * Spyc -- A Simple PHP YAML Class |
5 | | - * @version 0.2.3 -- 2006-02-04 |
6 | | - * @author Chris Wanstrath <chris@ozmm.org> |
7 | | - * @see http://spyc.sourceforge.net/ |
8 | | - * @copyright Copyright 2005-2006 Chris Wanstrath |
9 | | - * @license http://www.opensource.org/licenses/mit-license.php MIT License |
10 | | - */ |
11 | | - |
12 | | -/** |
13 | | - * The Simple PHP YAML Class. |
14 | | - * |
15 | | - * This class can be used to read a YAML file and convert its contents |
16 | | - * into a PHP array. It currently supports a very limited subsection of |
17 | | - * the YAML spec. |
18 | | - * |
19 | | - * @ingroup API |
20 | | - */ |
21 | | -class Spyc { |
22 | | - |
23 | | - /** |
24 | | - * Dump YAML from PHP array statically |
25 | | - * |
26 | | - * The dump method, when supplied with an array, will do its best |
27 | | - * to convert the array into friendly YAML. Pretty simple. Feel free to |
28 | | - * save the returned string as nothing.yml and pass it around. |
29 | | - * |
30 | | - * Oh, and you can decide how big the indent is and what the wordwrap |
31 | | - * for folding is. Pretty cool -- just pass in 'false' for either if |
32 | | - * you want to use the default. |
33 | | - * |
34 | | - * Indent's default is 2 spaces, wordwrap's default is 40 characters. And |
35 | | - * you can turn off wordwrap by passing in 0. |
36 | | - * |
37 | | - * @return string |
38 | | - * @param $array Array: PHP array |
39 | | - * @param $indent Integer: Pass in false to use the default, which is 2 |
40 | | - * @param $wordwrap Integer: Pass in 0 for no wordwrap, false for default (40) |
41 | | - */ |
42 | | - public static function YAMLDump( $array, $indent = false, $wordwrap = false ) { |
43 | | - $spyc = new Spyc; |
44 | | - return $spyc->dump( $array, $indent, $wordwrap ); |
45 | | - } |
46 | | - |
47 | | - /** |
48 | | - * Dump PHP array to YAML |
49 | | - * |
50 | | - * The dump method, when supplied with an array, will do its best |
51 | | - * to convert the array into friendly YAML. Pretty simple. Feel free to |
52 | | - * save the returned string as tasteful.yml and pass it around. |
53 | | - * |
54 | | - * Oh, and you can decide how big the indent is and what the wordwrap |
55 | | - * for folding is. Pretty cool -- just pass in 'false' for either if |
56 | | - * you want to use the default. |
57 | | - * |
58 | | - * Indent's default is 2 spaces, wordwrap's default is 40 characters. And |
59 | | - * you can turn off wordwrap by passing in 0. |
60 | | - * |
61 | | - * @public |
62 | | - * @return string |
63 | | - * @param $array Array: PHP array |
64 | | - * @param $indent Integer: Pass in false to use the default, which is 2 |
65 | | - * @param $wordwrap Integer: Pass in 0 for no wordwrap, false for default (40) |
66 | | - */ |
67 | | - function dump( $array, $indent = false, $wordwrap = false ) { |
68 | | - // Dumps to some very clean YAML. We'll have to add some more features |
69 | | - // and options soon. And better support for folding. |
70 | | - |
71 | | - // New features and options. |
72 | | - if ( $indent === false or !is_numeric( $indent ) ) { |
73 | | - $this->_dumpIndent = 2; |
74 | | - } else { |
75 | | - $this->_dumpIndent = $indent; |
76 | | - } |
77 | | - |
78 | | - if ( $wordwrap === false or !is_numeric( $wordwrap ) ) { |
79 | | - $this->_dumpWordWrap = 40; |
80 | | - } else { |
81 | | - $this->_dumpWordWrap = $wordwrap; |
82 | | - } |
83 | | - |
84 | | - // New YAML document |
85 | | - $string = "---\n"; |
86 | | - |
87 | | - // Start at the base of the array and move through it. |
88 | | - foreach ( $array as $key => $value ) { |
89 | | - $string .= $this->_yamlize( $key, $value, 0 ); |
90 | | - } |
91 | | - return $string; |
92 | | - } |
93 | | - |
94 | | - /**** Private Properties ****/ |
95 | | - |
96 | | - private $_haveRefs; |
97 | | - private $_allNodes; |
98 | | - private $_lastIndent; |
99 | | - private $_lastNode; |
100 | | - private $_inBlock; |
101 | | - private $_isInline; |
102 | | - private $_dumpIndent; |
103 | | - private $_dumpWordWrap; |
104 | | - |
105 | | - /**** Private Methods ****/ |
106 | | - |
107 | | - /** |
108 | | - * Attempts to convert a key / value array item to YAML |
109 | | - * @return string |
110 | | - * @param $key The name of the key |
111 | | - * @param $value The value of the item |
112 | | - * @param $indent The indent of the current node |
113 | | - */ |
114 | | - private function _yamlize( $key, $value, $indent ) { |
115 | | - if ( is_array( $value ) ) { |
116 | | - // It has children. What to do? |
117 | | - // Make it the right kind of item |
118 | | - $string = $this->_dumpNode( $key, null, $indent ); |
119 | | - // Add the indent |
120 | | - $indent += $this->_dumpIndent; |
121 | | - // Yamlize the array |
122 | | - $string .= $this->_yamlizeArray( $value, $indent ); |
123 | | - } elseif ( !is_array( $value ) ) { |
124 | | - // It doesn't have children. Yip. |
125 | | - $string = $this->_dumpNode( $key, $value, $indent ); |
126 | | - } |
127 | | - return $string; |
128 | | - } |
129 | | - |
130 | | - /** |
131 | | - * Attempts to convert an array to YAML |
132 | | - * @return string |
133 | | - * @param $array The array you want to convert |
134 | | - * @param $indent The indent of the current level |
135 | | - */ |
136 | | - private function _yamlizeArray( $array, $indent ) { |
137 | | - if ( is_array( $array ) ) { |
138 | | - $string = ''; |
139 | | - foreach ( $array as $key => $value ) { |
140 | | - $string .= $this->_yamlize( $key, $value, $indent ); |
141 | | - } |
142 | | - return $string; |
143 | | - } else { |
144 | | - return false; |
145 | | - } |
146 | | - } |
147 | | - |
148 | | - /** |
149 | | - * Find out whether a string needs to be output as a literal rather than in plain style. |
150 | | - * Added by Roan Kattouw 13-03-2008 |
151 | | - * @param $value The string to check |
152 | | - * @return bool |
153 | | - */ |
154 | | - function _needLiteral( $value ) { |
155 | | - // Check whether the string contains # or : or begins with any of: |
156 | | - // [ - ? , [ ] { } ! * & | > ' " % @ ` ] |
157 | | - // or is a number or contains newlines |
158 | | - return (bool)( gettype( $value ) == "string" && |
159 | | - ( is_numeric( $value ) || |
160 | | - strpos( $value, "\n" ) || |
161 | | - preg_match( "/[#:]/", $value ) || |
162 | | - preg_match( "/^[-?,[\]{}!*&|>'\"%@`]/", $value ) ) ); |
163 | | - } |
164 | | - |
165 | | - /** |
166 | | - * Returns YAML from a key and a value |
167 | | - * @return string |
168 | | - * @param $key The name of the key |
169 | | - * @param $value The value of the item |
170 | | - * @param $indent The indent of the current node |
171 | | - */ |
172 | | - private function _dumpNode( $key, $value, $indent ) { |
173 | | - // do some folding here, for blocks |
174 | | - if ( $this->_needLiteral( $value ) ) { |
175 | | - $value = $this->_doLiteralBlock( $value, $indent ); |
176 | | - } else { |
177 | | - $value = $this->_doFolding( $value, $indent ); |
178 | | - } |
179 | | - |
180 | | - $spaces = str_repeat( ' ', $indent ); |
181 | | - |
182 | | - if ( is_int( $key ) ) { |
183 | | - // It's a sequence |
184 | | - if ( $value !== '' && !is_null( $value ) ) |
185 | | - $string = $spaces . '- ' . $value . "\n"; |
186 | | - else |
187 | | - $string = $spaces . "-\n"; |
188 | | - } else { |
189 | | - if ($key == '*') //bug 21922 - Quote asterix used as keys |
190 | | - $key = "'*'"; |
191 | | - |
192 | | - // It's mapped |
193 | | - if ( $value !== '' && !is_null( $value ) ) |
194 | | - $string = $spaces . $key . ': ' . $value . "\n"; |
195 | | - else |
196 | | - $string = $spaces . $key . ":\n"; |
197 | | - } |
198 | | - return $string; |
199 | | - } |
200 | | - |
201 | | - /** |
202 | | - * Creates a literal block for dumping |
203 | | - * @return string |
204 | | - * @param $value |
205 | | - * @param $indent int The value of the indent |
206 | | - */ |
207 | | - private function _doLiteralBlock( $value, $indent ) { |
208 | | - $exploded = explode( "\n", $value ); |
209 | | - $newValue = '|-'; |
210 | | - $indent += $this->_dumpIndent; |
211 | | - $spaces = str_repeat( ' ', $indent ); |
212 | | - foreach ( $exploded as $line ) { |
213 | | - $newValue .= "\n" . $spaces . trim( $line ); |
214 | | - } |
215 | | - return $newValue; |
216 | | - } |
217 | | - |
218 | | - /** |
219 | | - * Folds a string of text, if necessary |
220 | | - * @return string |
221 | | - * @param $value The string you wish to fold |
222 | | - */ |
223 | | - private function _doFolding( $value, $indent ) { |
224 | | - // Don't do anything if wordwrap is set to 0 |
225 | | - if ( $this->_dumpWordWrap === 0 ) { |
226 | | - return $value; |
227 | | - } |
228 | | - |
229 | | - if ( strlen( $value ) > $this->_dumpWordWrap ) { |
230 | | - $indent += $this->_dumpIndent; |
231 | | - $indent = str_repeat( ' ', $indent ); |
232 | | - $wrapped = wordwrap( $value, $this->_dumpWordWrap, "\n$indent" ); |
233 | | - $value = ">-\n" . $indent . $wrapped; |
234 | | - } |
235 | | - return $value; |
236 | | - } |
237 | | -} |