Browse Source

Port `parseHuge` option to 3.x

Refs #10034
Mark Story 9 years ago
parent
commit
8aec91ed65
2 changed files with 23 additions and 3 deletions
  1. 10 3
      src/Utility/Xml.php
  2. 13 0
      tests/TestCase/Utility/XmlTest.php

+ 10 - 3
src/Utility/Xml.php

@@ -92,7 +92,9 @@ class Xml
      * - `readFile` Set to false to disable file reading. This is important to disable when
      *   putting user data into Xml::build(). If enabled local files will be read if they exist.
      *   Defaults to true for backwards compatibility reasons.
-     * - If using array as input, you can pass `options` from Xml::fromArray.
+     * - `parseHuge` Enable the `LIBXML_PARSEHUGE` flag.
+     *
+     * If using array as input, you can pass `options` from Xml::fromArray.
      *
      * @param string|array $input XML string, a path to a file, a URL or an array
      * @param string|array $options The options to use
@@ -104,7 +106,8 @@ class Xml
         $defaults = [
             'return' => 'simplexml',
             'loadEntities' => false,
-            'readFile' => true
+            'readFile' => true,
+            'parseHuge' => true,
         ];
         $options += $defaults;
 
@@ -142,9 +145,13 @@ class Xml
         if ($hasDisable && !$options['loadEntities']) {
             libxml_disable_entity_loader(true);
         }
+        $flags = LIBXML_NOCDATA;
+        if (!empty($options['parseHuge'])) {
+            $flags |= LIBXML_PARSEHUGE;
+        }
         try {
             if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
-                $xml = new SimpleXMLElement($input, LIBXML_NOCDATA);
+                $xml = new SimpleXMLElement($input, $flags);
             } else {
                 $xml = new DOMDocument();
                 $xml->loadXML($input);

+ 13 - 0
tests/TestCase/Utility/XmlTest.php

@@ -114,6 +114,19 @@ class XmlTest extends TestCase
     }
 
     /**
+     * test build() method with huge option
+     *
+     * @return void
+     */
+    public function testBuildHuge()
+    {
+        $xml = '<tag>value</tag>';
+        $obj = Xml::build($xml, array('parseHuge' => true));
+        $this->assertEquals('tag', $obj->getName());
+        $this->assertEquals('value', (string)$obj);
+    }
+
+    /**
      * Test that the readFile option disables local file parsing.
      *
      * @expectedException \Cake\Utility\Exception\XmlException