class ConnectionFactory

Properties

protected array<string, callable> $extensions

The custom connection resolvers.

Methods

__construct(Container $container)

Create a new connection factory instance.

make(array $config, string|null $name = null)

Establish a database connection based on the configuration.

callable|null
getExtension(array $config, string|null $name)

Get the extension resolver for a connection configuration.

void
extend(string $name, callable $resolver)

Register an extension connection resolver.

void
forgetExtension(string $name)

Remove an extension connection resolver.

makeSqliteFromSharedPdo(PDO $pdo, array $config, string|null $name = null)

Create an in-memory SQLite connection using a shared PDO.

makeSharedInMemorySqliteConnection(array $config, string|null $name = null)

Create the initial pooled in-memory SQLite connection.

array
parseConfig(array $config, string|null $name)

Parse and prepare the database configuration.

bool
hasReadConfig(array $config)

Determine if the configuration has a read side.

array
configForRead(array $config)

Get the single-role read configuration for a read / write connection.

createPdoConnectionFromConfig(array $config)

Create a PDO-backed connection from its configuration.

createSingleConnection(array $config)

Create a single PDO-backed connection instance.

createReadWriteConnection(array $config)

Create a read / write database connection instance.

createReadPdo(array $config)

Create a new PDO instance for reading.

array
getReadConfig(array $config)

Get the read configuration for a read / write connection.

array
getWriteConfig(array $config)

Get the write configuration for a read / write connection.

array
getReadWriteConfig(array $config, string $type)

Get a read / write level configuration.

array
mergeReadWriteConfig(array $config, array $merge)

Merge a configuration for a read / write connection.

createPdoResolver(array $config)

Create a new Closure that resolves to a PDO instance.

createPdoResolverWithHosts(array $config)

Create a new Closure that resolves to a PDO instance with a specific host or an array of hosts.

array
parseHosts(array $config)

Parse the hosts configuration item into an array.

createPdoResolverWithoutHosts(array $config)

Create a new Closure that resolves to a PDO instance where there is no configured host.

createConnector(array $config)

Create a connector instance based on the configuration.

createConnection(string $driver, PDO|Closure $connection, string $database, string $prefix = '', array $config = [])

Create a new connection instance.

Details

at line 35
__construct(Container $container)

Create a new connection factory instance.

Parameters

Container $container

at line 43
Connection make(array $config, string|null $name = null)

Establish a database connection based on the configuration.

Parameters

array $config
string|null $name

Return Value

Connection

at line 71
callable|null getExtension(array $config, string|null $name)

Get the extension resolver for a connection configuration.

Parameters

array $config
string|null $name

Return Value

callable|null

at line 86
void extend(string $name, callable $resolver)

Register an extension connection resolver.

Boot-only. The resolver persists in the singleton factory's extensions array for the worker lifetime and applies to every subsequent connection.

Parameters

string $name
callable $resolver

Return Value

void

at line 98
void forgetExtension(string $name)

Remove an extension connection resolver.

Boot or tests only. Mutates the singleton factory's extensions array; concurrent coroutines establishing connections may see the resolver removed mid-resolution.

Parameters

string $name

Return Value

void

at line 112
PdoConnection makeSqliteFromSharedPdo(PDO $pdo, array $config, string|null $name = null)

Create an in-memory SQLite connection using a shared PDO.

Used by connection pooling for in-memory SQLite where all pool slots must share the same PDO instance to see the same data. Without this, each pooled connection would get its own empty in-memory database.

Laravel-compatible PDO resolvers may return a custom PdoConnection subclass.

Parameters

PDO $pdo
array $config
string|null $name

Return Value

PdoConnection

at line 135
PdoConnection makeSharedInMemorySqliteConnection(array $config, string|null $name = null)

Create the initial pooled in-memory SQLite connection.

Parameters

array $config
string|null $name

Return Value

PdoConnection

at line 150
array parseConfig(array $config, string|null $name)

Parse and prepare the database configuration.

Parameters

array $config
string|null $name

Return Value

array

at line 160
bool hasReadConfig(array $config)

Determine if the configuration has a read side.

Parameters

array $config

Return Value

bool

at line 168
array configForRead(array $config)

Get the single-role read configuration for a read / write connection.

Parameters

array $config

Return Value

array

at line 184
protected PdoConnection createPdoConnectionFromConfig(array $config)

Create a PDO-backed connection from its configuration.

Parameters

array $config

Return Value

PdoConnection

at line 196
protected PdoConnection createSingleConnection(array $config)

Create a single PDO-backed connection instance.

Parameters

array $config

Return Value

PdoConnection

at line 212
protected PdoConnection createReadWriteConnection(array $config)

Create a read / write database connection instance.

Parameters

array $config

Return Value

PdoConnection

at line 224
protected Closure createReadPdo(array $config)

Create a new PDO instance for reading.

Parameters

array $config

Return Value

Closure

at line 232
protected array getReadConfig(array $config)

Get the read configuration for a read / write connection.

Parameters

array $config

Return Value

array

at line 246
protected array getWriteConfig(array $config)

Get the write configuration for a read / write connection.

Parameters

array $config

Return Value

array

at line 260
protected array getReadWriteConfig(array $config, string $type)

Get a read / write level configuration.

Parameters

array $config
string $type

Return Value

array

at line 270
protected array mergeReadWriteConfig(array $config, array $merge)

Merge a configuration for a read / write connection.

Parameters

array $config
array $merge

Return Value

array

at line 278
protected Closure createPdoResolver(array $config)

Create a new Closure that resolves to a PDO instance.

Parameters

array $config

Return Value

Closure

at line 290
protected Closure createPdoResolverWithHosts(array $config)

Create a new Closure that resolves to a PDO instance with a specific host or an array of hosts.

Parameters

array $config

Return Value

Closure

Exceptions

PDOException

at line 316
protected array parseHosts(array $config)

Parse the hosts configuration item into an array.

Parameters

array $config

Return Value

array

Exceptions

InvalidArgumentException

at line 330
protected Closure createPdoResolverWithoutHosts(array $config)

Create a new Closure that resolves to a PDO instance where there is no configured host.

Parameters

array $config

Return Value

Closure

at line 340
ConnectorInterface createConnector(array $config)

Create a connector instance based on the configuration.

Parameters

array $config

Return Value

ConnectorInterface

Exceptions

InvalidArgumentException

at line 364
protected PdoConnection createConnection(string $driver, PDO|Closure $connection, string $database, string $prefix = '', array $config = [])

Create a new connection instance.

Parameters

string $driver
PDO|Closure $connection
string $database
string $prefix
array $config

Return Value

PdoConnection

Exceptions

InvalidArgumentException