Documentation

No matching pages.

API

Config

Facade

Lifecycle

Class: DatabaseManager

Full name: Azera\Db\DatabaseManager

Manages multiple SQL connections (roles) and their factories.

This class allows the definition of multiple SQL connections (e.g. "default", "analytics", "logging") and retrieval of them by role. The first role defined will be used as the default when requesting the default connection, but it can be changed by calling setDefault(). Each role can be defined with either a Database instance or a factory callable that returns a Database instance. The factory will only be called once per role, and the resulting Database instance will be cached for future use.

Public methods

set() ·

public function set(string $role, Azera\Db\Database|callable $factory): static

Define a SQL connection for a specific role.

Parameters

Name Type Default Description
$role string - The name of the role (e.g. "default", "analytics")
$factory Database|callable - A factory callable that returns a Database instance, or a Database instance directly

Return value

  • Type: static

setDefault() ·

public function setDefault(string $role): static

Set the default SQL role to use when requesting the default connection. By default, the first defined role will be used as the default.

Parameters

Name Type Default Description
$role string - The name of the role to set as default

Return value

  • Type: static

Throws

  • RuntimeException If the specified role is not defined

has() ·

public function has(string $role): bool

Check if a SQL role is defined.

Parameters

Name Type Default Description
$role string - The name of the role to check

Return value

  • Type: bool
  • Description: True if the role is defined, false otherwise

get() ·

public function get(string $role): Azera\Db\Database

Get the Database instance for a specific role.

Parameters

Name Type Default Description
$role string - The name of the role to retrieve

Return value

  • Type: Database
  • Description: The Database instance for the specified role

Throws

  • RuntimeException If the role is not defined or if the factory does not return a Database instance

getOrDefault() ·

public function getOrDefault(string $role): Azera\Db\Database

Get the Database instance for a specific role, or the default if the role is not defined.

Parameters

Name Type Default Description
$role string - The name of the role to retrieve

Return value

  • Type: Database
  • Description: The Database instance for the specified role, or the default if not defined

Throws

  • RuntimeException If no default Database is configured

peek() ·

public function peek(string $role): Azera\Db\Database|null

Get the Database instance for a role if it is already instantiated, without invoking its factory.

Lets diagnostics inspect live connection state (for example to find connections left inside a transaction) without opening every configured connection as a side effect.

Parameters

Name Type Default Description
$role string - The role to inspect.

Return value

  • Type: Database|null
  • Description: The instance, or null when the role is unknown or its
    factory has not run yet.

getDefault() ·

public function getDefault(): Azera\Db\Database

Get the default Database instance.

Return value

  • Type: Database
  • Description: The default Database instance

Throws

  • RuntimeException If no default Database is configured

roles() ·

public function roles(): array

Return the names of all registered SQL roles.

Return value

  • Type: array
  • Description: List of role names (e.g. ["default", "read", "write"]).

defaultRole() ·

public function defaultRole(): string|null

Return the name of the default SQL role, or null if none is configured.

Return value

  • Type: string|null
  • Description: The default role name.

Back to the Index