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 diff --git a/src/Lock.php b/src/Lock.php index 984dfa6..4c2b6f2 100644 --- a/src/Lock.php +++ b/src/Lock.php @@ -244,11 +244,22 @@ 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) + 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 +278,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 +332,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..2dd2eab 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 @@ -327,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'); @@ -336,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);