Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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\Lock\LockFactory;
16
use Symfony\Component\Lock\LockInterface;
17
use Symfony\Component\Lock\Store\FlockStore;
18
use Symfony\Component\Lock\Store\SemaphoreStore;
19
 
20
/**
21
 * Basic lock feature for commands.
22
 *
23
 * @author Geoffrey Brier <geoffrey.brier@gmail.com>
24
 */
25
trait LockableTrait
26
{
27
    private ?LockInterface $lock = null;
28
 
29
    /**
30
     * Locks a command.
31
     */
32
    private function lock(string $name = null, bool $blocking = false): bool
33
    {
34
        if (!class_exists(SemaphoreStore::class)) {
35
            throw new LogicException('To enable the locking feature you must install the symfony/lock component.');
36
        }
37
 
38
        if (null !== $this->lock) {
39
            throw new LogicException('A lock is already in place.');
40
        }
41
 
42
        if (SemaphoreStore::isSupported()) {
43
            $store = new SemaphoreStore();
44
        } else {
45
            $store = new FlockStore();
46
        }
47
 
48
        $this->lock = (new LockFactory($store))->createLock($name ?: $this->getName());
49
        if (!$this->lock->acquire($blocking)) {
50
            $this->lock = null;
51
 
52
            return false;
53
        }
54
 
55
        return true;
56
    }
57
 
58
    /**
59
     * Releases the command lock if there is one.
60
     */
61
    private function release()
62
    {
63
        if ($this->lock) {
64
            $this->lock->release();
65
            $this->lock = null;
66
        }
67
    }
68
}