comparison vendor/symfony/console/Command/LockableTrait.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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\Filesystem\LockHandler;
17
18 /**
19 * Basic lock feature for commands.
20 *
21 * @author Geoffrey Brier <geoffrey.brier@gmail.com>
22 */
23 trait LockableTrait
24 {
25 private $lockHandler;
26
27 /**
28 * Locks a command.
29 *
30 * @return bool
31 */
32 private function lock($name = null, $blocking = false)
33 {
34 if (!class_exists(LockHandler::class)) {
35 throw new RuntimeException('To enable the locking feature you must install the symfony/filesystem component.');
36 }
37
38 if (null !== $this->lockHandler) {
39 throw new LogicException('A lock is already in place.');
40 }
41
42 $this->lockHandler = new LockHandler($name ?: $this->getName());
43
44 if (!$this->lockHandler->lock($blocking)) {
45 $this->lockHandler = null;
46
47 return false;
48 }
49
50 return true;
51 }
52
53 /**
54 * Releases the command lock if there is one.
55 */
56 private function release()
57 {
58 if ($this->lockHandler) {
59 $this->lockHandler->release();
60 $this->lockHandler = null;
61 }
62 }
63 }