class Decorator implements CanListStoredFeatures, CanSetManyFeaturesForScopes, Driver, HasFlushableCache mixin PendingScopedFeatureInteraction

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected string $name

The driver name.

protected Driver $driver

The driver being decorated.

protected Closure(): mixed $defaultScopeResolver

The default scope resolver.

protected null|FeaturePartition $partitionResolver

The resolver of the store's current partition.

protected Container $container

The container instance.

protected array<string, mixed> $nameMap

Map of feature names to their implementations.

protected array<class-string, string> $classNames

The feature names of feature classes.

protected array<string, class-string> $featureClasses

The implementation classes of features defined from feature classes.

protected ReflectionFunctionAbstract> $resolverFunctions

The reflected resolvers of defined features.

protected array<string, bool> $beforeHookAcceptsNull

Whether the before hooks of defined features accept a null scope.

Methods

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 $name, array $parameters)

Dynamically create a pending feature interaction.

__construct(string $name, Driver $driver, Closure $defaultScopeResolver, Closure|null $partitionResolver, Container $container)

Create a new driver decorator instance.

void
discover(string $namespace = 'App\Features', string|null $path = null)

Discover and register the application's feature classes.

void
define(string $feature, callable $resolver = null)

Define an initial feature flag state resolver.

mixed
resolve(string $feature, callable $resolver, mixed $scope)

Resolve the feature value.

bool
isResolverValidForScope(callable|string $resolver, mixed $scope)

Determine if the resolver can handle the scope.

resolverFunction(callable|string $resolver)

Reflect the function that resolves a feature.

bool
functionAcceptsScope(ReflectionFunctionAbstract $function, mixed $scope)

Determine if the function's scope parameter accepts the scope.

bool
typeAllowsScope(ReflectionType $type, mixed $scope, ReflectionFunctionAbstract $function)

Determine if the type can handle the scope.

bool
canHandleNullScope(callable|ReflectionFunctionAbstract $resolver)

Determine if the resolver accepts null scope.

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.

array
getAllMissing(array|string $features)

Get multiple feature flag values that are missing.

array
resolveFeatures(array $features, PennantState $state, string $connection, string $partition)

Resolve the values of the given features through their before hooks or the driver, and memoize them.

array
normalizeFeaturesToLoad(array|string $features)

Normalize the features to load.

mixed
get(string $feature, mixed $scope)

Retrieve a feature flag's value.

mixed
resolveBeforeHook(string $feature, mixed $scope, callable $hook)

Resolve the before hook value.

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

Set a feature flag's value.

void
setAll(array $features)

Set multiple feature flag values.

globally()

Create a pending feature interaction for the global scope, ignoring the default scope resolver.

void
activateForEveryone(UnitEnum|string|array $feature, mixed $value = true)

Activate the feature for everyone.

void
deactivateForEveryone(UnitEnum|string|array $feature)

Deactivate the feature for everyone.

void
setForAllScopes(string $feature, mixed $value)

Set a feature flag's value for all scopes.

void
delete(string $feature, mixed $scope)

Delete a feature flag's value.

void
purge(array|null $features = null)

Purge the given feature from storage.

string
name(UnitEnum|string $feature)

Retrieve the feature's name.

array
nameMap()

Retrieve the map of feature names to their implementations.

mixed
instance(UnitEnum|string $name)

Retrieve the feature's class.

definedFeaturesForScope(mixed $scope)

Retrieve the defined features for the given scope.

string
resolveFeature(UnitEnum|string $feature)

Resolve the feature name and ensure it is defined.

bool
shouldDynamicallyDefine(string $feature)

Determine if the feature should be dynamically defined.

string
ensureDynamicFeatureIsDefined(string $feature)

Dynamically define the feature.

bool
hasBeforeHook(string $feature)

Determine if the given feature has a before hook.

string|null
implementationClass(string $feature)

Retrieve the implementation feature class for the given feature name.

string
resolveFeatureName(string $class, object $instance)

Resolve the name for a feature class.

mixed
resolveScope(mixed $scope)

Resolve the scope.

bool
isCached(PennantState $state, string $connection, string $partition, string $feature, string $scope)

Determine if a feature's value is in the cache for the given scope.

void
putInCache(PennantState $state, string $connection, string $partition, string $feature, string $scope, mixed $value)

Put the given feature's value into the cache.

void
removeFromCache(PennantState $state, string $connection, string $partition, string $feature, string $scope)

