You are here

class ConnectionFactory in Dbal connection 8

Provides a connection factory for connection to databases via doctrine/dbal.

Hierarchy

Expanded class hierarchy of ConnectionFactory

1 string reference to 'ConnectionFactory'
dbal.services.yml in ./dbal.services.yml
dbal.services.yml
1 service uses ConnectionFactory
dbal_connection_factory in ./dbal.services.yml
Drupal\dbal\ConnectionFactory

File

src/ConnectionFactory.php, line 13

Namespace

Drupal\dbal
View source
class ConnectionFactory {

  /**
   * Connection info.
   *
   * @var array
   */
  protected $info;

  /**
   * Connection cache.
   *
   * @var \Doctrine\DBAL\Connection[]
   */
  protected $cache;

  /**
   * Constructs a new ConnectionFactory object.
   */
  public function __construct() {
    $this->info = Database::getAllConnectionInfo();
  }

  /**
   * {@inheritdoc}
   */
  public function __sleep() {

    // We don't serialize the connection cache.
    return [
      'info',
    ];
  }

  /**
   * Gets a DBAL connection to the given target.
   *
   * @param string $target
   *   Database connection as named in global $databases parameter.
   *
   * @return \Doctrine\DBAL\Connection
   *   Requested connection.
   */
  public function get($target = 'default') {
    if (!isset($this->cache[$target])) {
      if (!isset($this->info[$target])) {

        // Fallback to default connection.
        $target = 'default';
      }
      $info = $this->info[$target]['default'];
      $options = [
        'dbname' => $info['database'],
        'user' => $info['username'] ?? '',
        'password' => $info['password'] ?? '',
        'driver' => 'pdo_' . $info['driver'],
      ];
      if (isset($info['host'])) {
        $options['host'] = $info['host'];
      }
      if (isset($info['unix_socket'])) {
        $options['unix_socket'] = $info['unix_socket'];
      }
      if (isset($info['port'])) {
        $options['port'] = $info['port'];
      }
      $this->cache[$target] = DriverManager::getConnection($options, new Configuration());
      if ($info['driver'] == 'sqlite') {
        $this
          ->sqliteDatabases($this->cache[$target], $info['prefix'], $info['database']);
      }
    }
    return $this->cache[$target];
  }

  /**
   * SQLite attach prefixes as databases.
   *
   * @param \Doctrine\DBAL\Driver\Connection $connection
   *   The connection to an SQLite database.
   * @param array $prefixes
   *   Drupal info array of database prefixes.
   * @param string $base_db
   *   The connected dbname.
   *
   * @see Drupal\Core\Database\Driver\sqlite\Connection::__construct()
   */
  protected function sqliteDatabases(Connection $connection, array $prefixes, $base_db) {
    $attached = [];
    foreach ($prefixes as $prefix) {
      if (!isset($attached[$prefix])) {
        $attached[$prefix] = TRUE;
        $query = $connection
          ->prepare('ATTACH DATABASE :db AS :prefix');
        if ($base_db == ':memory:') {
          $query
            ->execute([
            ':db' => $base_db,
            ':prefix' => $prefix,
          ]);
        }
        else {
          $query
            ->execute([
            ':db' => $base_db . '-' . $prefix,
            ':prefix' => $prefix,
          ]);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConnectionFactory::$cache protected property Connection cache.
ConnectionFactory::$info protected property Connection info.
ConnectionFactory::get public function Gets a DBAL connection to the given target.
ConnectionFactory::sqliteDatabases protected function SQLite attach prefixes as databases.
ConnectionFactory::__construct public function Constructs a new ConnectionFactory object.
ConnectionFactory::__sleep public function