Skip to content

Commit 1276cfe

Browse files
kbondfabpot
authored andcommitted
[Messenger][Process] add RunProcessMessage and RunProcessMessageHandler
1 parent 0d38e48 commit 1276cfe

6 files changed

Lines changed: 171 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Add `RunProcessMessage` and `RunProcessMessageHandler`
8+
49
5.2.0
510
-----
611

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
namespaceSymfony\Component\Process\Exception;
13+
14+
useSymfony\Component\Process\Messenger\RunProcessContext;
15+
16+
/**
17+
* @author Kevin Bond <kevinbond@gmail.com>
18+
*/
19+
finalclass RunProcessFailedException extends RuntimeException
20+
{
21+
publicfunction__construct(ProcessFailedException$exception, publicreadonlyRunProcessContext$context)
22+
{
23+
parent::__construct($exception->getMessage(), $exception->getCode());
24+
}
25+
}

‎Messenger/RunProcessContext.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespaceSymfony\Component\Process\Messenger;
13+
14+
useSymfony\Component\Process\Process;
15+
16+
/**
17+
* @author Kevin Bond <kevinbond@gmail.com>
18+
*/
19+
finalclass RunProcessContext extends RunProcessMessage
20+
{
21+
publicreadonly ?int$exitCode;
22+
publicreadonly ?string$output;
23+
publicreadonly ?string$errorOutput;
24+
25+
publicfunction__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+
}

‎Messenger/RunProcessMessage.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
namespaceSymfony\Component\Process\Messenger;
13+
14+
/**
15+
* @author Kevin Bond <kevinbond@gmail.com>
16+
*/
17+
class RunProcessMessage implements \Stringable
18+
{
19+
publicfunction__construct(
20+
publicreadonlyarray$command,
21+
publicreadonly ?string$cwd = null,
22+
publicreadonly ?array$env = null,
23+
publicreadonlymixed$input = null,
24+
publicreadonly ?float$timeout = 60.0,
25+
) {
26+
}
27+
28+
publicfunction__toString(): string
29+
{
30+
return\implode('', $this->command);
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespaceSymfony\Component\Process\Messenger;
13+
14+
useSymfony\Component\Process\Exception\ProcessFailedException;
15+
useSymfony\Component\Process\Exception\RunProcessFailedException;
16+
useSymfony\Component\Process\Process;
17+
18+
/**
19+
* @author Kevin Bond <kevinbond@gmail.com>
20+
*/
21+
finalclass RunProcessMessageHandler
22+
{
23+
publicfunction__invoke(RunProcessMessage$message): RunProcessContext
24+
{
25+
$process = newProcess($message->command, $message->cwd, $message->env, $message->input, $message->timeout);
26+
27+
try {
28+
returnnewRunProcessContext($message, $process->mustRun());
29+
} catch (ProcessFailedException$e) {
30+
thrownewRunProcessFailedException($e, newRunProcessContext($message, $e->getProcess()));
31+
}
32+
}
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
namespaceSymfony\Component\Process\Tests\Messenger;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Process\Exception\RunProcessFailedException;
16+
useSymfony\Component\Process\Messenger\RunProcessMessage;
17+
useSymfony\Component\Process\Messenger\RunProcessMessageHandler;
18+
19+
class RunProcessMessageHandlerTest extends TestCase
20+
{
21+
publicfunctiontestRunSuccessfulProcess()
22+
{
23+
$context = (newRunProcessMessageHandler())(newRunProcessMessage(['ls'], cwd: __DIR__));
24+
25+
$this->assertSame(['ls'], $context->command);
26+
$this->assertSame(0, $context->exitCode);
27+
$this->assertStringContainsString(basename(__FILE__), $context->output);
28+
}
29+
30+
publicfunctiontestRunFailedProcess()
31+
{
32+
try {
33+
(newRunProcessMessageHandler())(newRunProcessMessage(['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');
42+
}
43+
}

0 commit comments

Comments
 (0)