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

[Console] Add support for invokable commands inLockableTrait#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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:7.3fromyceruto:lockable_invokable_command
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
Add support for invokable commands in LockableTrait
  • Loading branch information
@yceruto@fabpot
yceruto authored andfabpot committedMar 24, 2025
commit5595a32aeaa7a42390dc94fb63798957d5c94ae2
1 change: 1 addition & 0 deletionssrc/Symfony/Component/Console/CHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/Symfony/Component/Console/Command/LockableTrait.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@

namespaceSymfony\Component\Console\Command;

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

$this->lockFactory =(newLockFactory($store));
$this->lockFactory =newLockFactory($store);
}

$this->lock =$this->lockFactory->createLock($name ?:$this->getName());
if (!$name) {
if ($thisinstanceof Command) {
$name =$this->getName();
}elseif ($attribute = (new \ReflectionClass($this::class))->getAttributes(AsCommand::class)) {
$name =$attribute[0]->newInstance()->name;
}else {
thrownewLogicException(\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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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([]));
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp