From c06d290174661e1e848563143be0088d1a42c1b6 Mon Sep 17 00:00:00 2001 From: Matthias Neid Date: Mon, 13 Jan 2025 14:46:44 +0100 Subject: [PATCH 1/4] add option to set identifier in construct and with function --- src/Lock.php | 34 +++++++++++++++++++++++++++------- test/LockTest.php | 19 +++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/Lock.php b/src/Lock.php index 984dfa6..28b74e7 100644 --- a/src/Lock.php +++ b/src/Lock.php @@ -245,10 +245,20 @@ public static function setDelayPerUnavailableRetry(int $delayPerRetry): void * * @param string $key Can be anything, should describe the resource in a unique way */ - public function __construct(string $key) + public function __construct(string $key, ?string $identifier = null) { $this->key = $key; $this->etcdKey = static::$prefix . $this->key; + + if (static::$defaultIdentifier === null) { + static::setDefaultIdentifier(); + } + + if ($identifier === null) { + $this->identifier = static::$defaultIdentifier; + } else { + $this->identifier = $identifier; + } } /** @@ -267,12 +277,7 @@ public function lock(bool $exclusive = false, int $time = 120, int $wait = 300, $this->exclusive = $exclusive; $this->time = $time; - if (static::$defaultIdentifier === null) { - static::setDefaultIdentifier(); - } - if ($identifier === null) { - $this->identifier = static::$defaultIdentifier; - } else { + if ($identifier !== null) { $this->identifier = $identifier; } @@ -326,6 +331,21 @@ public function isLocked(): bool|int return false; } + /** + * Set the identifier for this lock, falls back to the default identifier if null + * + * @param string|null $identifier + * @return void + */ + public function setIdentifier(?string $identifier): void + { + if ($identifier === null) { + $this->identifier = static::$defaultIdentifier; + } else { + $this->identifier = $identifier; + } + } + /** * Get the used identifier for this lock * diff --git a/test/LockTest.php b/test/LockTest.php index 0990b7c..dca20b8 100644 --- a/test/LockTest.php +++ b/test/LockTest.php @@ -30,6 +30,25 @@ public function testCreateLock(): void $lock->break(); } + public function testConstructLockWithDefaultIdentifier(): void + { + $lock = new Lock("key"); + $this->assertIsString($lock->getIdentifier()); + } + + public function testConstructLockWithIdentifier(): void + { + $lock = new Lock("key", "identifier"); + $this->assertEquals("identifier", $lock->getIdentifier()); + } + + public function testSetIdentifier(): void + { + $lock = new Lock("key"); + $lock->setIdentifier("identifier"); + $this->assertEquals("identifier", $lock->getIdentifier()); + } + /** * @throws InvalidResponseStatusCodeException * @throws TooManySaveRetriesException From d2ae481042cbf2c68533f2b14a693aa53369d6ef Mon Sep 17 00:00:00 2001 From: Matthias Neid Date: Mon, 13 Jan 2025 14:50:11 +0100 Subject: [PATCH 2/4] fix test --- test/LockTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/LockTest.php b/test/LockTest.php index dca20b8..2dd2eab 100644 --- a/test/LockTest.php +++ b/test/LockTest.php @@ -346,8 +346,6 @@ public function testLockDeleteConflict(): void */ public function testLockFunctionUsesUniqueDefaultIdentifierIfNoIdentifierParameterIsProvided(): void { - $lock = new Lock($this->getRandomString()); - # Default identifier is not set explicitly and its default value is null. # (We have to set it to null manually because other tests might have set the default identifier before.) $reflection = new \ReflectionProperty(Lock::class, 'defaultIdentifier'); @@ -355,7 +353,7 @@ public function testLockFunctionUsesUniqueDefaultIdentifierIfNoIdentifierParamet $reflection->setValue(null); $this->assertNull($reflection->getValue()); - # lock() sets the default identifier to an uniqid() + $lock = new Lock($this->getRandomString()); $lock->lock(); $defaultIdentifier = $reflection->getValue(); $this->assertIsString($defaultIdentifier); From 3260fb2a08c5fbcc0052a1588007239694d1cfa0 Mon Sep 17 00:00:00 2001 From: Matthias Neid Date: Mon, 13 Jan 2025 14:57:21 +0100 Subject: [PATCH 3/4] update year --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 6dc2021..e4e7c3f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2019-2024 Aternos GmbH +Copyright (c) 2019-2025 Aternos GmbH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From e77a2b968bfcb76805185025ad4f6a6a97caf396 Mon Sep 17 00:00:00 2001 From: Matthias Neid Date: Tue, 14 Jan 2025 13:34:47 +0100 Subject: [PATCH 4/4] phpdoc --- src/Lock.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Lock.php b/src/Lock.php index 28b74e7..4c2b6f2 100644 --- a/src/Lock.php +++ b/src/Lock.php @@ -244,6 +244,7 @@ public static function setDelayPerUnavailableRetry(int $delayPerRetry): void * Create a lock * * @param string $key Can be anything, should describe the resource in a unique way + * @param string|null $identifier An identifier for this lock, falls back to the default identifier if null */ public function __construct(string $key, ?string $identifier = null) {