Skip to content

Commit 6cdc5e2

Browse files
committed
[Process] add RunProcessContext and RunProcessFailedException
1 parent 7077601 commit 6cdc5e2

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Process\Messenger;
13+
14+
use Symfony\Component\Process\Process;
15+
16+
/**
17+
* @author Kevin Bond <kevinbond@gmail.com>
18+
*/
19+
final class RunProcessContext extends RunProcessMessage
20+
{
21+
public readonly ?int $exitCode;
22+
public readonly ?string $output;
23+
public readonly ?string $errorOutput;
24+
25+
public function __construct(RunProcessMessage $message, Process $process)
26+
{
27+
parent::__construct($message->command, $message->cwd, $message->env, $message->input, $message->timeout);
28+
29+
$this->exitCode = $process->getExitCode();
30+
$this->output = $process->isOutputDisabled() ? null : $process->getOutput();
31+
$this->errorOutput = $process->isOutputDisabled() ? null : $process->getErrorOutput();
32+
}
33+
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Process\Messenger;
13+
14+
use Symfony\Component\Process\Exception\ProcessFailedException;
15+
use Symfony\Component\Process\Exception\RuntimeException;
16+
17+
/**
18+
* @author Kevin Bond <kevinbond@gmail.com>
19+
*/
20+
final class RunProcessFailedException extends RuntimeException
21+
{
22+
public function __construct(ProcessFailedException $exception, public readonly RunProcessContext $context)
23+
{
24+
parent::__construct($exception->getMessage(), $exception->getCode());
25+
}
26+
}

src/Symfony/Component/Process/Messenger/RunProcessMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @author Kevin Bond <kevinbond@gmail.com>
1616
*/
17-
final class RunProcessMessage implements \Stringable
17+
class RunProcessMessage implements \Stringable
1818
{
1919
public function __construct(
2020
public readonly string $command,

src/Symfony/Component/Process/Messenger/RunProcessMessageHandler.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
namespace Symfony\Component\Process\Messenger;
1313

14+
use Symfony\Component\Process\Exception\ProcessFailedException;
1415
use Symfony\Component\Process\Process;
1516

1617
/**
1718
* @author Kevin Bond <kevinbond@gmail.com>
1819
*/
1920
final class RunProcessMessageHandler
2021
{
21-
public function __invoke(RunProcessMessage $message): Process
22+
public function __invoke(RunProcessMessage $message): RunProcessContext
2223
{
2324
$process = Process::fromShellCommandline(
2425
$message->command,
@@ -28,6 +29,10 @@ public function __invoke(RunProcessMessage $message): Process
2829
$message->timeout,
2930
);
3031

31-
return $process->mustRun();
32+
try {
33+
return new RunProcessContext($message, $process->mustRun());
34+
} catch (ProcessFailedException $e) {
35+
throw new RunProcessFailedException($e, new RunProcessContext($message, $e->getProcess()));
36+
}
3237
}
3338
}

src/Symfony/Component/Process/Tests/Messenger/RunProcessMessageHandlerTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,32 @@
1212
namespace Symfony\Component\Process\Tests\Messenger;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Process\Messenger\RunProcessFailedException;
1516
use Symfony\Component\Process\Messenger\RunProcessMessage;
1617
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;
1718

1819
class RunProcessMessageHandlerTest extends TestCase
1920
{
20-
public function testCanExecuteProcess()
21+
public function testRunSuccessfulProcess()
2122
{
22-
$process = (new RunProcessMessageHandler())(new RunProcessMessage('ls', cwd: __DIR__));
23+
$context = (new RunProcessMessageHandler())(new RunProcessMessage('ls', cwd: __DIR__));
2324

24-
$this->assertTrue($process->isSuccessful());
25-
$this->assertStringContainsString(basename(__FILE__), $process->getOutput());
25+
$this->assertSame('ls', $context->command);
26+
$this->assertSame(0, $context->exitCode);
27+
$this->assertStringContainsString(basename(__FILE__), $context->output);
28+
}
29+
30+
public function testRunFailedProcess()
31+
{
32+
try {
33+
(new RunProcessMessageHandler())(new RunProcessMessage('invalid'));
34+
} catch (RunProcessFailedException $e) {
35+
$this->assertSame('invalid', $e->context->command);
36+
$this->assertSame(127, $e->context->exitCode);
37+
38+
return;
39+
}
40+
41+
$this->fail('Exception not thrown');
2642
}
2743
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy