فهرست منبع

[Yaml] added support for the end of document marker

Fabien Potencier 15 سال پیش
والد
کامیت
04e621a5cd
2فایلهای تغییر یافته به همراه24 افزوده شده و 2 حذف شده
  1. 13 2
      src/Symfony/Components/Yaml/Parser.php
  2. 11 0
      tests/Symfony/Tests/Components/Yaml/ParserTest.php

+ 13 - 2
src/Symfony/Components/Yaml/Parser.php

@@ -503,14 +503,25 @@ class Parser
         $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#su', '', $value, -1, $count);
         $this->offset += $count;
 
-        // remove leading comments and/or ---
-        $trimmedValue = preg_replace('#^((\#.*?\n)|(\-\-\-.*?\n))*#su', '', $value, -1, $count);
+        // remove leading comments
+        $trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count);
         if ($count == 1) {
             // items have been removed, update the offset
             $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
             $value = $trimmedValue;
         }
 
+        // remove start of the document marker (---)
+        $trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count);
+        if ($count == 1) {
+            // items have been removed, update the offset
+            $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
+            $value = $trimmedValue;
+
+            // remove end of the document marker (...)
+            $value = preg_replace('#\.\.\.\s*$#s', '', $value);
+        }
+
         return $value;
     }
 }

+ 11 - 0
tests/Symfony/Tests/Components/Yaml/ParserTest.php

@@ -89,6 +89,17 @@ class ParserTest extends \PHPUnit_Framework_TestCase
         }
     }
 
+    public function testEndOfTheDocumentMarker()
+    {
+        $yaml = <<<EOF
+--- %YAML:1.0
+foo
+...
+EOF;
+
+        $this->assertEquals('foo', $this->parser->parse($yaml));
+    }
+
     public function testObjectsSupport()
     {
         $b = array('foo' => new B(), 'bar' => 1);