Skip to content

Commit b3de71b

Browse files
committed
[Process] add ProcessMessage and ProcessMessageHandler
1 parent 5e6ea11 commit b3de71b

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
@@ -199,6 +199,7 @@
199199
use Symfony\Component\Notifier\Recipient\Recipient;
200200
use Symfony\Component\Notifier\TexterInterface;
201201
use Symfony\Component\Notifier\Transport\TransportFactoryInterface as NotifierTransportFactoryInterface;
202+
use Symfony\Component\Process\Messenger\ProcessMessageHandler;
202203
use Symfony\Component\PropertyAccess\PropertyAccessor;
203204
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
204205
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
@@ -307,6 +308,12 @@ public function load(array $configs, ContainerBuilder $container)
307308

308309
$container->registerAliasForArgument('parameter_bag', PsrContainerInterface::class);
309310

311+
$loader->load('process.php');
312+
313+
if (!class_exists(ProcessMessageHandler::class)) {
314+
$container->removeDefinition('process.messenger.process_message_handler');
315+
}
316+
310317
if ($this->hasConsole()) {
311318
$loader->load('console.php');
312319

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\ProcessMessageHandler;
15+
16+
return static function (ContainerConfigurator $container) {
17+
$container
18+
->services()
19+
->set('process.messenger.process_message_handler', ProcessMessageHandler::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.3
5+
---
6+
7+
* Add `ProcessMessage` and `ProcessMessageHandler`
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 ProcessMessage 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 ProcessMessageHandler
20+
{
21+
public function __invoke(ProcessMessage $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\ProcessMessage;
16+
use Symfony\Component\Process\Messenger\ProcessMessageHandler;
17+
18+
class ProcessMessageHandlerTest extends TestCase
19+
{
20+
public function testCanExecuteProcess()
21+
{
22+
$process = (new ProcessMessageHandler())(new ProcessMessage('ls', cwd: __DIR__));
23+
24+
$this->assertTrue($process->isSuccessful());
25+
$this->assertStringContainsString(basename(__FILE__), $process->getOutput());
26+
}
27+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy