瀏覽代碼

Fix bug with erroneously splitting on =s (fix #7)

shalvah 5 年之前
父節點
當前提交
a90d020203
共有 2 個文件被更改,包括 15 次插入26 次删除
  1. 1 1
      docs/index.md
  2. 14 25
      src/Tools/AnnotationParser.php

+ 1 - 1
docs/index.md

@@ -22,7 +22,7 @@ Generate API documentation for humans from your Laravel/Lumen/[Dingo](https://gi
 * [Documenting possible responses returned from the endpoint](documenting-endpoint-responses.md)
 * [Generating documentation](generating-documentation.md)
 * [Configuration](config.md)
-* [Troubleshooting and Debugging](troubleshooting.md)
+* [Troubleshooting and debugging](troubleshooting.md)
 * [Advanced customization](customization.md)
 * [How Scribe works](architecture.md)
 * [Extending functionality with plugins](plugins.md)

+ 14 - 25
src/Tools/AnnotationParser.php

@@ -6,39 +6,28 @@ class AnnotationParser
 {
     /**
      * Parse an annotation like 'status=400 when="things go wrong" {"message": "failed"}'.
-     * Attributes are always optional.
+     * Attributes are always optional and may appear at the start or the end of the string.
      *
      * @param string $annotationContent
      */
     public static function parseIntoContentAndAttributes(string $annotationContent, array $allowedAttributes): array
     {
-        $result = preg_split('/(\w+)=([^\s\'"]+|".+?"|\'.+?\')\s*/', trim($annotationContent), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
+        $parsedAttributes = array_fill_keys($allowedAttributes, null);
 
-        $defaults = array_fill_keys($allowedAttributes, null);
-        if (count($result) == 1) { // No key-value pairs
-            return [
-                'content' => trim($result[0]),
-                'attributes' => $defaults
-            ];
-        }
+        foreach ($allowedAttributes as $attribute) {
+            preg_match("/$attribute=([^\\s'\"]+|\".+?\"|'.+?')\\s*/", $annotationContent, $attributeAndValue);
 
-        // Separate the main content
-        if (in_array($result[0], $allowedAttributes)) {
-            $content = trim(array_pop($result));
-        } else {
-            $content = trim(array_shift($result));
-        }
+            if (count($attributeAndValue)) {
+                [$matchingText, $attributeValue] = $attributeAndValue;
+                $annotationContent = str_replace($matchingText, '', $annotationContent);
 
-        [$keys, $values] = collect($result)->partition(function ($value, $key) {
-            return $key % 2;
-        });
-        $attributes = collect($values)->combine($keys)
-            ->map(function ($value) {
-                return trim($value, '"\' ');
-            })
-            ->toArray();
-        $attributes = array_merge($defaults, $attributes);
+                $parsedAttributes[$attribute] = trim($attributeValue, '"\' ');
+            }
+        }
 
-        return compact('content', 'attributes');
+        return [
+            'content' => trim($annotationContent),
+            'attributes' => $parsedAttributes
+        ];
     }
 }