function Database::addConnectionInfo
Adds database connection information for a given key/target.
This method allows to add new connections at runtime.
Under normal circumstances the preferred way to specify database credentials is via settings.php. However, this method allows them to be added at arbitrary times, such as during unit tests, when connecting to admin-defined third party databases, etc. Use \Drupal\Core\Database\Database::setActiveConnection to select the connection to use.
If the given key/target pair already exists, this method will be ignored.
Parameters
string $key: The database key.
string $target: The database target name.
array $info: The database connection information, as defined in settings.php. The structure of this array depends on the database driver it is connecting to.
\Composer\Autoload\ClassLoader $class_loader: The class loader. Used for adding the database driver to the autoloader if $info['autoload'] is set.
string $app_root: The app root.
See also
\Drupal\Core\Database\Database::setActiveConnection
10 calls to Database::addConnectionInfo()
- Database::setMultipleConnectionInfo in core/
lib/ Drupal/ Core/ Database/ Database.php - Sets connection information for multiple databases.
- DbCommandBase::getDatabaseConnection in core/
lib/ Drupal/ Core/ Command/ DbCommandBase.php - Parse input options decide on a database.
- DbLog::log in core/
modules/ dblog/ src/ Logger/ DbLog.php - Logs with an arbitrary level.
- install_database_errors in core/
includes/ install.core.inc - Checks a database connection and returns any errors.
- MigrationConfigurationTrait::getConnection in core/
modules/ migrate_drupal/ src/ MigrationConfigurationTrait.php - Gets the database connection for the source Drupal database.
File
-
core/
lib/ Drupal/ Core/ Database/ Database.php, line 270
Class
- Database
- Primary front-controller for the database system.
Namespace
Drupal\Core\DatabaseCode
public static final function addConnectionInfo($key, $target, array $info, $class_loader = NULL, $app_root = NULL) {
if (empty(self::$databaseInfo[$key][$target])) {
$info = self::parseConnectionInfo($info);
self::$databaseInfo[$key][$target] = $info;
// If the database driver is provided by a module, then its code may need
// to be instantiated prior to when the module's root namespace is added
// to the autoloader, because that happens during service container
// initialization but the container definition is likely in the database.
// Therefore, allow the connection info to specify an autoload directory
// for the driver.
if (isset($info['autoload']) && $class_loader && $app_root) {
$class_loader->addPsr4($info['namespace'] . '\\', $app_root . '/' . $info['autoload']);
// When the database driver is extending from other database drivers,
// then add autoload directory for the parent database driver modules
// as well.
if (!empty($info['dependencies'])) {
assert(is_array($info['dependencies']));
foreach ($info['dependencies'] as $dependency) {
if (isset($dependency['namespace']) && isset($dependency['autoload'])) {
$class_loader->addPsr4($dependency['namespace'] . '\\', $app_root . '/' . $dependency['autoload']);
}
}
}
}
}
}