Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit28c73f8

Browse files
VincentLangletnicolas-grekas
authored andcommitted
[Console] Add a way to use custom lock factory in lockableTrait
1 parent272f021 commit28c73f8

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

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

Lines changed: 11 additions & 5 deletions
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
thrownewLogicException('A lock is already in place.');
4042
}
4143

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

48-
$this->lock =(newLockFactory($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‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
usePHPUnit\Framework\TestCase;
1515
useSymfony\Component\Console\Tester\CommandTester;
1616
useSymfony\Component\Lock\LockFactory;
17+
useSymfony\Component\Lock\SharedLockInterface;
1718
useSymfony\Component\Lock\Store\FlockStore;
1819
useSymfony\Component\Lock\Store\SemaphoreStore;
1920

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

3133
publicfunctiontestLockIsReleased()
@@ -64,4 +66,18 @@ public function testMultipleLockCallsThrowLogicException()
6466
$tester =newCommandTester($command);
6567
$this->assertSame(1,$tester->execute([]));
6668
}
69+
70+
publicfunctiontestCustomLockFactoryIsUsed()
71+
{
72+
$lockFactory =$this->createMock(LockFactory::class);
73+
$command =new \FooLock3Command($lockFactory);
74+
75+
$tester =newCommandTester($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
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
useSymfony\Component\Console\Command\Command;
4+
useSymfony\Component\Console\Command\LockableTrait;
5+
useSymfony\Component\Console\Input\InputInterface;
6+
useSymfony\Component\Console\Output\OutputInterface;
7+
useSymfony\Component\Lock\LockFactory;
8+
9+
class FooLock3Commandextends Command
10+
{
11+
use LockableTrait;
12+
13+
publicfunction__construct(LockFactory$lockFactory)
14+
{
15+
parent::__construct();
16+
17+
$this->lockFactory =$lockFactory;
18+
}
19+
20+
protectedfunctionconfigure():void
21+
{
22+
$this->setName('foo:lock3');
23+
}
24+
25+
protectedfunctionexecute(InputInterface$input,OutputInterface$output):int
26+
{
27+
if (!$this->lock()) {
28+
return1;
29+
}
30+
31+
$this->release();
32+
33+
return2;
34+
}
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp