Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28c73f8

Browse files
VincentLangletnicolas-grekas
authored andcommitted
[Console] Add a way to use custom lock factory in lockableTrait
1 parent 272f021 commit 28c73f8

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

src/Symfony/Component/Console/Command/LockableTrait.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ trait LockableTrait
2626
{
2727
private ?LockInterface $lock = null;
2828

29+
private ?LockFactory $lockFactory = null;
30+
2931
/**
3032
* Locks a command.
3133
*/
@@ -39,13 +41,17 @@ private function lock(?string $name = null, bool $blocking = false): bool
3941
throw new LogicException('A lock is already in place.');
4042
}
4143

42-
if (SemaphoreStore::isSupported()) {
43-
$store = new SemaphoreStore();
44-
} else {
45-
$store = new FlockStore();
44+
if (null === $this->lockFactory) {
45+
if (SemaphoreStore::isSupported()) {
46+
$store = new SemaphoreStore();
47+
} else {
48+
$store = new FlockStore();
49+
}
50+
51+
$this->lockFactory = (new LockFactory($store));
4652
}
4753

48-
$this->lock = (new LockFactory($store))->createLock($name ?: $this->getName());
54+
$this->lock = $this->lockFactory->createLock($name ?: $this->getName());
4955
if (!$this->lock->acquire($blocking)) {
5056
$this->lock = null;
5157

src/Symfony/Component/Console/Tests/Command/LockableTraitTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Tester\CommandTester;
1616
use Symfony\Component\Lock\LockFactory;
17+
use Symfony\Component\Lock\SharedLockInterface;
1718
use Symfony\Component\Lock\Store\FlockStore;
1819
use Symfony\Component\Lock\Store\SemaphoreStore;
1920

@@ -26,6 +27,7 @@ public static function setUpBeforeClass(): void
2627
self::$fixturesPath = __DIR__.'/../Fixtures/';
2728
require_once self::$fixturesPath.'/FooLockCommand.php';
2829
require_once self::$fixturesPath.'/FooLock2Command.php';
30+
require_once self::$fixturesPath.'/FooLock3Command.php';
2931
}
3032

3133
public function testLockIsReleased()
@@ -64,4 +66,18 @@ public function testMultipleLockCallsThrowLogicException()
6466
$tester = new CommandTester($command);
6567
$this->assertSame(1, $tester->execute([]));
6668
}
69+
70+
public function testCustomLockFactoryIsUsed()
71+
{
72+
$lockFactory = $this->createMock(LockFactory::class);
73+
$command = new \FooLock3Command($lockFactory);
74+
75+
$tester = new CommandTester($command);
76+
77+
$lock = $this->createMock(SharedLockInterface::class);
78+
$lock->method('acquire')->willReturn(false);
79+
80+
$lockFactory->expects(static::once())->method('createLock')->willReturn($lock);
81+
$this->assertSame(1, $tester->execute([]));
82+
}
6783
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Command\LockableTrait;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
use Symfony\Component\Lock\LockFactory;
8+
9+
class FooLock3Command extends Command
10+
{
11+
use LockableTrait;
12+
13+
public function __construct(LockFactory $lockFactory)
14+
{
15+
parent::__construct();
16+
17+
$this->lockFactory = $lockFactory;
18+
}
19+
20+
protected function configure(): void
21+
{
22+
$this->setName('foo:lock3');
23+
}
24+
25+
protected function execute(InputInterface $input, OutputInterface $output): int
26+
{
27+
if (!$this->lock()) {
28+
return 1;
29+
}
30+
31+
$this->release();
32+
33+
return 2;
34+
}
35+
}

0 commit comments

Comments
 (0)