Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- protected static function _decodeYaml($currentIndent, $lines)
- {
- $config = array();
- $inIndent = false;
- foreach ($lines as $n => $line) {
- $lineno = $n + 1;
- $line = rtrim(preg_replace("/#.*$/", "", $line));
- if (strlen($line) == 0) {
- continue;
- }
- $indent = strspn($line, " ");
- // line without the spaces
- $line = trim($line);
- if (strlen($line) == 0) {
- continue;
- }
- if ($indent < $currentIndent) {
- // this level is done
- return $config;
- }
- if (!$inIndent) {
- $currentIndent = $indent;
- $inIndent = true;
- }
- if (preg_match("/(?!-)([\w\-]+):\s*(.*)/", $line, $m)) {
- // key: value
- if (strlen($m[2])) {
- // simple key: value
- $value = preg_replace("/#.*$/", "", $m[2]);
- $value = self::_parseValue($value);
- } else {
- // key: and then values on new lines
- $value = self::_decodeYaml($currentIndent + 1, array_slice($lines, $n+1, count($lines)-1));
- if (is_array($value) && !count($value)) {
- $value = "";
- }
- }
- $config[$m[1]] = $value;
- } elseif ($line[0] == "-") {
- // item in the list:
- // - FOO
- if (strlen($line) > 2) {
- $value = substr($line, 2);
- $config[] = self::_parseValue($value);
- } else {
- $config[] = self::_decodeYaml($currentIndent + 1, array_slice($lines, $n+1, count($lines)-1));
- }
- } else {
- require_once 'Zend/Config/Exception.php';
- throw new Zend_Config_Exception(sprintf(
- 'Error parsing YAML at line %d - unsupported syntax: "%s"',
- $lineno, $line
- ));
- }
- }
- return $config;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement