Skip to content

Commit d24229d

Browse files
committed
Deprecate lock.store aliases
1 parent 81730d4 commit d24229d

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* deprecated `RetryTillSaveStore`, logic has been moved in `Lock` and is not needed anymore.
1212
* added `InMemoryStore`
1313
* added `PostgreSqlStore`
14+
* added the `LockFactory::CreateLockFromKey()` method.
1415

1516
5.1.0
1617
-----

LockFactory.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@ public function __construct(PersistingStoreInterface $store)
4343
*/
4444
public function createLock(string $resource, ?float $ttl = 300.0, bool $autoRelease = true): LockInterface
4545
{
46-
$lock = new Lock(new Key($resource), $this->store, $ttl, $autoRelease);
46+
return $this->createLockFromKey(new Key($resource), $ttl, $autoRelease);
47+
}
48+
49+
/**
50+
* Creates a lock from the given key.
51+
*
52+
* @param Key $key The key containing the lock's state
53+
* @param float|null $ttl Maximum expected lock duration in seconds
54+
* @param bool $autoRelease Whether to automatically release the lock or not when the lock instance is destroyed
55+
*/
56+
public function createLockFromKey(Key $key, ?float $ttl = 300.0, bool $autoRelease = true): LockInterface
57+
{
58+
$lock = new Lock($key, $this->store, $ttl, $autoRelease);
4759
$lock->setLogger($this->logger);
4860

4961
return $lock;

Tests/LockFactoryTest.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Log\LoggerInterface;
16+
use Symfony\Component\Lock\Key;
1617
use Symfony\Component\Lock\LockFactory;
17-
use Symfony\Component\Lock\LockInterface;
1818
use Symfony\Component\Lock\PersistingStoreInterface;
1919

2020
/**
@@ -25,12 +25,61 @@ class LockFactoryTest extends TestCase
2525
public function testCreateLock()
2626
{
2727
$store = $this->getMockBuilder(PersistingStoreInterface::class)->getMock();
28+
$store->expects($this->any())->method('exists')->willReturn(false);
29+
30+
$keys = [];
31+
$store
32+
->expects($this->exactly(2))
33+
->method('save')
34+
->with($this->callback(function ($key) use (&$keys) {
35+
$keys[] = $key;
36+
37+
return true;
38+
}))
39+
->willReturn(true);
40+
2841
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
2942
$factory = new LockFactory($store);
3043
$factory->setLogger($logger);
3144

32-
$lock = $factory->createLock('foo');
45+
$lock1 = $factory->createLock('foo');
46+
$lock2 = $factory->createLock('foo');
47+
48+
// assert lock1 and lock2 don't share the same state
49+
$lock1->acquire();
50+
$lock2->acquire();
51+
52+
$this->assertNotSame($keys[0], $keys[1]);
53+
}
54+
55+
public function testCreateLockFromKey()
56+
{
57+
$store = $this->getMockBuilder(PersistingStoreInterface::class)->getMock();
58+
$store->expects($this->any())->method('exists')->willReturn(false);
59+
60+
$keys = [];
61+
$store
62+
->expects($this->exactly(2))
63+
->method('save')
64+
->with($this->callback(function ($key) use (&$keys) {
65+
$keys[] = $key;
66+
67+
return true;
68+
}))
69+
->willReturn(true);
70+
71+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
72+
$factory = new LockFactory($store);
73+
$factory->setLogger($logger);
74+
75+
$key = new Key('foo');
76+
$lock1 = $factory->createLockFromKey($key);
77+
$lock2 = $factory->createLockFromKey($key);
78+
79+
// assert lock1 and lock2 share the same state
80+
$lock1->acquire();
81+
$lock2->acquire();
3382

34-
$this->assertInstanceOf(LockInterface::class, $lock);
83+
$this->assertSame($keys[0], $keys[1]);
3584
}
3685
}

0 commit comments

Comments
 (0)