Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Console\Command;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Console\Exception\LogicException;
|
Chris@0
|
15 use Symfony\Component\Console\Exception\RuntimeException;
|
Chris@0
|
16 use Symfony\Component\Filesystem\LockHandler;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Basic lock feature for commands.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @author Geoffrey Brier <geoffrey.brier@gmail.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 trait LockableTrait
|
Chris@0
|
24 {
|
Chris@0
|
25 private $lockHandler;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Locks a command.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @return bool
|
Chris@0
|
31 */
|
Chris@0
|
32 private function lock($name = null, $blocking = false)
|
Chris@0
|
33 {
|
Chris@0
|
34 if (!class_exists(LockHandler::class)) {
|
Chris@0
|
35 throw new RuntimeException('To enable the locking feature you must install the symfony/filesystem component.');
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 if (null !== $this->lockHandler) {
|
Chris@0
|
39 throw new LogicException('A lock is already in place.');
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 $this->lockHandler = new LockHandler($name ?: $this->getName());
|
Chris@0
|
43
|
Chris@0
|
44 if (!$this->lockHandler->lock($blocking)) {
|
Chris@0
|
45 $this->lockHandler = null;
|
Chris@0
|
46
|
Chris@0
|
47 return false;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 return true;
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Releases the command lock if there is one.
|
Chris@0
|
55 */
|
Chris@0
|
56 private function release()
|
Chris@0
|
57 {
|
Chris@0
|
58 if ($this->lockHandler) {
|
Chris@0
|
59 $this->lockHandler->release();
|
Chris@0
|
60 $this->lockHandler = null;
|
Chris@0
|
61 }
|
Chris@0
|
62 }
|
Chris@0
|
63 }
|