mirror of
https://github.com/symfony/process.git
synced 2026-03-23 23:42:06 +01:00
[Messenger][Process] add RunProcessMessage and RunProcessMessageHandler
This commit is contained in:
committed by
Fabien Potencier
parent
0d38e48b5e
commit
1276cfea91
@@ -1,6 +1,11 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
6.4
|
||||
---
|
||||
|
||||
* Add `RunProcessMessage` and `RunProcessMessageHandler`
|
||||
|
||||
5.2.0
|
||||
-----
|
||||
|
||||
|
||||
25
Exception/RunProcessFailedException.php
Normal file
25
Exception/RunProcessFailedException.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?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\Process\Exception;
|
||||
|
||||
use Symfony\Component\Process\Messenger\RunProcessContext;
|
||||
|
||||
/**
|
||||
* @author Kevin Bond <kevinbond@gmail.com>
|
||||
*/
|
||||
final class RunProcessFailedException extends RuntimeException
|
||||
{
|
||||
public function __construct(ProcessFailedException $exception, public readonly RunProcessContext $context)
|
||||
{
|
||||
parent::__construct($exception->getMessage(), $exception->getCode());
|
||||
}
|
||||
}
|
||||
33
Messenger/RunProcessContext.php
Normal file
33
Messenger/RunProcessContext.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?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\Process\Messenger;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
* @author Kevin Bond <kevinbond@gmail.com>
|
||||
*/
|
||||
final class RunProcessContext extends RunProcessMessage
|
||||
{
|
||||
public readonly ?int $exitCode;
|
||||
public readonly ?string $output;
|
||||
public readonly ?string $errorOutput;
|
||||
|
||||
public function __construct(RunProcessMessage $message, Process $process)
|
||||
{
|
||||
parent::__construct($message->command, $message->cwd, $message->env, $message->input, $message->timeout);
|
||||
|
||||
$this->exitCode = $process->getExitCode();
|
||||
$this->output = $process->isOutputDisabled() ? null : $process->getOutput();
|
||||
$this->errorOutput = $process->isOutputDisabled() ? null : $process->getErrorOutput();
|
||||
}
|
||||
}
|
||||
32
Messenger/RunProcessMessage.php
Normal file
32
Messenger/RunProcessMessage.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?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\Process\Messenger;
|
||||
|
||||
/**
|
||||
* @author Kevin Bond <kevinbond@gmail.com>
|
||||
*/
|
||||
class RunProcessMessage implements \Stringable
|
||||
{
|
||||
public function __construct(
|
||||
public readonly array $command,
|
||||
public readonly ?string $cwd = null,
|
||||
public readonly ?array $env = null,
|
||||
public readonly mixed $input = null,
|
||||
public readonly ?float $timeout = 60.0,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return \implode(' ', $this->command);
|
||||
}
|
||||
}
|
||||
33
Messenger/RunProcessMessageHandler.php
Normal file
33
Messenger/RunProcessMessageHandler.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?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\Process\Messenger;
|
||||
|
||||
use Symfony\Component\Process\Exception\ProcessFailedException;
|
||||
use Symfony\Component\Process\Exception\RunProcessFailedException;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
* @author Kevin Bond <kevinbond@gmail.com>
|
||||
*/
|
||||
final class RunProcessMessageHandler
|
||||
{
|
||||
public function __invoke(RunProcessMessage $message): RunProcessContext
|
||||
{
|
||||
$process = new Process($message->command, $message->cwd, $message->env, $message->input, $message->timeout);
|
||||
|
||||
try {
|
||||
return new RunProcessContext($message, $process->mustRun());
|
||||
} catch (ProcessFailedException $e) {
|
||||
throw new RunProcessFailedException($e, new RunProcessContext($message, $e->getProcess()));
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Tests/Messenger/RunProcessMessageHandlerTest.php
Normal file
43
Tests/Messenger/RunProcessMessageHandlerTest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?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\Process\Tests\Messenger;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Process\Exception\RunProcessFailedException;
|
||||
use Symfony\Component\Process\Messenger\RunProcessMessage;
|
||||
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;
|
||||
|
||||
class RunProcessMessageHandlerTest extends TestCase
|
||||
{
|
||||
public function testRunSuccessfulProcess()
|
||||
{
|
||||
$context = (new RunProcessMessageHandler())(new RunProcessMessage(['ls'], cwd: __DIR__));
|
||||
|
||||
$this->assertSame(['ls'], $context->command);
|
||||
$this->assertSame(0, $context->exitCode);
|
||||
$this->assertStringContainsString(basename(__FILE__), $context->output);
|
||||
}
|
||||
|
||||
public function testRunFailedProcess()
|
||||
{
|
||||
try {
|
||||
(new RunProcessMessageHandler())(new RunProcessMessage(['invalid']));
|
||||
} catch (RunProcessFailedException $e) {
|
||||
$this->assertSame(['invalid'], $e->context->command);
|
||||
$this->assertSame(127, $e->context->exitCode);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->fail('Exception not thrown');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user