PluginProbe
UpStream: a Project Management Plugin for WordPress / trunk
UpStream: a Project Management Plugin for WordPress vtrunk
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / vendor / symfony / console / Command / LockableTrait.php

LockableTrait.php in UpStream: a Project Management Plugin for WordPress trunk, at vendor/symfony/console/Command/LockableTrait.php

73 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Console\Command;
13
14 use Symfony\Component\Console\Exception\LogicException;
15 use Symfony\Component\Console\Exception\RuntimeException;
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;
20
21 /**
22 * Basic lock feature for commands.
23 *
24 * @author Geoffrey Brier <geoffrey.brier@gmail.com>
25 */
26 trait LockableTrait
27 {
28 /** @var Lock */
29 private $lock;
30
31 /**
32 * Locks a command.
33 *
34 * @return bool
35 */
36 private function lock($name = null, $blocking = false)
37 {
38 if (!class_exists(SemaphoreStore::class)) {
39 throw new RuntimeException('To enable the locking feature you must install the symfony/lock component.');
40 }
41
42 if (null !== $this->lock) {
43 throw new LogicException('A lock is already in place.');
44 }
45
46 if (SemaphoreStore::isSupported($blocking)) {
47 $store = new SemaphoreStore();
48 } else {
49 $store = new FlockStore();
50 }
51
52 $this->lock = (new Factory($store))->createLock($name ?: $this->getName());
53 if (!$this->lock->acquire($blocking)) {
54 $this->lock = null;
55
56 return false;
57 }
58
59 return true;
60 }
61
62 /**
63 * Releases the command lock if there is one.
64 */
65 private function release()
66 {
67 if ($this->lock) {
68 $this->lock->release();
69 $this->lock = null;
70 }
71 }
72 }
73