W3cubDocs

/Drupal 8

protected function DbCommandBase::getDatabaseConnection

protected DbCommandBase::getDatabaseConnection(InputInterface $input)

Parse input options decide on a database.

Parameters

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

Return value

\Drupal\Core\Database\Connection

File

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

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']['default'] = $prefix;

    Database::removeConnection($key);
    Database::addConnectionInfo($key, 'default', $info);
  }

  return Database::getConnection('default', $key);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Command!DbCommandBase.php/function/DbCommandBase::getDatabaseConnection/8.1.x