4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Command;
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;
22 * Basic lock feature for commands.
24 * @author Geoffrey Brier <geoffrey.brier@gmail.com>
36 private function lock($name = null, $blocking = false)
38 if (!class_exists(SemaphoreStore::class)) {
39 throw new RuntimeException('To enable the locking feature you must install the symfony/lock component.');
42 if (null !== $this->lock) {
43 throw new LogicException('A lock is already in place.');
46 if (SemaphoreStore::isSupported($blocking)) {
47 $store = new SemaphoreStore();
49 $store = new FlockStore();
52 $this->lock = (new Factory($store))->createLock($name ?: $this->getName());
53 if (!$this->lock->acquire($blocking)) {
63 * Releases the command lock if there is one.
65 private function release()
68 $this->lock->release();