We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

Commit 7e015df5 authored by Fabian Schmengler's avatar Fabian Schmengler

Update Spyc YAML lib to 0.5.1 for PHP 7.1 compatibility

parent ac76e595
<?php
/**
* Spyc -- A Simple PHP YAML Class
* @version 0.4.5
* @version 0.5.1
* @author Vlad Andersen <vlad.andersen@gmail.com>
* @author Chris Wanstrath <chris@ozmm.org>
* @link http://code.google.com/p/spyc/
* @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2009 Vlad Andersen
* @link https://github.com/mustangostang/spyc/
* @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2011 Vlad Andersen
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Spyc
*/
......@@ -32,7 +32,20 @@ if (!function_exists('spyc_load_file')) {
}
}
/**
if (!function_exists('spyc_dump')) {
/**
* Dumps array to YAML.
* @param array $data Array.
* @return string
*/
function spyc_dump ($data) {
return Spyc::YAMLDump($data, false, false, true);
}
}
if (!class_exists('Spyc')) {
/**
* The Simple PHP YAML Class.
*
* This class can be used to read a YAML file and convert its contents
......@@ -54,10 +67,12 @@ if (!function_exists('spyc_load_file')) {
* </code>
* @package Spyc
*/
class Spyc {
class Spyc {
// SETTINGS
const REMPTY = "\0\0\0\0\0";
/**
* Setting this to true will force YAMLDump to enclose any string value in
* quotes. False by default.
......@@ -100,13 +115,13 @@ class Spyc {
*/
public $_nodeId;
/**
/**
* Load a valid YAML string to Spyc.
* @param string $input
* @return array
*/
public function load ($input) {
return $this->__loadString($input);
return $this->_loadString($input);
}
/**
......@@ -115,7 +130,7 @@ class Spyc {
* @return array
*/
public function loadFile ($file) {
return $this->__load($file);
return $this->_load($file);
}
/**
......@@ -135,7 +150,7 @@ class Spyc {
*/
public static function YAMLLoad($input) {
$Spyc = new Spyc;
return $Spyc->__load($input);
return $Spyc->_load($input);
}
/**
......@@ -159,7 +174,7 @@ class Spyc {
*/
public static function YAMLLoadString($input) {
$Spyc = new Spyc;
return $Spyc->__loadString($input);
return $Spyc->_loadString($input);
}
/**
......@@ -178,13 +193,14 @@ class Spyc {
*
* @access public
* @return string
* @param array $array PHP array
* @param array|\stdClass $array PHP array
* @param int $indent Pass in false to use the default, which is 2
* @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
* @param bool $no_opening_dashes Do not start YAML file with "---\n"
*/
public static function YAMLDump($array,$indent = false,$wordwrap = false) {
public static function YAMLDump($array, $indent = false, $wordwrap = false, $no_opening_dashes = false) {
$spyc = new Spyc;
return $spyc->dump($array,$indent,$wordwrap);
return $spyc->dump($array, $indent, $wordwrap, $no_opening_dashes);
}
......@@ -208,7 +224,7 @@ class Spyc {
* @param int $indent Pass in false to use the default, which is 2
* @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
*/
public function dump($array,$indent = false,$wordwrap = false) {
public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashes = false) {
// Dumps to some very clean YAML. We'll have to add some more features
// and options soon. And better support for folding.
......@@ -226,16 +242,16 @@ class Spyc {
}
// New YAML document
$string = "---\n";
$string = "";
if (!$no_opening_dashes) $string = "---\n";
// Start at the base of the array and move through it.
if ($array) {
$array = (array)$array;
$first_key = key($array);
$previous_key = -1;
foreach ($array as $key => $value) {
$string .= $this->_yamlize($key,$value,0,$previous_key, $first_key);
if (!isset($first_key)) $first_key = $key;
$string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array);
$previous_key = $key;
}
}
......@@ -250,20 +266,21 @@ class Spyc {
* @param $value The value of the item
* @param $indent The indent of the current node
*/
private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0) {
private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) {
if(is_object($value)) $value = (array)$value;
if (is_array($value)) {
if (empty ($value))
return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key);
return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array);
// It has children. What to do?
// Make it the right kind of item
$string = $this->_dumpNode($key, NULL, $indent, $previous_key, $first_key);
$string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array);
// Add the indent
$indent += $this->_dumpIndent;
// Yamlize the array
$string .= $this->_yamlizeArray($value,$indent);
} elseif (!is_array($value)) {
// It doesn't have children. Yip.
$string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key);
$string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array);
}
return $string;
}
......@@ -279,9 +296,9 @@ class Spyc {
if (is_array($array)) {
$string = '';
$previous_key = -1;
$first_key = key($array);
foreach ($array as $key => $value) {
$string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key);
if (!isset($first_key)) $first_key = $key;
$string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array);
$previous_key = $key;
}
return $string;
......@@ -298,31 +315,44 @@ class Spyc {
* @param $value The value of the item
* @param $indent The indent of the current node
*/
private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0) {
private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) {
// do some folding here, for blocks
if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false ||
strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false ||
strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || substr ($value, -1, 1) == ':')) {
strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false || strpos ($value, '%') !== false || strpos ($value, ' ') !== false ||
strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || strpos($value,"&") !== false || strpos($value, "'") !== false || strpos($value, "!") === 0 ||
substr ($value, -1, 1) == ':')
) {
$value = $this->_doLiteralBlock($value,$indent);
} else {
$value = $this->_doFolding($value,$indent);
if (is_bool($value)) {
$value = ($value) ? "true" : "false";
}
}
if ($value === array()) $value = '[ ]';
if ($value === "") $value = '""';
if (self::isTranslationWord($value)) {
$value = $this->_doLiteralBlock($value, $indent);
}
if (trim ($value) != $value)
$value = $this->_doLiteralBlock($value,$indent);
if (is_bool($value)) {
$value = $value ? "true" : "false";
}
if ($value === null) $value = 'null';
if ($value === "'" . self::REMPTY . "'") $value = null;
$spaces = str_repeat(' ',$indent);
if (is_int($key) && $key - 1 == $previous_key && $first_key===0) {
//if (is_int($key) && $key - 1 == $previous_key && $first_key===0) {
if (is_array ($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) {
// It's a sequence
$string = $spaces.'- '.$value."\n";
} else {
if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"');
// if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"');
// It's mapped
if (strpos($key, ":") !== false) { $key = '"' . $key . '"'; }
$string = $spaces.$key.': '.$value."\n";
if (strpos($key, ":") !== false || strpos($key, "#") !== false) { $key = '"' . $key . '"'; }
$string = rtrim ($spaces.$key.': '.$value)."\n";
}
return $string;
}
......@@ -335,6 +365,7 @@ class Spyc {
* @param $indent int The value of the indent
*/
private function _doLiteralBlock($value,$indent) {
if ($value === "\n") return '\n';
if (strpos($value, "\n") === false && strpos($value, "'") === false) {
return sprintf ("'%s'", $value);
}
......@@ -343,10 +374,18 @@ class Spyc {
}
$exploded = explode("\n",$value);
$newValue = '|';
if (isset($exploded[0]) && ($exploded[0] == "|" || $exploded[0] == "|-" || $exploded[0] == ">")) {
$newValue = $exploded[0];
unset($exploded[0]);
}
$indent += $this->_dumpIndent;
$spaces = str_repeat(' ',$indent);
foreach ($exploded as $line) {
$newValue .= "\n" . $spaces . trim($line);
$line = trim($line);
if (strpos($line, '"') === 0 && strrpos($line, '"') == (strlen($line)-1) || strpos($line, "'") === 0 && strrpos($line, "'") == (strlen($line)-1)) {
$line = substr($line, 1, -1);
}
$newValue .= "\n" . $spaces . ($line);
}
return $newValue;
}
......@@ -366,7 +405,9 @@ class Spyc {
$wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent");
$value = ">\n".$indent.$wrapped;
} else {
if ($this->setting_dump_force_quotes && is_string ($value))
if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY)
$value = '"' . $value . '"';
if (is_numeric($value) && is_string($value))
$value = '"' . $value . '"';
}
......@@ -374,14 +415,68 @@ class Spyc {
return $value;
}
private function isTrueWord($value) {
$words = self::getTranslations(array('true', 'on', 'yes', 'y'));
return in_array($value, $words, true);
}
private function isFalseWord($value) {
$words = self::getTranslations(array('false', 'off', 'no', 'n'));
return in_array($value, $words, true);
}
private function isNullWord($value) {
$words = self::getTranslations(array('null', '~'));
return in_array($value, $words, true);
}
private function isTranslationWord($value) {
return (
self::isTrueWord($value) ||
self::isFalseWord($value) ||
self::isNullWord($value)
);
}
/**
* Coerce a string into a native type
* Reference: http://yaml.org/type/bool.html
* TODO: Use only words from the YAML spec.
* @access private
* @param $value The value to coerce
*/
private function coerceValue(&$value) {
if (self::isTrueWord($value)) {
$value = true;
} else if (self::isFalseWord($value)) {
$value = false;
} else if (self::isNullWord($value)) {
$value = null;
}
}
/**
* Given a set of words, perform the appropriate translations on them to
* match the YAML 1.1 specification for type coercing.
* @param $words The words to translate
* @access private
*/
private static function getTranslations(array $words) {
$result = array();
foreach ($words as $i) {
$result = array_merge($result, array(ucfirst($i), strtoupper($i), strtolower($i)));
}
return $result;
}
// LOADING FUNCTIONS
private function __load($input) {
private function _load($input) {
$Source = $this->loadFromSource($input);
return $this->loadWithSource($Source);
}
private function __loadString($input) {
private function _loadString($input) {
$Source = $this->loadFromString($input);
return $this->loadWithSource($Source);
}
......@@ -389,7 +484,7 @@ class Spyc {
private function loadWithSource($Source) {
if (empty ($Source)) return array();
if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) {
$array = syck_load (implode ('', $Source));
$array = syck_load (implode ("\n", $Source));
return is_array($array) ? $array : array();
}
......@@ -411,26 +506,24 @@ class Spyc {
if ($literalBlockStyle) {
$line = rtrim ($line, $literalBlockStyle . " \n");
$literalBlock = '';
$line .= $this->LiteralPlaceHolder;
$line .= ' '.$this->LiteralPlaceHolder;
$literal_block_indent = strlen($Source[$i+1]) - strlen(ltrim($Source[$i+1]));
while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) {
$literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle);
$literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent);
}
$i--;
}
// Strip out comments
if (strpos ($line, '#')) {
$line = preg_replace('/\s*#([^"\']+)$/','',$line);
}
while (++$i < $cnt && self::greedilyNeedNextLine($line)) {
$line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t");
}
$i--;
if (strpos ($line, '#')) {
if (strpos ($line, '"') === false && strpos ($line, "'") === false)
$line = preg_replace('/\s+#(.+)$/','',$line);
}
$lineArray = $this->_parseLine($line);
if ($literalBlockStyle)
......@@ -449,7 +542,7 @@ class Spyc {
private function loadFromSource ($input) {
if (!empty($input) && strpos($input, "\n") === false && file_exists($input))
return file($input);
$input = file_get_contents($input);
return $this->loadFromString($input);
}
......@@ -471,8 +564,8 @@ class Spyc {
private function _parseLine($line) {
if (!$line) return array();
$line = trim($line);
if (!$line) return array();
$array = array();
$group = $this->nodeContainsGroup($line);
......@@ -505,7 +598,7 @@ class Spyc {
* @return mixed
*/
private function _toType($value) {
if ($value === '') return null;
if ($value === '') return "";
$first_character = $value[0];
$last_character = substr($value, -1, 1);
......@@ -517,10 +610,14 @@ class Spyc {
$is_quoted = true;
} while (0);
if ($is_quoted)
return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\''));
if ($is_quoted) {
$value = str_replace('\n', "\n", $value);
if ($first_character == "'")
return strtr(substr ($value, 1, -1), array ('\'\'' => '\'', '\\\''=> '\''));
return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\\\''=> '\''));
}
if (strpos($value, ' #') !== false)
if (strpos($value, ' #') !== false && !$is_quoted)
$value = preg_replace('/\s+#(.+)$/','',$value);
if ($first_character == '[' && $last_character == ']') {
......@@ -568,26 +665,23 @@ class Spyc {
return null;
}
if (intval($first_character) > 0 && preg_match ('/^[1-9]+[0-9]*$/', $value)) {
if ( is_numeric($value) && preg_match ('/^(-|)[1-9]+[0-9]*$/', $value) ){
$intvalue = (int)$value;
if ($intvalue != PHP_INT_MAX)
if ($intvalue != PHP_INT_MAX && $intvalue != ~PHP_INT_MAX)
$value = $intvalue;
return $value;
}
if (in_array($value,
array('true', 'on', '+', 'yes', 'y', 'True', 'TRUE', 'On', 'ON', 'YES', 'Yes', 'Y'))) {
return true;
if ( is_string($value) && preg_match('/^0[xX][0-9a-fA-F]+$/', $value)) {
// Hexadecimal value.
return hexdec($value);
}
if (in_array(strtolower($value),
array('false', 'off', '-', 'no', 'n'))) {
return false;
}
$this->coerceValue($value);
if (is_numeric($value)) {
if ($value === '0') return 0;
if (trim ($value, 0) === $value)
if (rtrim ($value, 0) === $value)
$value = (float)$value;
return $value;
}
......@@ -609,6 +703,15 @@ class Spyc {
$seqs = array();
$maps = array();
$saved_strings = array();
$saved_empties = array();
// Check for empty strings
$regex = '/("")|(\'\')/';
if (preg_match_all($regex,$inline,$strings)) {
$saved_empties = $strings[0];
$inline = preg_replace($regex,'YAMLEmpty',$inline);
}
unset($regex);
// Check for strings
$regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/';
......@@ -618,6 +721,8 @@ class Spyc {
}
unset($regex);
// echo $inline;
$i = 0;
do {
......@@ -637,7 +742,8 @@ class Spyc {
} while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false);
$explode = explode(', ',$inline);
$explode = explode(',',$inline);
$explode = array_map('trim', $explode);
$stringi = 0; $i = 0;
while (1) {
......@@ -679,6 +785,17 @@ class Spyc {
}
}
// Re-add the empties
if (!empty($saved_empties)) {
foreach ($explode as $key => $value) {
while (strpos($value,'YAMLEmpty') !== false) {
$explode[$key] = preg_replace('/YAMLEmpty/', '', $value, 1);
$value = $explode[$key];
}
}
}
$finished = true;
foreach ($explode as $key => $value) {
if (strpos($value,'YAMLSeq') !== false) {
......@@ -690,6 +807,9 @@ class Spyc {
if (strpos($value,'YAMLString') !== false) {
$finished = false; break;
}
if (strpos($value,'YAMLEmpty') !== false) {
$finished = false; break;
}
}
if ($finished) break;
......@@ -698,6 +818,7 @@ class Spyc {
break; // Prevent infinite loops.
}
return $explode;
}
......@@ -772,6 +893,9 @@ class Spyc {
$_arr = array_merge ($_arr, $value);
} else if ($key || $key === '' || $key === '0') {
if (!is_array ($_arr))
$_arr = array ($key=>$value);
else
$_arr[$key] = $value;
} else {
if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; }
......@@ -820,8 +944,11 @@ class Spyc {
return false;
}
private function addLiteralLine ($literalBlock, $line, $literalBlockStyle) {
private function addLiteralLine ($literalBlock, $line, $literalBlockStyle, $indent = -1) {
$line = self::stripIndent($line, $indent);
if ($literalBlockStyle !== '|') {
$line = self::stripIndent($line);
}
$line = rtrim ($line, "\r\n\t ") . "\n";
if ($literalBlockStyle == '|') {
return $literalBlock . $line;
......@@ -889,8 +1016,8 @@ class Spyc {
private function isArrayElement ($line) {
if (!$line) return false;
if ($line[0] != '-') return false;
if (!$line || !is_scalar($line)) return false;
if (substr($line, 0, 2) != '- ') return false;
if (strlen ($line) > 3)
if (substr($line,0,3) == '---') return false;
......@@ -917,7 +1044,7 @@ class Spyc {
}
private function startsMappedSequence ($line) {
return ($line[0] == '-' && substr ($line, -1, 1) == ':');
return (substr($line, 0, 2) == '- ' && substr ($line, -1, 1) == ':');
}
private function returnMappedSequence ($line) {
......@@ -928,7 +1055,16 @@ class Spyc {
return array($array);
}
private function checkKeysInValue($value) {
if (strchr('[{"\'', $value[0]) === false) {
if (strchr($value, ': ') !== false) {
throw new Exception('Too many keys: '.$value);
}
}
}
private function returnMappedValue ($line) {
$this->checkKeysInValue($line);
$array = array();
$key = self::unquote (trim(substr($line,0,-1)));
$array[$key] = '';
......@@ -950,7 +1086,7 @@ class Spyc {
private function returnKeyValuePair ($line) {
$array = array();
$key = '';
if (strpos ($line, ':')) {
if (strpos ($line, ': ')) {
// It's a key/value pair most likely
// If the key is in double quotes pull it out
if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) {
......@@ -958,10 +1094,10 @@ class Spyc {
$key = $matches[2];
} else {
// Do some guesswork as to the key and the value
$explode = explode(':',$line);
$key = trim($explode[0]);
array_shift($explode);
$value = trim(implode(':',$explode));
$explode = explode(': ', $line);
$key = trim(array_shift($explode));
$value = trim(implode(': ', $explode));
$this->checkKeysInValue($value);
}
// Set the type of the value. Int, string, etc
$value = $this->_toType($value);
......@@ -980,6 +1116,9 @@ class Spyc {
$array = array();
$value = trim(substr($line,1));
$value = $this->_toType($value);
if ($this->isArrayElement($value)) {
$value = $this->returnArrayElement($value);
}
$array[] = $value;
return $array;
}
......@@ -1007,18 +1146,16 @@ class Spyc {
$line = trim(str_replace($group, '', $line));
return $line;
}
}
}
// Enable use of Spyc from command line
// The syntax is the following: php spyc.php spyc.yaml
define ('SPYC_FROM_COMMAND_LINE', false);
// The syntax is the following: php Spyc.php spyc.yaml
do {
if (!SPYC_FROM_COMMAND_LINE) break;
if (PHP_SAPI != 'cli') 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']) || FALSE === strpos ($_SERVER['PHP_SELF'], 'Spyc.php') ) break;
$file = $argv[1];
printf ("Spyc loading file: %s\n", $file);
print_r (spyc_load_file ($file));
echo json_encode (spyc_load_file ($file));
} while (0);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment