class DatabaseManager implements ConnectionResolverInterface mixin Connection

Traits

Constants

QUERY_DURATION_HANDLERS_CONTEXT_KEY

Context key for query duration handlers that have run in the current coroutine.

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected Connection> $connections

The active connection instances.

protected Closure $reconnector

The callback to be executed to reconnect to a database.

protected array<int, array{connection: string, key: string, threshold: float|int, handler: callable}> $queryDurationHandlers

All registered query duration handlers.

protected bool $queryDurationListenerRegistered

Indicates if the manager-level query duration listener has been registered.

Methods

int
secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

int
availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)

Get the "available at" UNIX timestamp.

parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)

If the given value is an interval, convert it to a DateTime instance.

int
currentTime()

Get the current system time as a UNIX timestamp.

string
runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically pass methods to the default connection.

__construct(Container $app, ConnectionFactory $factory)

Create a new database manager instance.

connection(UnitEnum|string|null $name = null)

Get a database connection instance.

resolveConnectionDirectly(string $name)

Resolve a connection directly without using the connection pool.

build(array $config)

Build a database connection instance from the given configuration.

connectUsing(string $name, array $config, bool $force = false)

Get a database connection instance from the given configuration.

makeConnection(ConnectionName|string $name)

Make the database connection instance.

array
configuration(ConnectionName|string $name)

Get the configuration for a connection.

configure(Connection $connection)

Prepare the database connection instance.

void
dispatchConnectionEstablishedEvent(Connection $connection)

Dispatch the ConnectionEstablished event if the event dispatcher is available.

void
purge(UnitEnum|string|null $name = null)

Disconnect from the given database and flush its pool.

void
disconnect(UnitEnum|string|null $name = null)

Disconnect from the given database.

reconnect(UnitEnum|string|null $name = null)

Reconnect to the given database.

mixed
usingConnection(UnitEnum|string $name, callable $callback)

Set the default database connection for the callback execution.

refreshPdoConnections(string $name)

Refresh the PDO connections on a given connection.

string|null
getDefaultConnection()

Get the default connection name.

void
whenQueryingForLongerThan(DateTimeInterface|CarbonInterval|float|int $threshold, callable $handler)

Register a callback to be invoked when the current connection queries for longer than a given amount of time.

void
allowQueryDurationHandlersToRunAgain()

Allow all the query duration handlers to run again for the current connection.

void
setDefaultConnection(string|null $name)

Set the default connection name for the current execution context.

mixed
configValue(string $key, mixed $default = null)

Get a config value.

string
getConnectionContextKey(string $name)

Get the context key for storing a connection.

array
connectionNameVariants(UnitEnum|string|null $name)

Get all coroutine/cache variants for a connection name.

string
getEffectiveConnectionName()

Get the current effective connection name.

void
registerQueryDurationListener()

Register the manager-level query duration listener.

string
queryDurationHandlerKey(int $key, string $connectionName)

Get the coroutine-local key for a query duration handler registration.

array
supportedDrivers()

Get all of the supported drivers.

array
availableDrivers()

Get all of the drivers that are actually available.

void
extend(string $name, callable $resolver)

Register an extension connection resolver.

void
forgetExtension(string $name)

Remove an extension connection resolver.

array
getConnections()

Return all of the created connections.

static void
purgeConnections()

Purge all connections on the current manager instance.

void
setReconnector(callable $reconnector)

Set the database reconnector callback.

setApplication(Application $app)

Set the application instance used by the manager.

static void
flushState()

Flush all static state.

Details

in InteractsWithTime at line 17
protected int secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

Parameters

DateInterval|DateTimeInterface|int $delay

Return Value

int

in InteractsWithTime at line 29
protected int availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)

Get the "available at" UNIX timestamp.

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

int

in InteractsWithTime at line 41
protected DateTimeInterface|int parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)

If the given value is an interval, convert it to a DateTime instance.

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

DateTimeInterface|int

in InteractsWithTime at line 57
protected int currentTime()

Get the current system time as a UNIX timestamp.

Return Value

int

in InteractsWithTime at line 65
protected string runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

Parameters

float $startTime
float|null $endTime

Return Value

string

in Macroable at line 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 736
mixed __call(string $method, array $parameters)

Dynamically pass methods to the default connection.

Parameters

string $method
array $parameters

Return Value

mixed

at line 78
__construct(Container $app, ConnectionFactory $factory)

Create a new database manager instance.

Parameters

Container $app
ConnectionFactory $factory

at line 102
ConnectionInterface connection(UnitEnum|string|null $name = null)

Get a database connection instance.

Delegates to ConnectionResolver for pooled, per-coroutine connection management. Resolves the default connection name here (checking Context for usingConnection override) before passing to the resolver.

Parameters

UnitEnum|string|null $name

Return Value

ConnectionInterface

at line 124
ConnectionInterface resolveConnectionDirectly(string $name)

internal  For use by SimpleConnectionResolver only
 

Resolve a connection directly without using the connection pool.

This method is used by SimpleConnectionResolver for testing and Capsule environments where connection pooling is not needed. It manages connections in the $connections array like Laravel's original DatabaseManager.

Parameters

string $name

Return Value

ConnectionInterface

at line 150
ConnectionInterface build(array $config)

Build a database connection instance from the given configuration.

Parameters

array $config

Return Value

ConnectionInterface

Exceptions

RuntimeException

at line 163
ConnectionInterface connectUsing(string $name, array $config, bool $force = false)

Get a database connection instance from the given configuration.

Parameters

string $name
array $config
bool $force

Return Value

ConnectionInterface

Exceptions

RuntimeException

at line 174
protected Connection makeConnection(ConnectionName|string $name)

Make the database connection instance.

Parameters

ConnectionName|string $name

Return Value

Connection

at line 187
protected array configuration(ConnectionName|string $name)

Get the configuration for a connection.

Parameters

ConnectionName|string $name

Return Value

array

Exceptions

InvalidArgumentException

at line 212
protected Connection configure(Connection $connection)

Prepare the database connection instance.

Parameters

Connection $connection

Return Value

Connection

at line 233
protected void dispatchConnectionEstablishedEvent(Connection $connection)

Dispatch the ConnectionEstablished event if the event dispatcher is available.

Parameters

Connection $connection

Return Value

void

at line 257
void purge(UnitEnum|string|null $name = null)

Disconnect from the given database and flush its pool.

In pooled mode, this disconnects the current coroutine's connection, clears its context key (so the next connection() call gets a fresh pooled connection), and flushes the pool. Use this when connection configuration has changed and you need to fully reset.

Boot or tests only. Flushes the shared pool used by every coroutine; concurrent coroutines holding pooled connections will release them back into a discarded pool, and the current coroutine may briefly hold two pooled connections (the old one releases via defer at coroutine end).

Parameters

UnitEnum|string|null $name

Return Value

void

at line 306
void disconnect(UnitEnum|string|null $name = null)

Disconnect from the given database.

In pooled mode, this nulls the PDOs on the current coroutine's connection (if one exists), forcing a reconnect on the next query. Does not clear context or affect the pool - the connection is still released at coroutine end.

In non-pooled mode (SimpleConnectionResolver), disconnects the connection stored in the $connections array.

Parameters

UnitEnum|string|null $name

Return Value

void

at line 336
Connection reconnect(UnitEnum|string|null $name = null)

Reconnect to the given database.

In pooled mode, if this coroutine already has a connection, reconnects its PDOs and returns it. In non-pooled mode, refreshes the existing connection's PDOs in-place. Otherwise gets a fresh connection.

Parameters

UnitEnum|string|null $name

Return Value

Connection

at line 381
mixed usingConnection(UnitEnum|string $name, callable $callback)

Set the default database connection for the callback execution.

Uses Context for coroutine-safe state management, ensuring concurrent requests don't interfere with each other's default connection.

Parameters

UnitEnum|string $name
callable $callback

Return Value

mixed

at line 404
protected Connection refreshPdoConnections(string $name)

Refresh the PDO connections on a given connection.

Parameters

string $name

Return Value

Connection

at line 421
string|null getDefaultConnection()

