From 7b2aca5dc55577efed35558320d590c2504b9eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Delr=C3=A9?= Date: Mon, 11 May 2026 00:15:18 +0200 Subject: [PATCH] feat: add libxml flags parameter to FluidXml::load() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Large XML files (or files served over HTTP) can exceed libxml's default parser limits, causing load() to silently return empty data. Pass an optional $flags int to load() and forward it to DOMDocument::load(), which lets callers opt into LIBXML_PARSEHUGE, LIBXML_COMPACT, or any other libxml flag as needed. The implementation also switches from file_get_contents() + loadXML() to DOMDocument::load() directly, which avoids loading the whole file into a PHP string before parsing. Closes #52 Signed-off-by: Guillaume Delré --- source/FluidXml/FluidXml.php | 14 +++++++------- specs/FluidXml.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/source/FluidXml/FluidXml.php b/source/FluidXml/FluidXml.php index 1f8b11d..254a550 100644 --- a/source/FluidXml/FluidXml.php +++ b/source/FluidXml/FluidXml.php @@ -26,17 +26,17 @@ class FluidXml implements FluidInterface private ?\FluidXml\FluidContext $context = null; private $contextEl; - public static function load($document) + public static function load($document, int $flags = 0) { - $file = $document; - $document = \file_get_contents($file); + $dom = new \DOMDocument(); + $dom->formatOutput = true; + $dom->preserveWhiteSpace = false; - // file_get_contents() returns false in case of error. - if (! $document) { - throw new \Exception("File '$file' not accessible."); + if (! @$dom->load($document, $flags)) { + throw new \Exception("File '$document' not accessible."); } - return (new FluidXml(null))->addChild($document); + return (new FluidXml(null))->addChild($dom); } public function __construct(...$arguments) diff --git a/specs/FluidXml.php b/specs/FluidXml.php index 6cac1ef..a331203 100644 --- a/specs/FluidXml.php +++ b/specs/FluidXml.php @@ -142,6 +142,20 @@ assert_is_a($actual, \Exception::class); }); + + it('should accept libxml flags', function () { + $file = "{$this->out_dir}.test_load_flags.xml"; + $doc = "\n" + . " value\n" + . ""; + + \file_put_contents($file, $doc); + $xml = FluidXml::load($file, \LIBXML_PARSEHUGE); + \unlink($file); + + $expected = $doc; + assert_equal_xml($xml, $expected); + }); }); if (\version_compare(\phpversion(), '7', '>=')) {