|
|
@ -1,7 +1,7 @@ |
|
|
|
<?php |
|
|
|
<?php |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Spyc -- A Simple PHP YAML Class |
|
|
|
* Spyc -- A Simple PHP YAML Class |
|
|
|
* @version 0.5 |
|
|
|
* @version 0.5.1 |
|
|
|
* @author Vlad Andersen <vlad.andersen@gmail.com> |
|
|
|
* @author Vlad Andersen <vlad.andersen@gmail.com> |
|
|
|
* @author Chris Wanstrath <chris@ozmm.org> |
|
|
|
* @author Chris Wanstrath <chris@ozmm.org> |
|
|
|
* @link http://code.google.com/p/spyc/ |
|
|
|
* @link http://code.google.com/p/spyc/ |
|
|
@ -63,7 +63,7 @@ class Spyc { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Setting this to true will force YAMLDump to enclose any string value in |
|
|
|
* Setting this to true will force YAMLDump to enclose any string value in |
|
|
|
* quotes. False by default. |
|
|
|
* quotes. False by default. |
|
|
|
* |
|
|
|
* |
|
|
|
* @var bool |
|
|
|
* @var bool |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $setting_dump_force_quotes = false; |
|
|
|
public $setting_dump_force_quotes = false; |
|
|
@ -143,7 +143,7 @@ class Spyc { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Load a string of YAML into a PHP array statically |
|
|
|
* Load a string of YAML into a PHP array statically |
|
|
|
* |
|
|
|
* |
|
|
|
* The load method, when supplied with a YAML string, will do its best |
|
|
|
* The load method, when supplied with a YAML string, will do its best |
|
|
|
* to convert YAML in a string into a PHP array. Pretty simple. |
|
|
|
* to convert YAML in a string into a PHP array. Pretty simple. |
|
|
|
* |
|
|
|
* |
|
|
|
* Note: use this function if you don't want files from the file system |
|
|
|
* Note: use this function if you don't want files from the file system |
|
|
@ -232,7 +232,7 @@ class Spyc { |
|
|
|
|
|
|
|
|
|
|
|
// Start at the base of the array and move through it. |
|
|
|
// Start at the base of the array and move through it. |
|
|
|
if ($array) { |
|
|
|
if ($array) { |
|
|
|
$array = (array)$array; |
|
|
|
$array = (array)$array; |
|
|
|
$previous_key = -1; |
|
|
|
$previous_key = -1; |
|
|
|
foreach ($array as $key => $value) { |
|
|
|
foreach ($array as $key => $value) { |
|
|
|
if (!isset($first_key)) $first_key = $key; |
|
|
|
if (!isset($first_key)) $first_key = $key; |
|
|
@ -321,7 +321,7 @@ class Spyc { |
|
|
|
if (is_bool($value)) { |
|
|
|
if (is_bool($value)) { |
|
|
|
$value = ($value) ? "true" : "false"; |
|
|
|
$value = ($value) ? "true" : "false"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($value === null) $value = 'null'; |
|
|
|
if ($value === null) $value = 'null'; |
|
|
|
if ($value === "'" . self::REMPTY . "'") $value = null; |
|
|
|
if ($value === "'" . self::REMPTY . "'") $value = null; |
|
|
|
|
|
|
|
|
|
|
@ -413,7 +413,7 @@ class Spyc { |
|
|
|
$cnt = count($Source); |
|
|
|
$cnt = count($Source); |
|
|
|
for ($i = 0; $i < $cnt; $i++) { |
|
|
|
for ($i = 0; $i < $cnt; $i++) { |
|
|
|
$line = $Source[$i]; |
|
|
|
$line = $Source[$i]; |
|
|
|
|
|
|
|
|
|
|
|
$this->indent = strlen($line) - strlen(ltrim($line)); |
|
|
|
$this->indent = strlen($line) - strlen(ltrim($line)); |
|
|
|
$tempPath = $this->getParentPathByIndent($this->indent); |
|
|
|
$tempPath = $this->getParentPathByIndent($this->indent); |
|
|
|
$line = self::stripIndent($line, $this->indent); |
|
|
|
$line = self::stripIndent($line, $this->indent); |
|
|
@ -433,18 +433,16 @@ class Spyc { |
|
|
|
$i--; |
|
|
|
$i--; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Strip out comments |
|
|
|
|
|
|
|
if (strpos ($line, '#')) { |
|
|
|
|
|
|
|
$line = preg_replace('/\s*#([^"\']+)$/','',$line); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while (++$i < $cnt && self::greedilyNeedNextLine($line)) { |
|
|
|
while (++$i < $cnt && self::greedilyNeedNextLine($line)) { |
|
|
|
$line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t"); |
|
|
|
$line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t"); |
|
|
|
} |
|
|
|
} |
|
|
|
$i--; |
|
|
|
$i--; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (strpos ($line, '#')) { |
|
|
|
|
|
|
|
if (strpos ($line, '"') === false && strpos ($line, "'") === false) |
|
|
|
|
|
|
|
$line = preg_replace('/\s+#(.+)$/','',$line); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$lineArray = $this->_parseLine($line); |
|
|
|
$lineArray = $this->_parseLine($line); |
|
|
|
|
|
|
|
|
|
|
|
if ($literalBlockStyle) |
|
|
|
if ($literalBlockStyle) |
|
|
@ -505,9 +503,9 @@ class Spyc { |
|
|
|
return $this->returnArrayElement($line); |
|
|
|
return $this->returnArrayElement($line); |
|
|
|
|
|
|
|
|
|
|
|
if ($this->isPlainArray($line)) |
|
|
|
if ($this->isPlainArray($line)) |
|
|
|
return $this->returnPlainArray($line); |
|
|
|
return $this->returnPlainArray($line); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $this->returnKeyValuePair($line); |
|
|
|
return $this->returnKeyValuePair($line); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
@ -533,7 +531,7 @@ class Spyc { |
|
|
|
|
|
|
|
|
|
|
|
if ($is_quoted) |
|
|
|
if ($is_quoted) |
|
|
|
return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\'')); |
|
|
|
return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\'')); |
|
|
|
|
|
|
|
|
|
|
|
if (strpos($value, ' #') !== false && !$is_quoted) |
|
|
|
if (strpos($value, ' #') !== false && !$is_quoted) |
|
|
|
$value = preg_replace('/\s+#(.+)$/','',$value); |
|
|
|
$value = preg_replace('/\s+#(.+)$/','',$value); |
|
|
|
|
|
|
|
|
|
|
@ -560,7 +558,7 @@ class Spyc { |
|
|
|
$value = $this->_toType($value); |
|
|
|
$value = $this->_toType($value); |
|
|
|
return array($key => $value); |
|
|
|
return array($key => $value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($first_character == '{' && $last_character == '}') { |
|
|
|
if ($first_character == '{' && $last_character == '}') { |
|
|
|
$innerValue = trim(substr ($value, 1, -1)); |
|
|
|
$innerValue = trim(substr ($value, 1, -1)); |
|
|
|
if ($innerValue === '') return array(); |
|
|
|
if ($innerValue === '') return array(); |
|
|
@ -607,7 +605,7 @@ class Spyc { |
|
|
|
$value = (float)$value; |
|
|
|
$value = (float)$value; |
|
|
|
return $value; |
|
|
|
return $value; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $value; |
|
|
|
return $value; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -653,7 +651,8 @@ class Spyc { |
|
|
|
|
|
|
|
|
|
|
|
} while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false); |
|
|
|
} while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false); |
|
|
|
|
|
|
|
|
|
|
|
$explode = explode(', ',$inline); |
|
|
|
$explode = explode(',',$inline); |
|
|
|
|
|
|
|
$explode = array_map('trim', $explode); |
|
|
|
$stringi = 0; $i = 0; |
|
|
|
$stringi = 0; $i = 0; |
|
|
|
|
|
|
|
|
|
|
|
while (1) { |
|
|
|
while (1) { |
|
|
@ -710,7 +709,7 @@ class Spyc { |
|
|
|
if ($finished) break; |
|
|
|
if ($finished) break; |
|
|
|
|
|
|
|
|
|
|
|
$i++; |
|
|
|
$i++; |
|
|
|
if ($i > 10) |
|
|
|
if ($i > 10) |
|
|
|
break; // Prevent infinite loops. |
|
|
|
break; // Prevent infinite loops. |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -738,7 +737,7 @@ class Spyc { |
|
|
|
private function addArrayInline ($array, $indent) { |
|
|
|
private function addArrayInline ($array, $indent) { |
|
|
|
$CommonGroupPath = $this->path; |
|
|
|
$CommonGroupPath = $this->path; |
|
|
|
if (empty ($array)) return false; |
|
|
|
if (empty ($array)) return false; |
|
|
|
|
|
|
|
|
|
|
|
foreach ($array as $k => $_) { |
|
|
|
foreach ($array as $k => $_) { |
|
|
|
$this->addArray(array($k => $_), $indent); |
|
|
|
$this->addArray(array($k => $_), $indent); |
|
|
|
$this->path = $CommonGroupPath; |
|
|
|
$this->path = $CommonGroupPath; |
|
|
@ -752,7 +751,7 @@ class Spyc { |
|
|
|
|
|
|
|
|
|
|
|
if (count ($incoming_data) > 1) |
|
|
|
if (count ($incoming_data) > 1) |
|
|
|
return $this->addArrayInline ($incoming_data, $incoming_indent); |
|
|
|
return $this->addArrayInline ($incoming_data, $incoming_indent); |
|
|
|
|
|
|
|
|
|
|
|
$key = key ($incoming_data); |
|
|
|
$key = key ($incoming_data); |
|
|
|
$value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
|
|
|
$value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
|
|
|
if ($key === '__!YAMLZero') $key = '0'; |
|
|
|
if ($key === '__!YAMLZero') $key = '0'; |
|
|
@ -768,7 +767,7 @@ class Spyc { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$history = array(); |
|
|
|
$history = array(); |
|
|
|
// Unfolding inner array tree. |
|
|
|
// Unfolding inner array tree. |
|
|
|
$history[] = $_arr = $this->result; |
|
|
|
$history[] = $_arr = $this->result; |
|
|
@ -915,7 +914,7 @@ class Spyc { |
|
|
|
if ($line[0] != '-') return false; |
|
|
|
if ($line[0] != '-') return false; |
|
|
|
if (strlen ($line) > 3) |
|
|
|
if (strlen ($line) > 3) |
|
|
|
if (substr($line,0,3) == '---') return false; |
|
|
|
if (substr($line,0,3) == '---') return false; |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -960,14 +959,14 @@ class Spyc { |
|
|
|
private function startsMappedValue ($line) { |
|
|
|
private function startsMappedValue ($line) { |
|
|
|
return (substr ($line, -1, 1) == ':'); |
|
|
|
return (substr ($line, -1, 1) == ':'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function isPlainArray ($line) { |
|
|
|
private function isPlainArray ($line) { |
|
|
|
return ($line[0] == '[' && substr ($line, -1, 1) == ']'); |
|
|
|
return ($line[0] == '[' && substr ($line, -1, 1) == ']'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function returnPlainArray ($line) { |
|
|
|
private function returnPlainArray ($line) { |
|
|
|
return $this->_toType($line); |
|
|
|
return $this->_toType($line); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function returnKeyValuePair ($line) { |
|
|
|
private function returnKeyValuePair ($line) { |
|
|
|
$array = array(); |
|
|
|
$array = array(); |
|
|
@ -1007,7 +1006,7 @@ class Spyc { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function nodeContainsGroup ($line) { |
|
|
|
private function nodeContainsGroup ($line) { |
|
|
|
$symbolsForReference = 'A-z0-9_\-'; |
|
|
|
$symbolsForReference = 'A-z0-9_\-'; |
|
|
|
if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) |
|
|
|
if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) |
|
|
|
if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
|
|
if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
|
@ -1032,15 +1031,15 @@ class Spyc { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Enable use of Spyc from command line |
|
|
|
// Enable use of Spyc from command line |
|
|
|
// The syntax is the following: php spyc.php spyc.yaml |
|
|
|
// The syntax is the following: php Spyc.php spyc.yaml |
|
|
|
|
|
|
|
|
|
|
|
define ('SPYC_FROM_COMMAND_LINE', false); |
|
|
|
define ('SPYC_FROM_COMMAND_LINE', false); |
|
|
|
|
|
|
|
|
|
|
|
do { |
|
|
|
do { |
|
|
|
if (!SPYC_FROM_COMMAND_LINE) break; |
|
|
|
if (!SPYC_FROM_COMMAND_LINE) break; |
|
|
|
if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break; |
|
|
|
if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break; |
|
|
|
if (empty ($_SERVER['PHP_SELF']) || $_SERVER['PHP_SELF'] != 'spyc.php') break; |
|
|
|
if (empty ($_SERVER['PHP_SELF']) || $_SERVER['PHP_SELF'] != 'Spyc.php') break; |
|
|
|
$file = $argv[1]; |
|
|
|
$file = $argv[1]; |
|
|
|
printf ("Spyc loading file: %s\n", $file); |
|
|
|
printf ("Spyc loading file: %s\n", $file); |
|
|
|
print_r (spyc_load_file ($file)); |
|
|
|
print_r (spyc_load_file ($file)); |
|
|
|
} while (0); |
|
|
|
} while (0); |