Skip to content

Commit 7077601

Browse files
committed
[Process] add RunProcessMessage and RunProcessMessageHandler
1 parent 510e51f commit 7077601

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-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: 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+
final class RunProcessMessage implements \Stringable
18+
{
19+
public function __construct(
20+
public readonly string $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,
25+
) {
26+
}
27+
28+
public function __toString(): string
29+
{
30+
return $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\Process;
15+
16+
/**
17+
* @author Kevin Bond <kevinbond@gmail.com>
18+
*/
19+
final class RunProcessMessageHandler
20+
{
21+
public function __invoke(RunProcessMessage $message): Process
22+
{
23+
$process = Process::fromShellCommandline(
24+
$message->command,
25+
$message->cwd,
26+
$message->env,
27+
$message->input,
28+
$message->timeout,
29+
);
30+
31+
return $process->mustRun();
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Messenger\RunProcessMessage;
16+
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;
17+
18+
class RunProcessMessageHandlerTest extends TestCase
19+
{
20+
public function testCanExecuteProcess()
21+
{
22+
$process = (new RunProcessMessageHandler())(new RunProcessMessage('ls', cwd: __DIR__));
23+
24+
$this->assertTrue($process->isSuccessful());
25+
$this->assertStringContainsString(basename(__FILE__), $process->getOutput());
26+
}
27+
}

0 commit comments

Comments
 (0)