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);
}
doc_Drupal
2016-10-29 09:01:06
Comments
Leave a Comment

Please login to continue.