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

Appearance settings

Commit 6fe4cb5

Browse filesBrowse files
committed
feature #59768 [Messenger][Process] add fromShellCommandline to RunProcessMessage (Staormin)
This PR was merged into the 7.3 branch. Discussion ---------- [Messenger][Process] add `fromShellCommandline` to `RunProcessMessage` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | | License | MIT The `RunProcess::fromShellCommandline()` method can be useful when using pipes or redirection. This feature allow the use of this method when using a RunProcessMessage. Commits ------- e448596 [Messenger][Process] add `fromShellCommandline` to `RunProcessMessage`
2 parents ab8a96f + e448596 commit 6fe4cb5
Copy full SHA for 6fe4cb5

File tree

4 files changed

+50
-2
lines changed
Filter options

4 files changed

+50
-2
lines changed

‎src/Symfony/Component/Process/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Process/CHANGELOG.md
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
5+
7.3
6+
---
7+
8+
* Add `RunProcessMessage::fromShellCommandline()` to instantiate a Process via the fromShellCommandline method
9+
410
7.1
511
---
612

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Process/Messenger/RunProcessMessage.php
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
class RunProcessMessage implements \Stringable
1818
{
19+
public ?string $commandLine = null;
20+
1921
public function __construct(
2022
public readonly array $command,
2123
public readonly ?string $cwd = null,
@@ -27,6 +29,19 @@ public function __construct(
2729

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Process/Messenger/RunProcessMessageHandler.php
+4-1Lines changed: 4 additions & 1 deletion
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+
$process = match ($message->commandLine) {
26+
null => new Process($message->command, $message->cwd, $message->env, $message->input, $message->timeout),
27+
default => 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Process/Tests/Messenger/RunProcessMessageHandlerTest.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,28 @@ 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+
$this->fail('Exception not thrown');
62+
} catch (RunProcessFailedException $e) {
63+
$this->assertSame('invalid', $e->context->message->commandLine);
64+
$this->assertContains(
65+
$e->context->exitCode,
66+
[null, '\\' === \DIRECTORY_SEPARATOR ? 1 : 127],
67+
'Exit code should be 1 on Windows, 127 on other systems, or null',
68+
);
69+
}
70+
}
4771
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.