13
13
14
14
use Symfony \Component \Console \Exception \LogicException ;
15
15
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 ;
17
20
18
21
/**
19
22
* Basic lock feature for commands.
22
25
*/
23
26
trait LockableTrait
24
27
{
25
- private $ lockHandler ;
28
+ /** @var Lock */
29
+ private $ lock ;
26
30
27
31
/**
28
32
* Locks a command.
@@ -31,18 +35,23 @@ trait LockableTrait
31
35
*/
32
36
private function lock ($ name = null , $ blocking = false )
33
37
{
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. ' );
36
40
}
37
41
38
- if (null !== $ this ->lockHandler ) {
42
+ if (null !== $ this ->lock ) {
39
43
throw new LogicException ('A lock is already in place. ' );
40
44
}
41
45
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
+ }
43
51
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 ;
46
55
47
56
return false ;
48
57
}
@@ -55,9 +64,9 @@ private function lock($name = null, $blocking = false)
55
64
*/
56
65
private function release ()
57
66
{
58
- if ($ this ->lockHandler ) {
59
- $ this ->lockHandler ->release ();
60
- $ this ->lockHandler = null ;
67
+ if ($ this ->lock ) {
68
+ $ this ->lock ->release ();
69
+ $ this ->lock = null ;
61
70
}
62
71
}
63
72
}
0 commit comments