8000 [Messenger][Process] add `RunProcessMessage` and `RunProcessMessageHa… · symfony/symfony@abde61a · GitHub
[go: up one dir, main page]

Skip to content

Commit abde61a

Browse files
kbondfabpot
authored andcommitted
[Messenger][Process] add RunProcessMessage and RunProcessMessageHandler
1 parent 510e51f commit abde61a

File tree

8 files changed

+200
-0
lines changed

8 files changed

+200
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
use Symfony\Component\Notifier\Recipient\Recipient;
130130
use Symfony\Component\Notifier\TexterInterface;
131131
use Symfony\Component\Notifier\Transport\TransportFactoryInterface as NotifierTransportFactoryInterface;
132+
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;
132133
use Symfony\Component\PropertyAccess\PropertyAccessor;
133134
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
134135
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
@@ -240,6 +241,12 @@ public function load(array $configs, ContainerBuilder $container)
240241

241242
$container->registerAliasForArgument('parameter_bag', PsrContainerInterface::class);
242243

244+
$loader->load('process.php');
245+
246+
if (!class_exists(RunProcessMessageHandler::class)) {
247+
$container->removeDefinition('process.messenger.process_message_handler');
248+
}
249+
243250
if ($this->hasConsole()) {
244251
$loader->load('console.php');
245252

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;
15+
16+
return static function (ContainerConfigurator $container) {
17+
$container
18+
->services()
19+
->set('process.messenger.process_message_handler', RunProcessMessageHandler::class)
20+
->tag('messenger.message_handler')
21+
;
22+
};

src/Symfony/Component/Process/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+
namespace Symfony\Component\Process\Exception;
13+
14+
use Symfony\Component\Process\Messenger\RunProcessContext;
15+
16+
/**
17+
* @author Kevin Bond <kevinbond@gmail.com>
18+
*/
19+
final class RunProcessFailedException extends RuntimeException
20+
{
21+
public function __construct(ProcessFailedException $exception, public readonly RunProcessContext $context)
22+
{
23+
parent::__construct($exception->getMessage(), $exception->getCode());
24+
}
25+
}
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+
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+
}
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+
namespace Symfony\Component\Process\Messenger;
13+
14+
/**
15+
* @author Kevin Bond <kevinbond@gmail.com>
16+
*/
17+
class RunProcessMessage implements \Stringable
18+
{
19+
public function __construct(
20+
public readonly array $command,
21+
public readonly ?string $cwd = null,
22+
public readonly ?array $env = null,
23+
public readonly mixed $input = null,
24+
public readonly ?float $timeout = 60.0,
25+
) {
26+
}
27+
28+
public function __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+
namespace Symfony\Component\Process\Messenger;
13+
14+
use Symfony\Component\Process\Exception\ProcessFailedException;
15+
use Symfony\Component\Process\Exception\RunProcessFailedException;
16+
use Symfony\Component\Process\Process;
17+
18+
/**
19+
* @author Kevin Bond <kevinbond@gmail.com>
20+
*/
21+
final class RunProcessMessageHandler
22+
{
23+
public function __invoke(RunProcessMessage $message): RunProcessContext
24+
{
25+
$process = new Process($message->command, $message->cwd, $message->env, $message->input, $message->timeout);
26+
27+
try {
28+
return new RunProcessContext($message, $process->mustRun());
29+
} catch (ProcessFailedException $e) {
30+
throw new RunProcessFailedException($e, new RunProcessContext($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+
namespace Symfony\Component\Process\Tests\Messenger;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Process\Exception\RunProcessFailedException;
16+
use Symfony\Component\Process\Messenger\RunProcessMessage;
17+
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;
18+
19+
class RunProcessMessageHandlerTest extends TestCase
20+
{
21+
public function testRunSuccessfulProcess()
22+
{
23+
$context = (new RunProcessMessageHandler())(new RunProcessMessage(['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+
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');
42+
}
43+
}

0 commit comments

Comments
 (0)
0