From c76853c9aa9ab3dfe0a861753cd0c76dd857b853 Mon Sep 17 00:00:00 2001
From: Matroskeen <matroskeen@3426249.no-reply.drupal.org>
Date: Wed, 17 Nov 2021 14:34:51 +0000
Subject: [PATCH] Allow currently processed/parsed URL to be read from data
 parser plugin.

---
 src/DataParserPluginBase.php                  |  9 ++++++++
 src/DataParserPluginInterface.php             |  8 +++++++
 tests/data/simple_xml_current_url1.xml        | 14 +++++++++++
 tests/data/simple_xml_current_url2.xml        |  9 ++++++++
 .../data_parser/SimpleXmlTest.php             | 23 +++++++++++++++++++
 5 files changed, 63 insertions(+)
 create mode 100644 tests/data/simple_xml_current_url1.xml
 create mode 100644 tests/data/simple_xml_current_url2.xml

diff --git a/src/DataParserPluginBase.php b/src/DataParserPluginBase.php
index 3e8fd47a..7d187f31 100644
--- a/src/DataParserPluginBase.php
+++ b/src/DataParserPluginBase.php
@@ -181,6 +181,15 @@ abstract class DataParserPluginBase extends PluginBase implements DataParserPlug
     return $this->currentItem;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function currentUrl(): ?string {
+    $index = $this->activeUrl ?: \array_key_first($this->urls);
+
+    return $this->urls[$index] ?? NULL;
+  }
+
   /**
    * {@inheritdoc}
    */
diff --git a/src/DataParserPluginInterface.php b/src/DataParserPluginInterface.php
index 838ca9d3..fb6ed69b 100644
--- a/src/DataParserPluginInterface.php
+++ b/src/DataParserPluginInterface.php
@@ -12,4 +12,12 @@ namespace Drupal\migrate_plus;
  */
 interface DataParserPluginInterface extends \Iterator, \Countable {
 
+  /**
+   * Returns current source URL.
+   *
+   * @return string|null
+   *   The URL currently parsed on success, otherwise NULL.
+   */
+  public function currentUrl(): ?string;
+
 }
diff --git a/tests/data/simple_xml_current_url1.xml b/tests/data/simple_xml_current_url1.xml
new file mode 100644
index 00000000..0cffd289
--- /dev/null
+++ b/tests/data/simple_xml_current_url1.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<items>
+  <item id="1">
+    <values title="Values">
+      <value>Value 1</value>
+      <value>Value 2</value>
+    </values>
+  </item>
+  <item id="2">
+    <values title="Values">
+      <value>Value 1 (single)</value>
+    </values>
+  </item>
+</items>
diff --git a/tests/data/simple_xml_current_url2.xml b/tests/data/simple_xml_current_url2.xml
new file mode 100644
index 00000000..6d4b8e0a
--- /dev/null
+++ b/tests/data/simple_xml_current_url2.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<items>
+  <item id="3">
+    <values title="Values">
+      <value>Value 1</value>
+      <value>Value 2</value>
+    </values>
+  </item>
+</items>
diff --git a/tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php b/tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php
index 6505075a..77ab9768 100644
--- a/tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php
+++ b/tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php
@@ -86,6 +86,29 @@ class SimpleXmlTest extends KernelTestBase {
     ];
   }
 
+  /**
+   * Tests current URL of parsed XML item.
+   */
+  public function testCurrentUrl() {
+    $urls = [
+      $this->path . '/tests/data/simple_xml_current_url1.xml',
+      $this->path . '/tests/data/simple_xml_current_url2.xml',
+    ];
+    $this->configuration['urls'] = $urls;
+    /** @var \Drupal\migrate_plus\DataParserPluginInterface $parser */
+    $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration);
+
+    // First 2 items available in the first URL.
+    $parser->rewind();
+    $this->assertEquals($urls[0], $parser->currentUrl());
+    $parser->next();
+    $this->assertEquals($urls[0], $parser->currentUrl());
+
+    // Third item available in the second URL.
+    $parser->next();
+    $this->assertEquals($urls[1], $parser->currentUrl());
+  }
+
   /**
    * Tests reducing single values.
    */
-- 
GitLab