abstract class DataParserPluginBase in Migrate Plus 8
Same name and namespace in other branches
- 8.5 src/DataParserPluginBase.php \Drupal\migrate_plus\DataParserPluginBase
- 8.2 src/DataParserPluginBase.php \Drupal\migrate_plus\DataParserPluginBase
- 8.3 src/DataParserPluginBase.php \Drupal\migrate_plus\DataParserPluginBase
- 8.4 src/DataParserPluginBase.php \Drupal\migrate_plus\DataParserPluginBase
Defines a base data parser implementation.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate_plus\DataParserPluginBase implements DataParserPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DataParserPluginBase
See also
\Drupal\migrate_plus\Annotation\DataParser
\Drupal\migrate_plus\DataParserPluginInterface
\Drupal\migrate_plus\DataParserPluginManager
1 file declares its use of DataParserPluginBase
File
- src/
DataParserPluginBase.php, line 21 - Contains \Drupal\migrate_plus\DataParserPluginBase.
Namespace
Drupal\migrate_plusView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DataParserPluginBase:: |
protected | property | Index of the currently-open url. | |
DataParserPluginBase:: |
protected | property | Value of the ID for the current item when iterating. | |
DataParserPluginBase:: |
protected | property | Current item when iterating. | |
DataParserPluginBase:: |
protected | property | The data retrieval client. | |
DataParserPluginBase:: |
protected | property | String indicating how to select an item's data from the source. | |
DataParserPluginBase:: |
protected | property | List of source urls. | |
DataParserPluginBase:: |
public | function | ||
DataParserPluginBase:: |
public static | function | ||
DataParserPluginBase:: |
public | function | ||
DataParserPluginBase:: |
abstract protected | function | Retrieves the next row of data from the open source URL, populating currentItem. | 1 |
DataParserPluginBase:: |
protected | function | Return the selectors used to populate each configured field. | |
DataParserPluginBase:: |
public | function | Returns the initialized data fetcher plugin. | |
DataParserPluginBase:: |
public | function | ||
DataParserPluginBase:: |
public | function | Implementation of Iterator::next(). | |
DataParserPluginBase:: |
protected | function | Advances the data parser to the next source url. | |
DataParserPluginBase:: |
abstract protected | function | Opens the specified URL. | 1 |
DataParserPluginBase:: |
public | function | ||
DataParserPluginBase:: |
public | function | ||
DataParserPluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |