RateLimitFilter
extends Filter
in package
Rate limiting filter for HTTP requests.
Implements configurable rate limiting with support for multiple storage backends and flexible key generation strategies.
Table of Contents
Properties
- $_blacklist : array<string|int, mixed>
- $_config : RateLimitConfig
- $_ipResolver : IIpResolver
- $_keyStrategy : string
- $_postFn : Closure|null
- $_preFn : Closure|null
- $_storage : IRateLimitStorage
- $_whitelist : array<string|int, mixed>
Methods
- __construct() : mixed
- clear() : void
- Clear all rate limits.
- getStorage() : IRateLimitStorage
- Get the storage instance (for testing).
- post() : mixed|null
- pre() : mixed|null
- reset() : void
- Reset rate limit for a specific key.
- addRateLimitHeaders() : void
- Add the rate limit headers to response.
- checkRateLimit() : void
- Check the rate limit for the current request.
- generateKey() : string
- Generate a unique key for rate limiting.
- getClientIp() : string
- Get the client IP address using the configured IP resolver.
- getCustomKey() : string
- Get a custom key for rate limiting.
- getLimit() : int
- Get rate limit for a key.
- getUserId() : string
- Get user ID for authenticated requests.
- getWindow() : int
- Get the time window for a key.
- isBlacklisted() : bool
- Check if a key is blacklisted.
- isWhitelisted() : bool
- Check if a key is whitelisted.
Properties
$_blacklist
private
array<string|int, mixed>
$_blacklist
$_config
private
RateLimitConfig
$_config
$_ipResolver
private
IIpResolver
$_ipResolver
$_keyStrategy
private
string
$_keyStrategy
$_postFn
private
Closure|null
$_postFn
$_preFn
private
Closure|null
$_preFn
$_storage
private
IRateLimitStorage
$_storage
$_whitelist
private
array<string|int, mixed>
$_whitelist
Methods
__construct()
public
__construct(RateLimitConfig $config[, string $keyStrategy = 'ip' ][, array<string|int, mixed> $whitelist = [] ][, array<string|int, mixed> $blacklist = [] ][, IIpResolver|null $ipResolver = null ]) : mixed
Parameters
- $config : RateLimitConfig
-
Rate limit configuration
- $keyStrategy : string = 'ip'
-
Key generation strategy ('ip', 'user', 'route', 'custom')
- $whitelist : array<string|int, mixed> = []
-
IP addresses or user IDs to exempt from rate limiting
- $blacklist : array<string|int, mixed> = []
-
IP addresses or user IDs to apply stricter limits
- $ipResolver : IIpResolver|null = null
-
Optional IP resolver (defaults to DefaultIpResolver)
Tags
clear()
Clear all rate limits.
public
clear() : void
getStorage()
Get the storage instance (for testing).
public
getStorage() : IRateLimitStorage
Return values
IRateLimitStoragepost()
public
post(RouteMap $route) : mixed|null
Parameters
- $route : RouteMap
Return values
mixed|nullpre()
public
pre(RouteMap $route) : mixed|null
Parameters
- $route : RouteMap
Return values
mixed|nullreset()
Reset rate limit for a specific key.
public
reset(string $key) : void
Parameters
- $key : string
addRateLimitHeaders()
Add the rate limit headers to response.
protected
addRateLimitHeaders(string $key, int $limit, int $window) : void
Parameters
- $key : string
- $limit : int
- $window : int
checkRateLimit()
Check the rate limit for the current request.
protected
checkRateLimit(RouteMap $route) : void
Parameters
- $route : RouteMap
generateKey()
Generate a unique key for rate limiting.
protected
generateKey(RouteMap $route) : string
Parameters
- $route : RouteMap
Return values
stringgetClientIp()
Get the client IP address using the configured IP resolver.
protected
getClientIp() : string
Return values
stringgetCustomKey()
Get a custom key for rate limiting.
protected
getCustomKey(RouteMap $route) : string
Parameters
- $route : RouteMap
Return values
stringgetLimit()
Get rate limit for a key.
protected
getLimit(string $key) : int
Parameters
- $key : string
Return values
intgetUserId()
Get user ID for authenticated requests.
protected
getUserId() : string
Return values
stringgetWindow()
Get the time window for a key.
protected
getWindow(string $key) : int
Parameters
- $key : string
Return values
intisBlacklisted()
Check if a key is blacklisted.
protected
isBlacklisted(string $key) : bool
Parameters
- $key : string
Return values
boolisWhitelisted()
Check if a key is whitelisted.
protected
isWhitelisted(string $key) : bool
Parameters
- $key : string