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

Appearance settings

Commit a2fc092

Browse filesBrowse files
feature #54135 [Console] Add a way to use custom lock factory in lockableTrait (VincentLanglet)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [Console] Add a way to use custom lock factory in lockableTrait | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT The LockableTrait only use SemaphoreStore or FlockStore, but currently we cannot use MemcachedStore or RedisStore. When using a custom LockFactory everywhere in the codebase, it would be useful to chose if the commande need a server-related store or not. I'm not sure if I have a way to provide autowire to the `setLockFactory` method without introducing a BC break for people who rely on the fact that the LockableTrait use a different LockFactory than the one configured the `framework.lock` dsn. Commits ------- 28c73f8 [Console] Add a way to use custom lock factory in lockableTrait
2 parents 099daf5 + 28c73f8 commit a2fc092
Copy full SHA for a2fc092

File tree

Expand file treeCollapse file tree

3 files changed

+62
-5
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+62
-5
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Command/LockableTrait.php
+11-5Lines 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
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Command/LockableTraitTest.php
+16Lines changed: 16 additions & 0 deletions
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
}
+35Lines changed: 35 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.