Skip to content

DatabaseManager

DatabaseManager allows you to setup your database connectivity before the request is handled.

This eliminates the need for a filter to manage database connections.

class DatabaseManager implements ContextComponentInterface

ImplementsContextComponentInterface
Since1.0.0
SourceDatabase/DatabaseManager.php
MethodDescription
getContext(): ContextRetrieve the current application context.
getDatabase(string $name = null): DatabaseRetrieve the database connection associated with this Database implementation.
getDatabaseName(Database $database): ?stringRetrieve the name of the given database instance.
getDefaultDatabaseName(): stringReturns the name of the default database.
initialize(Context $context, array<string, mixed> $parameters = []): voidInitialize this DatabaseManager.
recycleConnections(): voidProbe all managed database connections and null any that are stale.
shutdown(): voidExecute the shutdown procedure.
startup(): voidDo any necessary startup work after initialization.

final public function getContext(): Context

Retrieve the current application context.

Returns Context — The current Context instance.

public function getDatabase(string $name = null): Database

Retrieve the database connection associated with this Database implementation.

A database name.

ParameterTypeDescription
$namestringA database name.

Returns Database — A Database instance.

ThrowsWhen
DatabaseExceptionIf the requested database name does not exist.

public function getDatabaseName(Database $database): ?string

Retrieve the name of the given database instance.

The database to fetch the name of.

ParameterTypeDescription
$databaseDatabaseThe database to fetch the name of.

Returns ?``string — The name of the database, or null if it was not found.

public function getDefaultDatabaseName(): string

Returns the name of the default database.

Returns string — The name of the default database.

public function initialize(Context $context, array<string, mixed> $parameters = []): void

Initialize this DatabaseManager.

ParameterTypeDescription
$contextContextAn Context instance.
$parametersarray``<``string``, ``mixed``>An array of initialization parameters.
ThrowsWhen
InitializationExceptionIf an error occurs while initializing this DatabaseManager.

public function recycleConnections(): void

Probe all managed database connections and null any that are stale.

Called from Context::reset() instead of shutdown() so that this manager object stays alive across requests, avoiding the re-initialization cost on every request. Any connection that fails its ping() is nulled inside the database object; getConnection() will then reconnect lazily on the next use — which fixes “connection lost after laptop sleep” without a full restart.

public function shutdown(): void

Execute the shutdown procedure.

ThrowsWhen
DatabaseExceptionIf an error occurs while shutting down this DatabaseManager.

public function startup(): void

Do any necessary startup work after initialization.

This method is not called directly after initialize().