Ver código fonte

Add support for nested arrays in the json manipulator, fixes #3296

Jordi Boggiano 10 anos atrás
pai
commit
55a6a1c3d4

+ 4 - 2
src/Composer/Json/JsonManipulator.php

@@ -18,6 +18,7 @@ namespace Composer\Json;
 class JsonManipulator
 {
     private static $RECURSE_BLOCKS;
+    private static $RECURSE_ARRAYS;
     private static $JSON_VALUE;
     private static $JSON_STRING;
 
@@ -29,8 +30,9 @@ class JsonManipulator
     {
         if (!self::$RECURSE_BLOCKS) {
             self::$RECURSE_BLOCKS = '(?:[^{}]*|\{(?:[^{}]*|\{(?:[^{}]*|\{(?:[^{}]*|\{[^{}]*\})*\})*\})*\})*';
-            self::$JSON_STRING = '"(?:\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\0-\x09\x0a-\x1f\\\\"])*"';
-            self::$JSON_VALUE = '(?:[0-9.]+|null|true|false|'.self::$JSON_STRING.'|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})';
+            self::$RECURSE_ARRAYS = '(?:[^\]]*|\[(?:[^\]]*|\[(?:[^\]]*|\[(?:[^\]]*|\[[^\]]*\])*\])*\])*\])*';
+            self::$JSON_STRING = '"(?:\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\0-\x09\x0a-\x1f\\\\"])+"';
+            self::$JSON_VALUE = '(?:[0-9.]+|null|true|false|'.self::$JSON_STRING.'|\['.self::$RECURSE_ARRAYS.'\]|\{'.self::$RECURSE_BLOCKS.'\})';
         }
 
         $contents = trim($contents);

+ 66 - 0
tests/Composer/Test/Json/JsonManipulatorTest.php

@@ -228,6 +228,54 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
         "foo": "qux"
     }
 }
+'
+            ),
+            array(
+                '{
+    "repositories": [{
+        "type": "package",
+        "package": {
+            "bar": "ba[z",
+            "dist": {
+                "url": "http...",
+                "type": "zip"
+            },
+            "autoload": {
+                "classmap": [ "foo/bar" ]
+            }
+        }
+    }],
+    "require": {
+        "php": "5.*"
+    },
+    "require-dev": {
+        "foo": "bar"
+    }
+}',
+                'require-dev',
+                'foo',
+                'qux',
+                '{
+    "repositories": [{
+        "type": "package",
+        "package": {
+            "bar": "ba[z",
+            "dist": {
+                "url": "http...",
+                "type": "zip"
+            },
+            "autoload": {
+                "classmap": [ "foo/bar" ]
+            }
+        }
+    }],
+    "require": {
+        "php": "5.*"
+    },
+    "require-dev": {
+        "foo": "qux"
+    }
+}
 '
             ),
         );
@@ -468,6 +516,24 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
             "package": { "bar": "ba}z" }
         }
     }
+}',
+                'bar',
+                false
+            ),
+            'fails on deep arrays with borked texts' => array(
+                '{
+    "repositories": [{
+        "package": { "bar": "ba[z" }
+    }]
+}',
+                'bar',
+                false
+            ),
+            'fails on deep arrays with borked texts2' => array(
+                '{
+    "repositories": [{
+        "package": { "bar": "ba]z" }
+    }]
 }',
                 'bar',
                 false