View source
<?php
namespace Drupal\migrate_drupal\Plugin\migrate\source;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Core\Entity\DependencyTrait;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\State\StateInterface;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Plugin\RequirementsInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class DrupalSqlBase extends SqlBase implements ContainerFactoryPluginInterface, RequirementsInterface, DependentPluginInterface {
use DependencyTrait;
protected $systemData;
protected $requirements = TRUE;
protected $entityManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state);
$this->entityManager = $entity_manager;
}
public function getSystemData() {
if (!isset($this->systemData)) {
$this->systemData = array();
try {
$results = $this
->select('system', 's')
->fields('s')
->execute();
foreach ($results as $result) {
$this->systemData[$result['type']][$result['name']] = $result;
}
} catch (\Exception $e) {
}
}
return $this->systemData;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('state'), $container
->get('entity.manager'));
}
public function checkRequirements() {
if ($this->pluginDefinition['requirements_met'] === TRUE) {
if (isset($this->pluginDefinition['source_provider'])) {
if ($this
->moduleExists($this->pluginDefinition['source_provider'])) {
if (isset($this->pluginDefinition['minimum_schema_version']) && !$this
->getModuleSchemaVersion($this->pluginDefinition['source_provider']) < $this->pluginDefinition['minimum_schema_version']) {
throw new RequirementsException('Required minimum schema version ' . $this->pluginDefinition['minimum_schema_version'], [
'minimum_schema_version' => $this->pluginDefinition['minimum_schema_version'],
]);
}
}
else {
throw new RequirementsException('Missing source provider ' . $this->pluginDefinition['source_provider'], [
'source_provider' => $this->pluginDefinition['source_provider'],
]);
}
}
}
}
protected function getModuleSchemaVersion($module) {
$system_data = $this
->getSystemData();
return isset($system_data['module'][$module]['schema_version']) ? $system_data['module'][$module]['schema_version'] : FALSE;
}
protected function moduleExists($module) {
$system_data = $this
->getSystemData();
return !empty($system_data['module'][$module]['status']);
}
protected function variableGet($name, $default) {
try {
$result = $this
->select('variable', 'v')
->fields('v', array(
'value',
))
->condition('name', $name)
->execute()
->fetchField();
} catch (\Exception $e) {
$result = FALSE;
}
return $result !== FALSE ? unserialize($result) : $default;
}
public function calculateDependencies() {
if (isset($this->configuration['constants']['entity_type'])) {
$this
->addDependency('module', $this->entityManager
->getDefinition($this->configuration['constants']['entity_type'])
->getProvider());
}
if (isset($this->configuration['constants']['module'])) {
$this
->addDependency('module', $this->configuration['constants']['module']);
}
return $this->dependencies;
}
}