Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to set identifier in construct and with function #10

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
35 changes: 28 additions & 7 deletions src/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
matthi4s marked this conversation as resolved.
Show resolved Hide resolved
{
$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;
}
}

/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
*
Expand Down
23 changes: 20 additions & 3 deletions test/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -327,16 +346,14 @@ 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');
$reflection->setAccessible(true);
$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);
Expand Down
Loading