-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
8000
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Messenger][Process] add
RunProcessMessage
and `RunProcessMessageHa…
…ndler`
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Symfony/Bundle/FrameworkBundle/Resources/config/process.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.4 | ||
--- | ||
|
||
* Add `RunProcessMessage` and `RunProcessMessageHandler` | ||
|
||
5.2.0 | ||
----- | ||
|
||
|
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Process/Exception/RunProcessFailedException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
src/Symfony/Component/Process/Messenger/RunProcessContext.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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
32
src/Symfony/Component/Process/Messenger/RunProcessMessage.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/Process/Messenger/RunProcessMessageHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/Process/Tests/Messenger/RunProcessMessageHandlerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.