From 8fd770d639a7b042331129cc335e1bcd64b1a1fb Mon Sep 17 00:00:00 2001
From: Stefan Korn <drupal@stefan-korn.de>
Date: Mon, 26 May 2025 16:47:23 +0200
Subject: [PATCH] Issue #3486188: [Regression] Clean up temporary files created
 by XML data parser plugin - without __destruct

---
 src/Plugin/migrate_plus/data_parser/Xml.php | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/Plugin/migrate_plus/data_parser/Xml.php b/src/Plugin/migrate_plus/data_parser/Xml.php
index 7ec22a69..47165bdc 100644
--- a/src/Plugin/migrate_plus/data_parser/Xml.php
+++ b/src/Plugin/migrate_plus/data_parser/Xml.php
@@ -87,6 +87,13 @@ class Xml extends DataParserPluginBase implements ContainerFactoryPluginInterfac
    */
   protected bool $prefixedName = FALSE;
 
+  /**
+   * Temporary file name of file that holds XML.
+   *
+   * @var string|null
+   */
+  protected ?string $tempFileName;
+
   /**
    * Constructs a new XML data parser.
    *
@@ -201,8 +208,8 @@ class Xml extends DataParserPluginBase implements ContainerFactoryPluginInterfac
 
     // Fetch the data and save it to a temporary file.
     $xml_data = $this->getDataFetcherPlugin()->getResponseContent($url);
-    $url = $this->fileSystem->tempnam('temporary://', 'file');
-    if (file_put_contents($url, $xml_data) === FALSE) {
+    $this->tempFileName = $this->fileSystem->tempnam('temporary://', 'file');
+    if (file_put_contents($this->tempFileName, $xml_data) === FALSE) {
       throw new MigrateException('Unable to save temporary XML');
     }
 
@@ -212,7 +219,15 @@ class Xml extends DataParserPluginBase implements ContainerFactoryPluginInterfac
     // that occur from attempting to load the XML string into an object here.
     libxml_clear_errors();
 
-    return $this->reader->open($url, NULL, \LIBXML_NOWARNING);
+    return $this->reader->open($this->tempFileName, NULL, \LIBXML_NOWARNING);
+  }
+
+  protected function nextSource(): bool {
+    $valid_source = parent::nextSource();
+    if (isset($this->tempFileName) && file_exists($this->tempFileName)) {
+      unlink($this->tempFileName);
+    }
+    return $valid_source;
   }
 
   /**
-- 
GitLab