comparison vendor/symfony/console/Command/LockableTrait.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
11 11
12 namespace Symfony\Component\Console\Command; 12 namespace Symfony\Component\Console\Command;
13 13
14 use Symfony\Component\Console\Exception\LogicException; 14 use Symfony\Component\Console\Exception\LogicException;
15 use Symfony\Component\Console\Exception\RuntimeException; 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 * Basic lock feature for commands. 22 * Basic lock feature for commands.
20 * 23 *
21 * @author Geoffrey Brier <geoffrey.brier@gmail.com> 24 * @author Geoffrey Brier <geoffrey.brier@gmail.com>
22 */ 25 */
23 trait LockableTrait 26 trait LockableTrait
24 { 27 {
25 private $lockHandler; 28 /** @var Lock */
29 private $lock;
26 30
27 /** 31 /**
28 * Locks a command. 32 * Locks a command.
29 * 33 *
30 * @return bool 34 * @return bool
31 */ 35 */
32 private function lock($name = null, $blocking = false) 36 private function lock($name = null, $blocking = false)
33 { 37 {
34 if (!class_exists(LockHandler::class)) { 38 if (!class_exists(SemaphoreStore::class)) {
35 throw new RuntimeException('To enable the locking feature you must install the symfony/filesystem component.'); 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 throw new LogicException('A lock is already in place.'); 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();
50 }
43 51
44 if (!$this->lockHandler->lock($blocking)) { 52 $this->lock = (new Factory($store))->createLock($name ?: $this->getName());
45 $this->lockHandler = null; 53 if (!$this->lock->acquire($blocking)) {
54 $this->lock = null;
46 55
47 return false; 56 return false;
48 } 57 }
49 58
50 return true; 59 return true;
53 /** 62 /**
54 * Releases the command lock if there is one. 63 * Releases the command lock if there is one.
55 */ 64 */
56 private function release() 65 private function release()
57 { 66 {
58 if ($this->lockHandler) { 67 if ($this->lock) {
59 $this->lockHandler->release(); 68 $this->lock->release();
60 $this->lockHandler = null; 69 $this->lock = null;
61 } 70 }
62 } 71 }
63 } 72 }