Remove the given feature's value from the cache.

void
trackTransaction(PennantState $state, string $connection)

Forget the values memoized for the connection if its current transaction rolls back.

string
connectionName()

Get the name of the connection the driver currently uses, or an empty string for drivers without one.

string
partitionKey()

Get the key of the store's current partition.

mixed
defaultScope()

Retrieve the default scope.

void
flushCache()

Flush the in-memory cache of feature values.

void
flushSharedCache()

Invalidate shared cached database values for the current partition.

getDriver()

Get the underlying feature driver.

setContainer(Container $container)

Set the container instance used by the decorator.

Details

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

Register a custom macro.

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 44
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

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 61
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 72
static void flushMacros()

Flush the existing macros.

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

Return Value

void

in Macroable at line 83
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 1076
mixed __call(string $name, array $parameters)

Dynamically create a pending feature interaction.

Parameters

string $name
array $parameters

Return Value

mixed

at line 142
__construct(string $name, Driver $driver, Closure $defaultScopeResolver, Closure|null $partitionResolver, Container $container)

Create a new driver decorator instance.

Feature values are memoized in the current coroutine's PennantState instead of on the decorator, which every request in the worker shares.

Parameters

string $name
Driver $driver
Closure $defaultScopeResolver
Closure|null $partitionResolver
Container $container

at line 157
void discover(string $namespace = 'App\Features', string|null $path = null)

Discover and register the application's feature classes.

On a shared store, the definitions persist for the worker lifetime and add every discovered class to all() for subsequent requests.

Parameters

string $namespace
string|null $path

Return Value

void

at line 179
void define(string $feature, callable $resolver = null)

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 231
protected mixed resolve(string $feature, callable $resolver, mixed $scope)

Resolve the feature value.

Parameters

string $feature
callable $resolver
mixed $scope

Return Value

mixed

at line 249
bool isResolverValidForScope(callable|string $resolver, mixed $scope)

Determine if the resolver can handle the scope.

Parameters

callable|string $resolver
mixed $scope

Return Value

bool

at line 259
protected ReflectionFunctionAbstract resolverFunction(callable|string $resolver)

Reflect the function that resolves a feature.

Parameters

callable|string $resolver

Return Value

ReflectionFunctionAbstract

at line 275
protected bool functionAcceptsScope(ReflectionFunctionAbstract $function, mixed $scope)

Determine if the function's scope parameter accepts the scope.

Parameters

ReflectionFunctionAbstract $function
mixed $scope

Return Value

bool

at line 295
protected bool typeAllowsScope(ReflectionType $type, mixed $scope, ReflectionFunctionAbstract $function)

Determine if the type can handle the scope.

Parameters

ReflectionType $type
mixed $scope
ReflectionFunctionAbstract $function

Return Value

bool

Exceptions

RuntimeException

at line 361
protected bool canHandleNullScope(callable|ReflectionFunctionAbstract $resolver)

Determine if the resolver accepts null scope.

Parameters

callable|ReflectionFunctionAbstract $resolver

Return Value

bool

at line 377
array defined()

Retrieve the names of all defined features.

Return Value

array

at line 387
array stored()

Retrieve the names of all stored features.

Return Value

array

at line 404
array getAll(array $features)

internal  
 

Get multiple feature flag values.

Parameters

array $features

Return Value

array

at line 423
array getAllMissing(array|string $features)

internal  
 

Get multiple feature flag values that are missing.

Parameters

array|string $features

Return Value

array

at line 455
protected array resolveFeatures(array $features, PennantState $state, string $connection, string $partition)

Resolve the values of the given features through their before hooks or the driver, and memoize them.

Parameters

array $features
PennantState $state
string $connection
string $partition

Return Value

array

at line 507
protected array normalizeFeaturesToLoad(array|string $features)

Normalize the features to load.

Parameters

array|string $features

Return Value

array

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

internal  
 

Retrieve a feature flag's value.

Parameters

string $feature
mixed $scope

Return Value

mixed

at line 569
protected mixed resolveBeforeHook(string $feature, mixed $scope, callable $hook)

Resolve the before hook value.

Parameters

string $feature
mixed $scope
callable $hook

Return Value

mixed

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

internal  
 

Set a feature flag's value.

Parameters

string $feature
mixed $scope
mixed $value

Return Value

void

at line 618
void setAll(array $features)

internal  
 

