You are here

abstract class DataParserPluginBase in Migrate Plus 8

Same name and namespace in other branches
  1. 8.5 src/DataParserPluginBase.php \Drupal\migrate_plus\DataParserPluginBase
  2. 8.2 src/DataParserPluginBase.php \Drupal\migrate_plus\DataParserPluginBase
  3. 8.3 src/DataParserPluginBase.php \Drupal\migrate_plus\DataParserPluginBase
  4. 8.4 src/DataParserPluginBase.php \Drupal\migrate_plus\DataParserPluginBase

Defines a base data parser implementation.

Hierarchy

Expanded class hierarchy of DataParserPluginBase

See also

\Drupal\migrate_plus\Annotation\DataParser

\Drupal\migrate_plus\DataParserPluginInterface

\Drupal\migrate_plus\DataParserPluginManager

Plugin API

1 file declares its use of DataParserPluginBase
Json.php in src/Plugin/migrate_plus/data_parser/Json.php
Contains Drupal\migrate_plus\Plugin\migrate_plus\data_parser\JSON.

File

src/DataParserPluginBase.php, line 21
Contains \Drupal\migrate_plus\DataParserPluginBase.

Namespace

Drupal\migrate_plus
View source
abstract class DataParserPluginBase extends PluginBase implements DataParserPluginInterface {

  /**
   * List of source urls.
   *
   * @var string[]
   */
  protected $urls;

  /**
   * Index of the currently-open url.
   *
   * @var int
   */
  protected $activeUrl;

  /**
   * String indicating how to select an item's data from the source.
   *
   * @var string
   */
  protected $itemSelector;

  /**
   * Current item when iterating.
   *
   * @var mixed
   */
  protected $currentItem = NULL;

  /**
   * Value of the ID for the current item when iterating.
   *
   * @var string
   */
  protected $currentId = NULL;

  /**
   * The data retrieval client.
   *
   * @var \Drupal\migrate_plus\DataFetcherPluginInterface
   */
  protected $dataFetcher;

  /**
   * {@inheritdoc}
   */
  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'];
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * Returns the initialized data fetcher plugin.
   *
   * @return \Drupal\migrate_plus\DataFetcherPluginInterface
   *   The data fetcher plugin.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function rewind() {
    $this->activeUrl = NULL;
    $this
      ->next();
  }

  /**
   * Implementation of Iterator::next().
   */
  public function next() {
    $this->currentItem = $this->currentId = NULL;
    if (is_null($this->activeUrl)) {
      if (!$this
        ->nextSource()) {

        // No data to import.
        return;
      }
    }

    // At this point, we have a valid open source url, try to fetch a row from
    // it.
    $this
      ->fetchNextRow();

    // If there was no valid row there, try the next url (if any).
    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];
      }
    }
  }

  /**
   * Opens the specified URL.
   *
   * @param $url
   *   URL to open.
   *
   * @return bool
   *   TRUE if the URL was successfully opened, FALSE otherwise.
   */
  protected abstract function openSourceUrl($url);

  /**
   * Retrieves the next row of data from the open source URL, populating
   * currentItem.
   */
  protected abstract function fetchNextRow();

  /**
   * Advances the data parser to the next source url.
   *
   * @return bool
   *   TRUE if a valid source URL was opened
   */
  protected function nextSource() {
    while ($this->activeUrl === NULL || count($this->urls) - 1 > $this->activeUrl) {
      if (is_null($this->activeUrl)) {
        $this->activeUrl = 0;
      }
      else {

        // Increment the activeUrl so we try to load the next source.
        $this->activeUrl = $this->activeUrl + 1;
        if ($this->activeUrl >= count($this->urls)) {
          return FALSE;
        }
      }
      if ($this
        ->openSourceUrl($this->urls[$this->activeUrl])) {

        // We have a valid source.
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function current() {
    return $this->currentItem;
  }

  /**
   * {@inheritdoc}
   */
  public function key() {
    return $this->currentId;
  }

  /**
   * {@inheritdoc}
   */
  public function valid() {
    return !empty($this->currentItem);
  }

  /**
   * {@inheritdoc}
   */
  public function count() {
    $count = 0;
    foreach ($this as $item) {
      $count++;
    }
    return $count;
  }

  /**
   * Return the selectors used to populate each configured field.
   *
   * @return string[]
   *   Array of selectors, keyed by field name.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DataParserPluginBase::$activeUrl protected property Index of the currently-open url.
DataParserPluginBase::$currentId protected property Value of the ID for the current item when iterating.
DataParserPluginBase::$currentItem protected property Current item when iterating.
DataParserPluginBase::$dataFetcher protected property The data retrieval client.
DataParserPluginBase::$itemSelector protected property String indicating how to select an item's data from the source.
DataParserPluginBase::$urls protected property List of source urls.
DataParserPluginBase::count public function
DataParserPluginBase::create public static function
DataParserPluginBase::current public function
DataParserPluginBase::fetchNextRow abstract protected function Retrieves the next row of data from the open source URL, populating currentItem. 1
DataParserPluginBase::fieldSelectors protected function Return the selectors used to populate each configured field.
DataParserPluginBase::getDataFetcherPlugin public function Returns the initialized data fetcher plugin.
DataParserPluginBase::key public function
DataParserPluginBase::next public function Implementation of Iterator::next().
DataParserPluginBase::nextSource protected function Advances the data parser to the next source url.
DataParserPluginBase::openSourceUrl abstract protected function Opens the specified URL. 1
DataParserPluginBase::rewind public function
DataParserPluginBase::valid public function
DataParserPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.