Get the default connection name.

Checks Context first for per-coroutine override (from usingConnection()), then falls back to the global config default.

Return Value

string|null

at line 435
void whenQueryingForLongerThan(DateTimeInterface|CarbonInterval|float|int $threshold, callable $handler)

Register a callback to be invoked when the current connection queries for longer than a given amount of time.

Boot-only. The callback persists on the database manager for the worker lifetime and affects every subsequent request for the same connection.

Parameters

DateTimeInterface|CarbonInterval|float|int $threshold
callable $handler

Return Value

void

at line 461
void allowQueryDurationHandlersToRunAgain()

Allow all the query duration handlers to run again for the current connection.

Return Value

void

at line 487
void setDefaultConnection(string|null $name)

Set the default connection name for the current execution context.

Writes to coroutine Context so concurrent requests in the same Swoole worker are not affected. A null value clears the override and getDefaultConnection() falls back to config('database.default').

Parameters

string|null $name

Return Value

void

at line 499
protected mixed configValue(string $key, mixed $default = null)

Get a config value.

Parameters

string $key
mixed $default

Return Value

mixed

at line 512
protected string getConnectionContextKey(string $name)

Get the context key for storing a connection.

Uses the same format as ConnectionResolver for consistency.

Parameters

string $name

Return Value

string

at line 522
protected array connectionNameVariants(UnitEnum|string|null $name)

Get all coroutine/cache variants for a connection name.

Parameters

UnitEnum|string|null $name

Return Value

array

at line 540
protected string getEffectiveConnectionName()

Get the current effective connection name.

Return Value

string

at line 548
protected void registerQueryDurationListener()

Register the manager-level query duration listener.

Return Value

void

at line 598
protected string queryDurationHandlerKey(int $key, string $connectionName)

Get the coroutine-local key for a query duration handler registration.

Parameters

int $key
string $connectionName

Return Value

string

at line 608
array supportedDrivers()

Get all of the supported drivers.

Return Value

array

at line 618
array availableDrivers()

Get all of the drivers that are actually available.

Return Value

array

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

Register an extension connection resolver.

Extensions are stored on the ConnectionFactory so they are consulted by both the pooled path (PooledConnection → factory) and the non-pooled path (DatabaseManager → factory).

Boot-only. The resolver persists on the singleton ConnectionFactory for the worker lifetime and applies to every subsequent connection.

Parameters

string $name
callable $resolver

Return Value

void

at line 648
void forgetExtension(string $name)

Remove an extension connection resolver.

Boot or tests only. Mutates the singleton ConnectionFactory's extension registry; concurrent coroutines establishing connections may see the resolver removed mid-resolution.

Parameters

string $name

Return Value

void

at line 663
array getConnections()

Return all of the created connections.

Note: In Hypervel's pooled connection mode, connections are stored per-coroutine in Context rather than in this array. This method returns an empty array in normal pooled operation. Use the pool infrastructure to inspect active connections if needed.

Return Value

array

at line 684
static void purgeConnections()

Purge all connections on the current manager instance.

Resolves the DatabaseManager from the container and disconnects every active connection. Safe to call when no container or no database manager exists (e.g., unit tests).

Uses bound() - not has() - because Application::has() falls through to class_exists(), so any string that case-insensitively matches a loaded PHP class name would falsely report bound. bound() only checks bindings/instances/aliases.

Boot or tests only. Calls purge() on every connection (flushes the shared pools); concurrent coroutines lose their cached connection references mid-request.

Return Value

void

at line 707
void setReconnector(callable $reconnector)

Set the database reconnector callback.

Boot-only. The reconnector persists on the singleton DatabaseManager for the worker lifetime and is invoked for every disconnected connection across all coroutines.

Parameters

callable $reconnector

Return Value

void

at line 718
DatabaseManager setApplication(Application $app)

Set the application instance used by the manager.

Tests only. Swaps the singleton's application reference; per-request use races across coroutines and breaks every concurrent database operation.

Parameters

Application $app

Return Value

DatabaseManager

at line 728
static void flushState()

Flush all static state.

Return Value

void