diff -u b/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php b/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php --- b/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php +++ b/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php @@ -8,9 +8,12 @@ namespace Drupal\migrate\Plugin\migrate\source; use Drupal\Core\Database\Database; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\State\StateInterface; use Drupal\migrate\Entity\MigrationInterface; use Drupal\migrate\Plugin\migrate\id_map\Sql; use Drupal\migrate\Plugin\MigrateIdMapInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Sources whose data may be fetched via DBTNG. @@ -21,7 +24,7 @@ * is present, it is used as a database connection information array to define * the connection. */ -abstract class SqlBase extends SourcePluginBase { +abstract class SqlBase extends SourcePluginBase implements ContainerFactoryPluginInterface { /** * @var \Drupal\Core\Database\Query\SelectInterface @@ -34,10 +37,31 @@ protected $database; /** + * State service for retrieving database info. + * + * @var \Drupal\Core\State\StateInterface + */ + protected $state; + + /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state) { parent::__construct($configuration, $plugin_id, $plugin_definition, $migration); + $this->state = $state; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $migration, + \Drupal::state() + ); } /** @@ -61,47 +85,44 @@ // See if the database info is in state - if not, fallback to // configuration. if (isset($this->configuration['database_state_key'])) { - // @todo: Inject the state service. - $database_state = \Drupal::state()->get($this->configuration['database_state_key']); - if (isset($database_state['target'])) { - $target = $database_state['target']; - } - else { - $target = 'default'; - } - if (isset($database_state['key'])) { - $key = $database_state['key']; - } - else { - $key = 'migrate'; - } - if (isset($database_state['database'])) { - Database::addConnectionInfo($key, $target, $database_state['database']); - } + $this->database = $this->setUpDatabase($this->state->get($this->configuration['database_state_key'])); } else { - if (isset($this->configuration['target'])) { - $target = $this->configuration['target']; - } - else { - $target = 'default'; - } - if (isset($this->configuration['key'])) { - $key = $this->configuration['key']; - } - else { - $key = 'migrate'; - } - if (isset($this->configuration['database'])) { - Database::addConnectionInfo($key, $target, $this->configuration['database']); - } + $this->database = $this->setUpDatabase($this->configuration); } - $this->database = Database::getConnection($target, $key); } return $this->database; } /** + * Get a connection to the referenced database, adding the connection if + * necessary. + * + * @param $database_info + * + * @return \Drupal\Core\Database\Connection + * The connection to use for this plugin's queries. + */ + protected function setUpDatabase($database_info) { + if (isset($database_info['target'])) { + $target = $database_info['target']; + } + else { + $target = 'default'; + } + if (isset($database_info['key'])) { + $key = $database_info['key']; + } + else { + $key = 'migrate'; + } + if (isset($database_info['database'])) { + Database::addConnectionInfo($key, $target, $database_info['database']); + } + return Database::getConnection($target, $key); + } + + /** * Wrapper for database select. */ protected function select($table, $alias = NULL, array $options = array()) { diff -u b/core/modules/migrate/src/Tests/SqlBaseTest.php b/core/modules/migrate/src/Tests/SqlBaseTest.php --- b/core/modules/migrate/src/Tests/SqlBaseTest.php +++ b/core/modules/migrate/src/Tests/SqlBaseTest.php @@ -97,7 +97,9 @@ /** * Override the constructor so we can create one easily. */ - public function __construct() {} + public function __construct() { + $this->state = \Drupal::state(); + } /** * Get the database without caching it. only in patch2: unchanged: --- a/core/modules/migrate/tests/src/Unit/MigrateSqlSourceTestCase.php +++ b/core/modules/migrate/tests/src/Unit/MigrateSqlSourceTestCase.php @@ -77,6 +77,7 @@ */ protected function setUp() { $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); + $state = $this->getMock('Drupal\Core\State\StateInterface'); $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); $migration = $this->getMigration(); @@ -86,7 +87,7 @@ protected function setUp() { // Setup the plugin. $plugin_class = static::PLUGIN_CLASS; - $plugin = new $plugin_class($this->migrationConfiguration['source'], $this->migrationConfiguration['source']['plugin'], array(), $migration, $entity_manager); + $plugin = new $plugin_class($this->migrationConfiguration['source'], $this->migrationConfiguration['source']['plugin'], array(), $migration, $state, $entity_manager); // Do some reflection to set the database and moduleHandler. $plugin_reflection = new \ReflectionClass($plugin); only in patch2: unchanged: --- a/core/modules/migrate_drupal/src/Plugin/migrate/source/DrupalSqlBase.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/DrupalSqlBase.php @@ -11,6 +11,7 @@ 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; @@ -51,8 +52,8 @@ /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityManagerInterface $entity_manager) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $migration); + 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; } @@ -89,6 +90,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $migration, + \Drupal::state(), $container->get('entity.manager') ); } only in patch2: unchanged: --- a/core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php @@ -8,6 +8,7 @@ namespace Drupal\migrate_drupal\Plugin\migrate\source; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\State\StateInterface; use Drupal\migrate\Entity\MigrationInterface; /** @@ -32,8 +33,8 @@ class Variable extends DrupalSqlBase { /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityManagerInterface $entity_manager) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $entity_manager); + 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, $entity_manager); $this->variables = $this->configuration['variables']; } only in patch2: unchanged: --- a/core/modules/migrate_drupal/tests/src/Unit/source/d6/Drupal6SqlBaseTest.php +++ b/core/modules/migrate_drupal/tests/src/Unit/source/d6/Drupal6SqlBaseTest.php @@ -68,8 +68,9 @@ class Drupal6SqlBaseTest extends MigrateTestCase { */ protected function setUp() { $plugin = 'placeholder_id'; + $state = $this->getmock('Drupal\Core\State\StateInterface'); $entity_manager = $this->getmock('Drupal\Core\Entity\EntityManagerInterface'); - $this->base = new TestDrupal6SqlBase($this->migrationConfiguration, $plugin, array(), $this->getMigration(), $entity_manager); + $this->base = new TestDrupal6SqlBase($this->migrationConfiguration, $plugin, array(), $this->getMigration(), $state, $entity_manager); $this->base->setDatabase($this->getDatabase($this->databaseContents)); }