10000 [Messenger][Process] add `RunProcessMessage` and `RunProcessMessageHandler` by kbond · Pull Request #49813 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger][Process] add RunProcessMessage and RunProcessMessageHandler #49813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Notifier\TexterInterface;
use Symfony\Component\Notifier\Transport\TransportFactoryInterface as NotifierTransportFactoryInterface;
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
Expand Down Expand Up @@ -240,6 +241,12 @@ public function load(array $configs, ContainerBuilder $container)

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

$loader->load('process.php');

if (!class_exists(RunProcessMessageHandler::class)) {
$container->removeDefinition('process.messenger.process_message_handler');
}

if ($this->hasConsole()) {
$loader->load('console.php');

Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\Process\Messenger\RunProcessMessageHandler;

return static function (ContainerConfigurator $container) {
$container
->services()
->set('process.messenger.process_message_handler', RunProcessMessageHandler::class)
->tag('messenger.message_handler')
;
};
5 changes: 5 additions & 0 deletions src/Symfony/Component/Process/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Add `RunProcessMessage` and `RunProcessMessageHandler`

5.2.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Process\Exception;

use Symfony\Component\Process\Messenger\RunProcessContext;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class RunProcessFailedException extends RuntimeException
{
public function __construct(ProcessFailedException $exception, public readonly RunProcessContext $context)
{
parent::__construct($exception->getMessage(), $exception->getCode());
}
}
33 changes: 33 additions & 0 deletions src/Symfony/Component/Process/Messenger/RunProcessContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Process\Messenger;

use Symfony\Component\Process\Process;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class RunProcessContext extends RunProcessMessage
{
public readonly ?int $exitCode;
public readonly ?string $output;
public readonly ?string $errorOutput;

public function __construct(RunProcessMessage $message, Process $process)
{
parent::__construct($message->command, $message->cwd, $message->env, $message->input, $message->timeout);

$this->exitCode = $process->getExitCode();
$this->output = $process->isOutputDisabled() ? null : $process->getOutput();
$this->errorOutput = $process->isOutputDisabled() ? null : $process->getErrorOutput();
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/Process/Messenger/RunProcessMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Process\Messenger;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
class RunProcessMessage implements \Stringable
{
public function __construct(
public readonly array $command,
public readonly ?string $cwd = null,
public readonly ?array $env = null,
public readonly mixed $input = null,
public readonly ?float $timeout = 60.0,
) {
}

public function __toString(): string
{
return \implode(' ', $this->command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Process\Messenger;

use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\RunProcessFailedException;
use Symfony\Component\Process\Process;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class RunProcessMessageHandler
{
public function __invoke(RunProcessMessage $message): RunProcessContext
{
$process = new Process($message->command, $message->cwd, $message->env, $message->input, $message->timeout);

try {
return new RunProcessContext($message, $process->mustRun());
} catch (ProcessFailedException $e) {
throw new RunProcessFailedException($e, new RunProcessContext($message, $e->getProcess()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Process\Tests\Messenger;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Exception\RunProcessFailedException;
use Symfony\Component\Process\Messenger\RunProcessMessage;
use Symfony\Component\Process\Messenger\RunProcessMessageHandler;

class RunProcessMessageHandlerTest extends TestCase
{
public function testRunSuccessfulProcess()
{
$context = (new RunProcessMessageHandler())(new RunProcessMessage(['ls'], cwd: __DIR__));

$this->assertSame(['ls'], $context->command);
$this->assertSame(0, $context->exitCode);
$this->assertStringContainsString(basename(__FILE__), $context->output);
}

public function testRunFailedProcess()
{
try {
(new RunProcessMessageHandler())(new RunProcessMessage(['invalid']));
} catch (RunProcessFailedException $e) {
$this->assertSame(['invalid'], $e->context->command);
$this->assertSame(127, $e->context->exitCode);

return;
}

$this->fail('Exception not thrown');
}
}
0