class DatabaseDriver implements CanListStoredFeatures, CanSetManyFeaturesForScopes, Driver, UsesDatabaseConnection

Constants

CREATED_AT

The name of the "created at" column.

UPDATED_AT

The name of the "updated at" column.

QUERY_GROUP_LIMIT

The maximum number of feature groups in one read query.

Half of SQLite's default expression depth limit of 1000.

QUERY_BINDING_LIMIT

The maximum number of bindings in one read query.

WRITE_ROW_LIMIT

The maximum number of rows in one write statement.

Properties

protected DatabaseManager $db

The database manager.

protected string|null $connection

The store's connection name, or null for the default connection.

protected string $table

The store's table.

protected null|FeaturePartition $partitionResolver

The resolver of the store's current partition.

protected StoredValueCache|null $cache

The shared cache of stored values.

protected null|FeaturePartition): void $writeGuard

The callback that guards database writes.

protected array<string, callable(mixed): mixed> $featureStateResolvers

The feature state resolvers.

protected stdClass $unknownFeatureValue

The sentinel value for unknown features.

Methods

__construct(DatabaseManager $db, string|null $connection, string $table, Closure|null $partitionResolver, StoredValueCache|null $cache, array $featureStateResolvers, Closure|null $writeGuard = null)

Create a new driver instance.

void
define(string $feature, callable $resolver)

Define an initial feature flag state resolver.

array
defined()

Retrieve the names of all defined features.

array
stored()

Retrieve the names of all stored features.

array
getAll(array $features)

Get multiple feature flag values.

mixed
get(string $feature, mixed $scope)

Retrieve a feature flag's value.

array
retrieveValues(array $pairs)

Retrieve the values of the given features and scopes, storing initial values for any that are missing.

array
readStoredValues(array $pairs, FeaturePartition|null $partition, bool $useWritePdo = false, bool $lock = false)

Read the stored JSON values of the given features and scopes.

mixed
resolveValue(string $feature, mixed $scope)

Determine the initial value for a given feature and scope.

void
set(string $feature, mixed $scope, mixed $value)

Set a feature flag's value.

void
setAll(array $features)

Set multiple feature flag values.

void
setForAllScopes(string $feature, mixed $value)

Set a feature flag's value for all scopes.

array
insertMany(FeaturePartition|null $partition, array $pairs)

Insert initial values for the given features and scopes, returning the values that are stored.

bool
insertCandidates(FeaturePartition|null $partition, array $rows)

Insert the candidate rows, skipping those that already exist, and determine whether every candidate was inserted.

array
row(FeaturePartition|null $partition, string $feature, string $scope, string $value, CarbonImmutable $now)

Get a row for the given feature value.

array
uniqueBy(FeaturePartition|null $partition)

Get the columns that identify a stored value.

void
delete(string $feature, mixed $scope)

Delete a feature flag's value.

void
purge(array|null $features)

Purge the given feature from storage.

void
flushSharedCache()

Invalidate the current partition's shared cached values after the transaction commits.

void
invalidate(FeaturePartition|null $partition, array|null $scopes = null)

Rotate the shared cache tokens of the given scopes, or of the whole partition, once the write commits.

string
connectionName()

Get the name of the store's current connection.

partition()

Get the store's current partition.

newQuery()

Create a new table query.

newPartitionQuery(FeaturePartition|null $partition)

Create a new table query limited to the given partition.

connection()

The database connection.

Details

at line 111
__construct(DatabaseManager $db, string|null $connection, string $table, Closure|null $partitionResolver, StoredValueCache|null $cache, array $featureStateResolvers, Closure|null $writeGuard = null)

Create a new driver instance.

Parameters

DatabaseManager $db
string|null $connection
string $table
Closure|null $partitionResolver
StoredValueCache|null $cache
array $featureStateResolvers
Closure|null $writeGuard

at line 139
void define(string $feature, callable $resolver)

Define an initial feature flag state resolver.

On a shared store, the definition persists for the worker lifetime and applies to every subsequent request.

Parameters

string $feature
callable $resolver

Return Value

void

at line 150
array defined()

Retrieve the names of all defined features.

Return Value

array