Set multiple feature flag values.

Parameters

array $features

Return Value

void

at line 669
PendingScopedFeatureInteraction globally()

Create a pending feature interaction for the global scope, ignoring the default scope resolver.

at line 679
void activateForEveryone(UnitEnum|string|array $feature, mixed $value = true)

Activate the feature for everyone.

Parameters

UnitEnum|string|array $feature
mixed $value

Return Value

void

at line 690
void deactivateForEveryone(UnitEnum|string|array $feature)

Deactivate the feature for everyone.

Parameters

UnitEnum|string|array $feature

Return Value

void

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

internal  
 

Set a feature flag's value for all scopes.

Parameters

string $feature
mixed $value

Return Value

void

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

internal  
 

Delete a feature flag's value.

Parameters

string $feature
mixed $scope

Return Value

void

at line 739
void purge(array|null $features = null)

Purge the given feature from storage.

Parameters

array|null $features

Return Value

void

at line 771
string name(UnitEnum|string $feature)

Retrieve the feature's name.

Parameters

UnitEnum|string $feature

Return Value

string

at line 781
array nameMap()

Retrieve the map of feature names to their implementations.

Return Value

array

at line 789
mixed instance(UnitEnum|string $name)

Retrieve the feature's class.

Parameters

UnitEnum|string $name

Return Value

mixed

at line 817
Collection definedFeaturesForScope(mixed $scope)

internal  
 

Retrieve the defined features for the given scope.

Parameters

mixed $scope

Return Value

Collection

at line 850
protected string resolveFeature(UnitEnum|string $feature)

Resolve the feature name and ensure it is defined.

Parameters

UnitEnum|string $feature

Return Value

string

at line 870
protected bool shouldDynamicallyDefine(string $feature)

Determine if the feature should be dynamically defined.

Parameters

string $feature

Return Value

bool

at line 882
protected string ensureDynamicFeatureIsDefined(string $feature)

Dynamically define the feature.

Parameters

string $feature

Return Value

string

at line 898
protected bool hasBeforeHook(string $feature)

Determine if the given feature has a before hook.

Parameters

string $feature

Return Value

bool

at line 910
protected string|null implementationClass(string $feature)

Retrieve the implementation feature class for the given feature name.

Parameters

string $feature

Return Value

string|null

at line 918
protected string resolveFeatureName(string $class, object $instance)

Resolve the name for a feature class.

Parameters

string $class
object $instance

Return Value

string

at line 932
protected mixed resolveScope(mixed $scope)

Resolve the scope.

Parameters

mixed $scope

Return Value

mixed

at line 942
protected bool isCached(PennantState $state, string $connection, string $partition, string $feature, string $scope)

Determine if a feature's value is in the cache for the given scope.

Parameters

PennantState $state
string $connection
string $partition
string $feature
string $scope

Return Value

bool

at line 950
protected void putInCache(PennantState $state, string $connection, string $partition, string $feature, string $scope, mixed $value)

Put the given feature's value into the cache.

Parameters

PennantState $state
string $connection
string $partition
string $feature
string $scope
mixed $value

Return Value

void

at line 958
protected void removeFromCache(PennantState $state, string $connection, string $partition, string $feature, string $scope)

Remove the given feature's value from the cache.

Parameters

PennantState $state
string $connection
string $partition
string $feature
string $scope

Return Value

void

at line 968
protected void trackTransaction(PennantState $state, string $connection)

Forget the values memoized for the connection if its current transaction rolls back.

Values read or written inside a transaction may never be committed.

Parameters

PennantState $state
string $connection

Return Value

void

at line 995
protected string connectionName()

Get the name of the connection the driver currently uses, or an empty string for drivers without one.

Return Value

string

at line 1003
protected string partitionKey()

Get the key of the store's current partition.

Return Value

string

at line 1011
protected mixed defaultScope()

Retrieve the default scope.

Return Value

mixed

at line 1021
void flushCache()

Flush the in-memory cache of feature values.

Only the current coroutine's values are flushed.

Return Value

void

at line 1037
void flushSharedCache()

Invalidate shared cached database values for the current partition.

Return Value

void

at line 1047
Driver getDriver()

Get the underlying feature driver.

Return Value

Driver

at line 1059
Decorator setContainer(Container $container)

Set the container instance used by the decorator.

On a shared store, the container persists for the worker lifetime.

Parameters

Container $container

Return Value

Decorator