Skip to content

Commit e7f890d

Browse files
committed
Deprecate Filesystem/LockHandler
1 parent 4e1a385 commit e7f890d

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

Command/LockableTrait.php

+20-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
use Symfony\Component\Console\Exception\LogicException;
1515
use Symfony\Component\Console\Exception\RuntimeException;
16-
use Symfony\Component\Filesystem\LockHandler;
16+
use Symfony\Component\Lock\Factory;
17+
use Symfony\Component\Lock\Lock;
18+
use Symfony\Component\Lock\Store\FlockStore;
19+
use Symfony\Component\Lock\Store\SemaphoreStore;
1720

1821
/**
1922
* Basic lock feature for commands.
@@ -22,7 +25,8 @@
2225
*/
2326
trait LockableTrait
2427
{
25-
private $lockHandler;
28+
/** @var Lock */
29+
private $lock;
2630

2731
/**
2832
* Locks a command.
@@ -31,18 +35,23 @@ trait LockableTrait
3135
*/
3236
private function lock($name = null, $blocking = false)
3337
{
34-
if (!class_exists(LockHandler::class)) {
35-
throw new RuntimeException('To enable the locking feature you must install the symfony/filesystem component.');
38+
if (!class_exists(SemaphoreStore::class)) {
39+
throw new RuntimeException('To enable the locking feature you must install the symfony/lock component.');
3640
}
3741

38-
if (null !== $this->lockHandler) {
42+
if (null !== $this->lock) {
3943
throw new LogicException('A lock is already in place.');
4044
}
4145

42-
$this->lockHandler = new LockHandler($name ?: $this->getName());
46+
if (SemaphoreStore::isSupported($blocking)) {
47+
$store = new SemaphoreStore();
48+
} else {
49+
$store = new FlockStore(sys_get_temp_dir());
50+
}
4351

44-
if (!$this->lockHandler->lock($blocking)) {
45-
$this->lockHandler = null;
52+
$this->lock = (new Factory($store))->createLock($name ?: $this->getName());
53+
if (!$this->lock->acquire($blocking)) {
54+
$this->lock = null;
4655

4756
return false;
4857
}
@@ -55,9 +64,9 @@ private function lock($name = null, $blocking = false)
5564
*/
5665
private function release()
5766
{
58-
if ($this->lockHandler) {
59-
$this->lockHandler->release();
60-
$this->lockHandler = null;
67+
if ($this->lock) {
68+
$this->lock->release();
69+
$this->lock = null;
6170
}
6271
}
6372
}

Tests/Command/LockableTraitTest.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Tester\CommandTester;
16-
use Symfony\Component\Filesystem\LockHandler;
16+
use Symfony\Component\Lock\Factory;
17+
use Symfony\Component\Lock\Store\FlockStore;
18+
use Symfony\Component\Lock\Store\SemaphoreStore;
1719

1820
class LockableTraitTest extends TestCase
1921
{
@@ -39,8 +41,14 @@ public function testLockReturnsFalseIfAlreadyLockedByAnotherCommand()
3941
{
4042
$command = new \FooLockCommand();
4143

42-
$lock = new LockHandler($command->getName());
43-
$lock->lock();
44+
if (SemaphoreStore::isSupported(false)) {
45+
$store = new SemaphoreStore();
46+
} else {
47+
$store = new FlockStore(sys_get_temp_dir());
48+
}
49+
50+
$lock = (new Factory($store))->createLock($command->getName());
51+
$lock->acquire();
4452

4553
$tester = new CommandTester($command);
4654
$this->assertSame(1, $tester->execute(array()));

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
"symfony/http-kernel": "~2.8|~3.0|~4.0",
2626
"symfony/event-dispatcher": "~2.8|~3.0|~4.0",
2727
"symfony/dependency-injection": "~3.3|~4.0",
28-
"symfony/filesystem": "~2.8|~3.0|~4.0",
28+
"symfony/lock": "~3.4|~4.0",
2929
"symfony/process": "~3.3|~4.0",
3030
"psr/log": "~1.0"
3131
},
3232
"suggest": {
3333
"symfony/event-dispatcher": "",
34-
"symfony/filesystem": "",
34+
"symfony/lock": "",
3535
"symfony/process": "",
3636
"psr/log": "For using the console logger"
3737
},

0 commit comments

Comments
 (0)