class RateLimiter extends MultipleInstanceManager mixin Limiter

Traits

Properties

protected Repository $config

The configuration repository instance.

from  MultipleInstanceManager
protected array $instances

The array of resolved instances.

from  MultipleInstanceManager
protected array $customCreators

The registered custom instance creators.

from  MultipleInstanceManager
protected string $driverKey

The key name of the "driver" equivalent configuration option.

from  MultipleInstanceManager
static protected array $macros

The registered string macros.

from  Macroable
protected Closure> $limiters

The configured named limiter callbacks.

protected array<string, string> $limiterStores

The optional store selected for each named limiter.

protected list<Closure(string): ?string> $keyScopeResolvers

The callbacks used to resolve scopes for named limiter keys.

Methods

Closure|null
bindCallbackToSelf(Closure $callback)

Bind the provided callback to the class instance.

__construct(Application $app)

Create a new manager instance.

string
getDefaultInstance()

Get the default rate limiter store name.

void
setDefaultInstance(string $name)

Set the default rate limiter store name.

array
getInstanceConfig(string $name)

Get the store-specific configuration.

mixed
instance(string|null $name = null)

Get an instance by name.

mixed
get(string $name)

Attempt to get an instance from the local cache.

mixed
resolve(string $name)

Resolve a store and wrap it in the public limiter API.

mixed
callCustomCreator(array $config)

Call a custom instance creator.

forgetInstance(array|string|null $name = null)

Unset the given instances.

void
purge(string|null $name = null)

Disconnect the given instance and remove from local cache.

extend(string $name, Closure $callback)

Register a custom instance creator Closure.

setApplication(Application $app)

Set the application instance used by the manager.

mixed
__call(string $method, array $parameters)

Dynamically call a macro or the default limiter.

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.

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

Get a limiter store by name.

for(UnitEnum|string $name, Closure $callback, UnitEnum|string|null $store = null)

Register a named rate limiter configuration.

Closure|null
limiter(UnitEnum|string $name)

Get the given named rate limiter.

string|null
limiterStore(UnitEnum|string $name)

Get the store registered for a named rate limiter.

void
resolveKeyScopeUsing(Closure|null $resolver)

Register a named limiter key scope resolver.

array
resolveKeyScopes(string $limiterName)

Resolve the contributed scopes for a named limiter.

createWorkerArrayDriver()

Create a worker-lifetime array store.

createDatabaseDriver(array $config)

Create a database store.

createRedisDriver(array $config)

Create a Redis store.

createSwooleDriver(array $config)

Create a Swoole store.

string
normalizeName(UnitEnum|string $name)

Normalize an enum or string manager name.

static void
flushState()

Flush all static state.

Details

protected Closure|null bindCallbackToSelf(Closure $callback)

Bind the provided callback to the class instance.

Parameters

Closure $callback

Return Value

Closure|null

Exceptions

ReflectionException

__construct(Application $app)

Create a new manager instance.

Parameters

Application $app

at line 127
string getDefaultInstance()

Get the default rate limiter store name.

Return Value

string

at line 137
void setDefaultInstance(string $name)

Set the default rate limiter store name.

Boot-only. Mutates process-global config; per-request use races across coroutines.

Parameters

string $name

Return Value

void

at line 145
array getInstanceConfig(string $name)

Get the store-specific configuration.

Parameters

string $name

Return Value

array

mixed instance(string|null $name = null)

Get an instance by name.

Parameters

string|null $name

Return Value

mixed

protected mixed get(string $name)

Attempt to get an instance from the local cache.

Parameters

string $name

Return Value

mixed

at line 159
protected mixed resolve(string $name)

Resolve a store and wrap it in the public limiter API.

Parameters

string $name

Return Value

mixed

protected mixed callCustomCreator(array $config)

Call a custom instance creator.

Parameters

array $config

Return Value

mixed

MultipleInstanceManager forgetInstance(array|string|null $name = null)

Unset the given instances.

Boot or tests only. Mutates the singleton's instance cache; concurrent coroutines may already hold a reference to the forgotten instance and next resolution will rebuild.

Parameters

array|string|null $name

Return Value

MultipleInstanceManager

void purge(string|null $name = null)

Disconnect the given instance and remove from local cache.

Boot or tests only. Mutates the singleton's instance cache; concurrent coroutines may already hold a reference to the instance and next resolution will rebuild.

Parameters

string|null $name

Return Value

void

MultipleInstanceManager extend(string $name, Closure $callback)

Register a custom instance creator Closure.

Anonymous closures run in this manager's class scope; non-static closures also receive the manager as $this.

Boot-only. The callback persists in the singleton's customCreators array for the worker lifetime and applies to every subsequent instance resolution.

Parameters

string $name
Closure $callback

Return Value

MultipleInstanceManager

MultipleInstanceManager setApplication(Application $app)

Set the application instance used by the manager.

Tests only. Swaps the singleton's application and configuration references without rebuilding resolved instances; per-request use races across coroutines and breaks every concurrent resolution through this manager.

Parameters

Application $app

Return Value

MultipleInstanceManager

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

Dynamically call a macro or the default limiter.

Parameters

string $method
array $parameters

Return Value

mixed

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 53
Limiter store(UnitEnum|string|null $name = null)

Get a limiter store by name.

Parameters

UnitEnum|string|null $name

Return Value

Limiter

at line 69
RateLimiter for(UnitEnum|string $name, Closure $callback, UnitEnum|string|null $store = null)

Register a named rate limiter configuration.

Boot-only. The callback and store selection persist on the singleton manager for the worker lifetime and affect every subsequent request.

Parameters

UnitEnum|string $name
Closure $callback
UnitEnum|string|null $store

Return Value

RateLimiter

at line 90
Closure|null limiter(UnitEnum|string $name)

Get the given named rate limiter.

Parameters

UnitEnum|string $name

Return Value

Closure|null

at line 98
string|null limiterStore(UnitEnum|string $name)

Get the store registered for a named rate limiter.

Parameters

UnitEnum|string $name

Return Value

string|null

at line 113
void resolveKeyScopeUsing(Closure|null $resolver)

Register a named limiter key scope resolver.

Boot-only. Each non-null callback is appended in registration order, while passing null clears every registered callback. The callbacks persist on the singleton manager for the worker lifetime and affect every subsequent named limiter operation.

Parameters

Closure|null $resolver

Return Value

void

at line 185
protected array resolveKeyScopes(string $limiterName)

Resolve the contributed scopes for a named limiter.

Parameters

string $limiterName

Return Value

array

at line 203
protected Store createWorkerArrayDriver()

Create a worker-lifetime array store.

Return Value

Store

at line 211
protected Store createDatabaseDriver(array $config)

Create a database store.

Parameters

array $config

Return Value

Store

at line 234
protected Store createRedisDriver(array $config)

Create a Redis store.

Parameters

array $config

Return Value

Store

at line 251
protected Store createSwooleDriver(array $config)

Create a Swoole store.

Parameters

array $config

Return Value

Store

at line 276
protected string normalizeName(UnitEnum|string $name)

Normalize an enum or string manager name.

Parameters

UnitEnum|string $name

Return Value

string

at line 286
static void flushState()

Flush all static state.

Return Value

void