10000 [Console] Add support for invokable commands in `LockableTrait` by yceruto · Pull Request #60024 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Add support for invokable commands in LockableTrait #60024

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 24, 2025
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* Add support for help definition via `AsCommand` attribute
* Deprecate methods `Command::getDefaultName()` and `Command::getDefaultDescription()` in favor of the `#[AsCommand]` attribute
* Add support for Markdown format in `Table`
* Add support for `LockableTrait` in invokable commands

7.2
---
Expand Down
15 changes: 13 additions & 2 deletions src/Symfony/Component/Console/Command/LockableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Console\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\LockInterface;
Expand Down Expand Up @@ -48,10 +49,20 @@ private function lock(?string $name = null, bool $blocking = false): bool
$store = new FlockStore();
}

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

$this->lock = $this->lockFactory->createLock($name ?: $this->getName());
if (!$name) {
if ($this instanceof Command) {
$name = $this->getName();
} elseif ($attribute = (new \ReflectionClass($this::class))->getAttributes(AsCommand::class)) {
$name = $attribute[0]->newInstance()->name;
} else {
throw new LogicException(\sprintf('Lock name missing: provide it via "%s()", #[AsCommand] attribute, or by extending Command class.', __METHOD__));
}
}

$this->lock = $this->lockFactory->createLock($name);
if (!$this->lock->acquire($blocking)) {
$this->lock = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\SharedLockInterface;
Expand All @@ -28,6 +29,7 @@ public static function setUpBeforeClass(): void
require_once self::$fixturesPath.'/FooLockCommand.php';
require_once self::$fixturesPath.'/FooLock2Command.php';
require_once self::$fixturesPath.'/FooLock3Command.php';
require_once self::$fixturesPath.'/FooLock4InvokableCommand.php';
}

public function testLockIsReleased()
Expand Down Expand Up @@ -80,4 +82,25 @@ public function testCustomLockFactoryIsUsed()
$lockFactory->expects(static::once())->method('createLock')->willReturn($lock);
$this->assertSame(1, $tester->execute([]));
}

public function testLockInvokableCommandReturnsFalseIfAlreadyLockedByAnotherCommand()
{
$command = new Command('foo:lock4');
$command->setCode(new \FooLock4InvokableCommand());

if (SemaphoreStore::isSupported()) {
$store = new SemaphoreStore();
} else {
$store = new FlockStore();
}

$lock = (new LockFactory($store))->createLock($command->getName());
$lock->acquire();

$tester = new CommandTester($command);
$this->assertSame(Command::FAILURE, $tester->execute([]));

$lock->release();
$this->assertSame(Command::SUCCESS, $tester->execute([]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LockableTrait;

#[AsCommand(name: 'foo:lock4')]
class FooLock4InvokableCommand
{
use LockableTrait;

public function __invoke(): int
{
if (!$this->lock()) {
return Command::FAILURE;
}

$this->release();

return Command::SUCCESS;
}
}
Loading
0