-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Console][Messenger] add
RunCommandMessage
and `RunCommandMessageHa…
…ndler`
- Loading branch information
Showing
9 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/Console/Exception/RunCommandFailedException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Exception; | ||
|
||
use Symfony\Component\Console\Messenger\RunCommandContext; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class RunCommandFailedException extends RuntimeException | ||
{ | ||
public function __construct(\Throwable|string $exception, public readonly RunCommandContext $context) | ||
{ | ||
parent::__construct( | ||
$exception instanceof \Throwable ? $exception->getMessage() : $exception, | ||
$exception instanceof \Throwable ? $exception->getCode() : 0, | ||
$exception instanceof \Throwable ? $exception : null, | ||
); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/Console/Messenger/RunCommandContext.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Messenger; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class RunCommandContext extends RunCommandMessage | ||
{ | ||
public function __construct(RunCommandMessage $message, public readonly int $exitCode, public readonly string $output) | ||
{ | ||
parent::__construct($message->input, $message->throwOnFailure, $message->catchExceptions); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Symfony/Component/Console/Messenger/RunCommandMessage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Messenger; | ||
|
||
use Symfony\Component\Console\Exception\RunCommandFailedException; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
class RunCommandMessage implements \Stringable | ||
{ | ||
/** | ||
* @param bool $throwOnFailure If the command has a non-zero exit code, throw {@see RunCommandFailedException} | ||
* @param bool $catchExceptions @see Application::setCatchExceptions() | ||
*/ | ||
public function __construct( | ||
public readonly string $input, | ||
public readonly bool $throwOnFailure = true, | ||
public readonly bool $catchExceptions = false, | ||
) { | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->input; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/Console/Messenger/RunCommandMessageHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Messenger; | ||
|
||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Exception\RunCommandFailedException; | ||
use Symfony\Component\Console\Input\StringInput; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class RunCommandMessageHandler | ||
{ | ||
public function __construct(private readonly Application $application) | ||
{ | ||
} | ||
|
||
public function __invoke(RunCommandMessage $message): RunCommandContext | ||
{ | ||
$input = new StringInput($message->input); | ||
$output = new BufferedOutput(); | ||
|
||
$this->application->setCatchExceptions($message->catchExceptions); | ||
|
||
try { | ||
$exitCode = $this->application->run($input, $output); | ||
} catch (\Throwable $e) { | ||
throw new RunCommandFailedException($e, new RunCommandContext($message, Command::FAILURE, $output->fetch())); | ||
} | ||
|
||
if ($message->throwOnFailure && Command::SUCCESS !== $exitCode) { | ||
throw new RunCommandFailedException(sprintf('Command "%s" exited with code "%s".', $message->input, $exitCode), new RunCommandContext($message, $exitCode, $output->fetch())); | ||
} | ||
|
||
return new RunCommandContext($message, $exitCode, $output->fetch()); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/Symfony/Component/Console/Tests/Messenger/RunCommandMessageHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Tests\Messenger; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Exception\RunCommandFailedException; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Messenger\RunCommandMessage; | ||
use Symfony\Component\Console\Messenger\RunCommandMessageHandler; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class RunCommandMessageHandlerTest extends TestCase | ||
{ | ||
public function testExecutesCommand() | ||
{ | ||
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand()); | ||
$context = $handler(new RunCommandMessage('test:command')); | ||
|
||
$this->assertSame(0, $context->exitCode); | ||
$this->assertStringContainsString('some message', $context->output); | ||
} | ||
|
||
public function testExecutesCommandThatThrowsException() | ||
{ | ||
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand()); | ||
|
||
try { | ||
$handler(new RunCommandMessage('test:command --throw')); | ||
} catch (RunCommandFailedException $e) { | ||
$this->assertSame(1, $e->context->exitCode); | ||
$this->assertStringContainsString('some message', $e->context->output); | ||
$this->assertInstanceOf(\RuntimeException::class, $e->getPrevious()); | ||
$this->assertSame('exception message', $e->getMessage()); | ||
|
||
return; | ||
} | ||
|
||
$this->fail('Exception not thrown.'); | ||
} | ||
|
||
public function testExecutesCommandThatCatchesThrownException() | ||
{ | ||
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand()); | ||
$context = $handler(new RunCommandMessage('test:command --throw -v', throwOnFailure: false, catchExceptions: true)); | ||
|
||
$this->assertSame(1, $context->exitCode); | ||
$this->assertStringContainsString('[RuntimeException]', $context->output); | ||
$this->assertStringContainsString('exception message', $context->output); | ||
} | ||
|
||
public function testThrowOnNonSuccess() | ||
{ | ||
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand()); | ||
|
||
try { | ||
$handler(new RunCommandMessage('test:command --exit=1')); | ||
} catch (RunCommandFailedException $e) { | ||
$this->assertSame(1, $e->context->exitCode); | ||
$this->assertStringContainsString('some message', $e->context->output); | ||
$this->assertSame('Command "test:command --exit=1" exited with code "1".', $e->getMessage()); | ||
$this->assertNull($e->getPrevious()); | ||
|
||
return; | ||
} | ||
|
||
$this->fail('Exception not thrown.'); | ||
} | ||
|
||
private function createApplicationWithCommand(): Application | ||
{ | ||
$application = new Application(); | ||
$application->setAutoExit(false); | ||
$application->addCommands([ | ||
new class() extends Command { | ||
public function configure(): void | ||
{ | ||
$this | ||
->setName('test:command') | ||
->addOption('throw') | ||
->addOption('exit', null, InputOption::VALUE_REQUIRED, 0) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$output->write('some message'); | ||
|
||
if ($input->getOption('throw')) { | ||
throw new \RuntimeException('exception message'); | ||
} | ||
|
||
return (int) $input->getOption('exit'); | ||
} | ||
}, | ||
]); | ||
|
||
return $application; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters