From 7569c4c7bf4a41c77017bda0806734f13d42e3be Mon Sep 17 00:00:00 2001 From: jon Date: Fri, 12 Jul 2024 18:39:59 +0100 Subject: [PATCH] feat: locker allow custom ids and prefixes --- src/Data/OptionData.php | 3 +- src/Helpers/Locker/Locker.php | 44 +++++++++++++++++++++++++-- src/Helpers/RateLimiterHelper.php | 3 +- src/LaravelHelpersServiceProvider.php | 4 ++- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/Data/OptionData.php b/src/Data/OptionData.php index 164ca17..601b9f8 100644 --- a/src/Data/OptionData.php +++ b/src/Data/OptionData.php @@ -18,5 +18,6 @@ public function __construct( public Optional|null|string $groupIcon = null, /** @var Collection|null */ public Optional|Collection|null $items = null, - ) {} + ) { + } } diff --git a/src/Helpers/Locker/Locker.php b/src/Helpers/Locker/Locker.php index ad3b86c..b2a2cd4 100644 --- a/src/Helpers/Locker/Locker.php +++ b/src/Helpers/Locker/Locker.php @@ -23,17 +23,33 @@ public function __construct( protected bool $executedIfAlreadyLocked = false, protected int $lockFor = 10, protected int $waitForLock = 3, - protected ?string $owner = null + protected ?string $owner = null, + protected ?string $id = null, + protected string $idPrefix = '', ) { if (config('cache.default') !== 'redis') { throw new Exception('Cache driver must be redis'); } + + if ($id == null) { + $this->id = $this->getIdFromModel(); + } } /** - * Get the ID of the lock. + * Get the ID of the lock */ public function getId(): string + { + $id = $this->id ?? $this->getIdFromModel(); + + return $this->idPrefix ? $this->idPrefix.'_'.$id : ''; + } + + /** + * Get the ID of the lock. + */ + public function getIdFromModel(): string { // locks_App\Models\User_1 $key = $this->model->getKeyName(); @@ -46,6 +62,30 @@ public function getId(): string )); } + /** + * Set the ID of the lock to the given value + * + * @return $this + */ + public function id(string $id): Locker + { + $this->id = $id; + + return $this; + } + + /** + * Set a prefix to the id, useful for multi tenancy or testing + * + * @return $this + */ + public function prefix(string $prefix): Locker + { + $this->idPrefix = $prefix; + + return $this; + } + /** * If the resource is already locked skip and execute the callback */ diff --git a/src/Helpers/RateLimiterHelper.php b/src/Helpers/RateLimiterHelper.php index 4ad06c1..e79129a 100644 --- a/src/Helpers/RateLimiterHelper.php +++ b/src/Helpers/RateLimiterHelper.php @@ -29,7 +29,8 @@ public function __construct( protected string $key, protected string $by, protected bool $hashed = true - ) {} + ) { + } /** * Forwards the call to the RateLimiter instance. diff --git a/src/LaravelHelpersServiceProvider.php b/src/LaravelHelpersServiceProvider.php index 023f0b5..eef3c12 100644 --- a/src/LaravelHelpersServiceProvider.php +++ b/src/LaravelHelpersServiceProvider.php @@ -19,5 +19,7 @@ public function bootingPackage(): void // Booting the package } - public function registeringPackage(): void {} + public function registeringPackage(): void + { + } }