Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Command/DbCommandBase.php \Drupal\Core\Command\DbCommandBase::getDatabaseConnection()
  2. 9 core/lib/Drupal/Core/Command/DbCommandBase.php \Drupal\Core\Command\DbCommandBase::getDatabaseConnection()

Parse input options decide on a database.

Parameters

\Symfony\Component\Console\Input\InputInterface $input: Input object.

Return value

\Drupal\Core\Database\Connection

3 calls to DbCommandBase::getDatabaseConnection()
DbCommandBaseTester::getDatabaseConnection in core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php
Parse input options decide on a database.
DbDumpCommand::execute in core/lib/Drupal/Core/Command/DbDumpCommand.php
DbImportCommand::execute in core/lib/Drupal/Core/Command/DbImportCommand.php
1 method overrides DbCommandBase::getDatabaseConnection()
DbCommandBaseTester::getDatabaseConnection in core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php
Parse input options decide on a database.

File

core/lib/Drupal/Core/Command/DbCommandBase.php, line 35

Class

DbCommandBase
Base command that abstracts handling of database connection arguments.

Namespace

Drupal\Core\Command

Code

protected function getDatabaseConnection(InputInterface $input) {

  // Load connection from a URL.
  if ($input
    ->getOption('database-url')) {

    // @todo this could probably be refactored to not use a global connection.
    // Ensure database connection isn't set.
    if (Database::getConnectionInfo('db-tools')) {
      throw new \RuntimeException('Database "db-tools" is already defined. Cannot define database provided.');
    }
    $info = Database::convertDbUrlToConnectionInfo($input
      ->getOption('database-url'), \Drupal::root());
    Database::addConnectionInfo('db-tools', 'default', $info);
    $key = 'db-tools';
  }
  else {
    $key = $input
      ->getOption('database');
  }

  // If they supplied a prefix, replace it in the connection information.
  $prefix = $input
    ->getOption('prefix');
  if ($prefix) {
    $info = Database::getConnectionInfo($key)['default'];
    $info['prefix'] = $prefix;
    Database::removeConnection($key);
    Database::addConnectionInfo($key, 'default', $info);
  }
  return Database::getConnection('default', $key);
}