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

Skip to content

[Console] Add a way to use custom lock factory in lockableTrait #54135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/Symfony/Component/Console/Command/LockableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ trait LockableTrait
{
private ?LockInterface $lock = null;

private ?LockFactory $lockFactory = null;

/**
* Locks a command.
*/
Expand All @@ -39,13 +41,17 @@ private function lock(?string $name = null, bool $blocking = false): bool
throw new LogicException('A lock is already in place.');
}

if (SemaphoreStore::isSupported()) {
$store = new SemaphoreStore();
} else {
$store = new FlockStore();
if (null === $this->lockFactory) {
if (SemaphoreStore::isSupported()) {
$store = new SemaphoreStore();
} else {
$store = new FlockStore();
}

$this->lockFactory = (new LockFactory($store));
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\SharedLockInterface;
use Symfony\Component\Lock\Store\FlockStore;
use Symfony\Component\Lock\Store\SemaphoreStore;

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

public function testLockIsReleased()
Expand Down Expand Up @@ -64,4 +66,18 @@ public function testMultipleLockCallsThrowLogicException()
$tester = new CommandTester($command);
$this->assertSame(1, $tester->execute([]));
}

public function testCustomLockFactoryIsUsed()
{
$lockFactory = $this->createMock(LockFactory::class);
$command = new \FooLock3Command($lockFactory);

$tester = new CommandTester($command);

$lock = $this->createMock(SharedLockInterface::class);
$lock->method('acquire')->willReturn(false);

$lockFactory->expects(static::once())->method('createLock')->willReturn($lock);
$this->assertSame(1, $tester->execute([]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Lock\LockFactory;

class FooLock3Command extends Command
{
use LockableTrait;

public function __construct(LockFactory $lockFactory)
{
parent::__construct();

$this->lockFactory = $lockFactory;
}

protected function configure(): void
{
$this->setName('foo:lock3');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->lock()) {
return 1;
}

$this->release();

return 2;
}
}