at line 161
array stored()

Retrieve the names of all stored features.

Return Value

array

at line 177
array getAll(array $features)

Get multiple feature flag values.

Parameters

array $features

Return Value

array

at line 208
mixed get(string $feature, mixed $scope)

Retrieve a feature flag's value.

Parameters

string $feature
mixed $scope

Return Value

mixed

at line 219
protected array retrieveValues(array $pairs)

Retrieve the values of the given features and scopes, storing initial values for any that are missing.

Parameters

array $pairs

scopes keyed by their serialized identifiers

Return Value

array

at line 291
protected array readStoredValues(array $pairs, FeaturePartition|null $partition, bool $useWritePdo = false, bool $lock = false)

Read the stored JSON values of the given features and scopes.

Features that share the same scopes are combined into one predicate, and the predicates are packed into as few statements as the group and binding limits allow.

Parameters

array $pairs

scopes keyed by their serialized identifiers

FeaturePartition|null $partition
bool $useWritePdo
bool $lock

Return Value

array

at line 374
protected mixed resolveValue(string $feature, mixed $scope)

Determine the initial value for a given feature and scope.

Parameters

string $feature
mixed $scope

Return Value

mixed

at line 390
void set(string $feature, mixed $scope, mixed $value)

Set a feature flag's value.

Parameters

string $feature
mixed $scope
mixed $value

Return Value

void

at line 411
void setAll(array $features)

Set multiple feature flag values.

Parameters

array $features

Return Value

void

at line 455
void setForAllScopes(string $feature, mixed $value)

Set a feature flag's value for all scopes.

Parameters

string $feature
mixed $value

Return Value

void

at line 483
protected array insertMany(FeaturePartition|null $partition, array $pairs)

Insert initial values for the given features and scopes, returning the values that are stored.

When every candidate is known to be inserted, the resolved values are returned as they are. Otherwise the rows are read back and their stored values returned, since a matching row may belong to a concurrent insert. Rows that vanish before they can be read back, such as through a concurrent purge, are resolved and inserted again, at most twice.

Parameters

FeaturePartition|null $partition
array $pairs

scopes keyed by their serialized identifiers

Return Value

array

the values, and the stored JSON of those that are stored

Exceptions

RuntimeException

at line 568
protected bool insertCandidates(FeaturePartition|null $partition, array $rows)

Insert the candidate rows, skipping those that already exist, and determine whether every candidate was inserted.

Parameters

FeaturePartition|null $partition
array $rows

Return Value

bool

at line 599
protected array row(FeaturePartition|null $partition, string $feature, string $scope, string $value, CarbonImmutable $now)

Get a row for the given feature value.

Parameters

FeaturePartition|null $partition
string $feature
string $scope
string $value
CarbonImmutable $now

Return Value

array

at line 621
protected array uniqueBy(FeaturePartition|null $partition)

Get the columns that identify a stored value.

Parameters

FeaturePartition|null $partition

Return Value

array

at line 629
void delete(string $feature, mixed $scope)

Delete a feature flag's value.

Parameters

string $feature
mixed $scope

Return Value

void

at line 646
void purge(array|null $features)

Purge the given feature from storage.

Parameters

array|null $features

Return Value

void

at line 670
void flushSharedCache()

Invalidate the current partition's shared cached values after the transaction commits.

Return Value

void

at line 680
protected void invalidate(FeaturePartition|null $partition, array|null $scopes = null)

Rotate the shared cache tokens of the given scopes, or of the whole partition, once the write commits.

Parameters

FeaturePartition|null $partition
array|null $scopes

Return Value

void

at line 698
string connectionName()

Get the name of the store's current connection.

Return Value

string

Exceptions

InvalidArgumentException

at line 713
protected FeaturePartition|null partition()

Get the store's current partition.

Return Value

FeaturePartition|null

at line 721
protected Builder newQuery()

Create a new table query.

Return Value

Builder

at line 729
protected Builder newPartitionQuery(FeaturePartition|null $partition)

Create a new table query limited to the given partition.

Parameters

FeaturePartition|null $partition

Return Value

Builder

at line 743
protected Connection connection()

The database connection.

Return Value

Connection