DataParserPluginBase.php in Migrate Plus 8.2
File
src/DataParserPluginBase.php
View source
<?php
namespace Drupal\migrate_plus;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class DataParserPluginBase extends PluginBase implements DataParserPluginInterface {
protected $urls;
protected $activeUrl;
protected $itemSelector;
protected $currentItem = NULL;
protected $currentId = NULL;
protected $dataFetcher;
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->urls = $configuration['urls'];
$this->itemSelector = $configuration['item_selector'];
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition);
}
public function getDataFetcherPlugin() {
if (!isset($this->dataFetcherPlugin)) {
$this->dataFetcherPlugin = \Drupal::service('plugin.manager.migrate_plus.data_fetcher')
->createInstance($this->configuration['data_fetcher_plugin'], $this->configuration);
}
return $this->dataFetcherPlugin;
}
public function rewind() {
$this->activeUrl = NULL;
$this
->next();
}
public function next() {
$this->currentItem = $this->currentId = NULL;
if (is_null($this->activeUrl)) {
if (!$this
->nextSource()) {
return;
}
}
$this
->fetchNextRow();
if (is_null($this->currentItem)) {
if ($this
->nextSource()) {
$this
->fetchNextRow();
}
}
if ($this
->valid()) {
foreach ($this->configuration['ids'] as $id_field_name => $id_info) {
$this->currentId[$id_field_name] = $this->currentItem[$id_field_name];
}
}
}
protected abstract function openSourceUrl($url);
protected abstract function fetchNextRow();
protected function nextSource() {
while ($this->activeUrl === NULL || count($this->urls) - 1 > $this->activeUrl) {
if (is_null($this->activeUrl)) {
$this->activeUrl = 0;
}
else {
$this->activeUrl = $this->activeUrl + 1;
if ($this->activeUrl >= count($this->urls)) {
return FALSE;
}
}
if ($this
->openSourceUrl($this->urls[$this->activeUrl])) {
return TRUE;
}
}
return FALSE;
}
public function current() {
return $this->currentItem;
}
public function key() {
return $this->currentId;
}
public function valid() {
return !empty($this->currentItem);
}
public function count() {
$count = 0;
foreach ($this as $item) {
$count++;
}
return $count;
}
protected function fieldSelectors() {
$fields = [];
foreach ($this->configuration['fields'] as $field_info) {
if (isset($field_info['selector'])) {
$fields[$field_info['name']] = $field_info['selector'];
}
}
return $fields;
}
}