🆙 Add cms i using 🆙

This commit is contained in:
2025-11-25 22:42:56 +01:00
parent 94704e0925
commit d44196149e
35591 changed files with 3601123 additions and 0 deletions
@@ -0,0 +1,27 @@
<?php
namespace DanHarrin\LivewireRateLimiting\Exceptions;
use Exception;
class TooManyRequestsException extends Exception
{
public $minutesUntilAvailable;
public function __construct(
public $component,
public $method,
public $ip,
public $secondsUntilAvailable,
) {
$this->minutesUntilAvailable = ceil($this->secondsUntilAvailable / 60);
parent::__construct(sprintf(
'Too many requests from [%s] to method [%s] on component: [%s]. Retry in %d seconds.',
$this->ip,
$this->method,
$this->component,
$this->secondsUntilAvailable,
));
}
}
@@ -0,0 +1,58 @@
<?php
namespace DanHarrin\LivewireRateLimiting;
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
use Illuminate\Support\Facades\RateLimiter;
trait WithRateLimiting
{
protected function clearRateLimiter($method = null, $component = null)
{
$method ??= debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, limit: 2)[1]['function'];
$component ??= static::class;
$key = $this->getRateLimitKey($method, $component);
RateLimiter::clear($key);
}
protected function getRateLimitKey($method, $component = null)
{
$method ??= debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, limit: 2)[1]['function'];
$component ??= static::class;
return 'livewire-rate-limiter:'.sha1($component.'|'.$method.'|'.request()->ip());
}
protected function hitRateLimiter($method = null, $decaySeconds = 60, $component = null)
{
$method ??= debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, limit: 2)[1]['function'];
$component ??= static::class;
$key = $this->getRateLimitKey($method, $component);
RateLimiter::hit($key, $decaySeconds);
}
protected function rateLimit($maxAttempts, $decaySeconds = 60, $method = null, $component = null)
{
$method ??= debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, limit: 2)[1]['function'];
$component ??= static::class;
$key = $this->getRateLimitKey($method, $component);
if (RateLimiter::tooManyAttempts($key, $maxAttempts)) {
$ip = request()->ip();
$secondsUntilAvailable = RateLimiter::availableIn($key);
throw new TooManyRequestsException($component, $method, $ip, $secondsUntilAvailable);
}
$this->hitRateLimiter($method, $decaySeconds, $component);
}
}