Skip to content

Commit 243baf1

Browse files
committed
[Messenger][Process] add fromShellCommandline to RunProcessMessage
Allows using the Process::fromShellCommandline when using a RunProcessMessage
1 parent d824d53 commit 243baf1

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

src/Symfony/Component/Process/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5+
7.3
6+
---
7+
* Add `RunProcessMessage::fromShellCommandline()` to instantiate a Process via the fromShellCommandline method
8+
49
7.1
510
---
611

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@ public function __construct(
2222
public readonly ?array $env = null,
2323
public readonly mixed $input = null,
2424
public readonly ?float $timeout = 60.0,
25+
public readonly ?string $commandLine = null,
2526
) {
2627
}
2728

2829
public function __toString(): string
2930
{
30-
return implode(' ', $this->command);
31+
return $this->commandLine ?? implode(' ', $this->command);
32+
}
33+
34+
/**
35+
* Create a process message instance that will instantiate a Process using the fromShellCommandline method.
36+
*
37+
* @see Process::fromShellCommandline
38+
*/
39+
public static function fromShellCommandline(string $command, ?string $cwd = null, ?array $env = null, mixed $input = null, ?float $timeout = 60): static
40+
{
41+
return new static([], $cwd, $env, $input, $timeout, $command);
3142
}
3243
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ final class RunProcessMessageHandler
2222
{
2323
public function __invoke(RunProcessMessage $message): RunProcessContext
2424
{
25-
$process = new Process($message->command, $message->cwd, $message->env, $message->input, $message->timeout);
25+
match ($message->commandLine) {
26+
null => $process = new Process($message->command, $message->cwd, $message->env, $message->input, $message->timeout),
27+
default => $process = Process::fromShellCommandline($message->commandLine, $message->cwd, $message->env, $message->input, $message->timeout),
28+
};
2629

2730
try {
2831
return new RunProcessContext($message, $process->mustRun());

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

+27
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,31 @@ public function testRunFailedProcess()
4444

4545
$this->fail('Exception not thrown');
4646
}
47+
48+
public function testRunSuccessfulProcessFromShellCommandline()
49+
{
50+
$context = (new RunProcessMessageHandler())(RunProcessMessage::fromShellCommandline('ls | grep Test', cwd: __DIR__));
51+
52+
$this->assertSame('ls | grep Test', $context->message->commandLine);
53+
$this->assertSame(0, $context->exitCode);
54+
$this->assertStringContainsString(basename(__FILE__), $context->output);
55+
}
56+
57+
public function testRunFailedProcessFromShellCommandline()
58+
{
59+
try {
60+
(new RunProcessMessageHandler())(RunProcessMessage::fromShellCommandline('invalid'));
61+
} catch (RunProcessFailedException $e) {
62+
$this->assertSame('invalid', $e->context->message->commandLine);
63+
$this->assertContains(
64+
$e->context->exitCode,
65+
[null, '\\' === \DIRECTORY_SEPARATOR ? 1 : 127],
66+
'Exit code should be 1 on Windows, 127 on other systems, or null',
67+
);
68+
69+
return;
70+
}
71+
72+
$this->fail('Exception not thrown');
73+
}
4774
}

0 commit comments

Comments
 (0)