Skip to content

Commit

Permalink
Migrated code from symfony/process to react/child-process
Browse files Browse the repository at this point in the history
  • Loading branch information
gplanchat committed Nov 21, 2023
1 parent 8ea8526 commit 9e24b6f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
59 changes: 34 additions & 25 deletions src/Console/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,37 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function dataflowWorker(Console\Style\SymfonyStyle $style, string $cwd, string $path, string $entrypoint): Process
{
$source =<<<PHP
\$dotenv = new Dotenv();
\$dotenv->usePutenv();
if (file_exists(\$file = $cwd.'/.env')) {
\$dotenv->loadEnv(\$file);
}
if (file_exists(\$file = $cwd.'/'.{$path}.'/.env')) {
\$dotenv->loadEnv(\$file);
}
<?php
declare(strict_types=1);
/** @var ClassLoader \$autoload */
\$autoload = include 'vendor/autoload.php';
\$autoload = include '{$cwd}/{$path}/vendor/autoload.php';
\$autoload->addClassMap([
/* @phpstan-ignore-next-line */
\ProjectServiceContainer::class => 'container.php',
]);
\$autoload->register();
\$dotenv = new \Symfony\Component\Dotenv\Dotenv();
\$dotenv->usePutenv();
\$runtime = new PipelineConsoleRuntime(
new Symfony\Component\Console\Output\StreamOutput(STDOUT),
if (file_exists(\$file = '{$cwd}/.env')) {
\$dotenv->loadEnv(\$file);
}
if (file_exists(\$file = '{$cwd}/{$path}/.env')) {
\$dotenv->loadEnv(\$file);
}
\$runtime = new \Kiboko\Component\Satellite\Builder\Pipeline\ConsoleRuntime(
new \Symfony\Component\Console\Output\StreamOutput(STDOUT),
new \Kiboko\Component\Pipeline\Pipeline(
new \Kiboko\Component\Pipeline\PipelineRunner(
new \Psr\Log\NullLogger()
)
),
);
\$satellite = include '$entrypoint';
\$satellite = include '{$cwd}/{$path}/$entrypoint';
\$satellite(\$runtime);
\$runtime->run();
Expand All @@ -112,11 +115,23 @@ private function dataflowWorker(Console\Style\SymfonyStyle $style, string $cwd,

chdir($cwd);

$command = ['php', '-r', '--'];
$command = ['php'];

$style->note($source);

$command = implode(' ', array_map(fn ($part) => escapeshellarg($part), $command));
$style->note($command);
$process = new Process($command, $cwd);

$process = new Process(implode (' ', array_map(fn ($part) => escapeshellarg($part), $command)), $cwd);
$process->start();

$process->stdout->on('data', function ($chunk) use ($style) {
$style->text($chunk);
});
$process->stderr->on('data', function ($chunk) use ($style) {
$style->info($chunk);
});

$input->pipe($process->stdin);

return $process;
Expand All @@ -127,9 +142,11 @@ private function httpWorker(Console\Style\SymfonyStyle $style, string $cwd, stri
{
chdir($cwd);

$command = ['php', '-S', 'localhost:8000', 'main.php'];
$command = ['php', '-S', 'localhost:8000', $entrypoint];

$process = new Process(implode (' ', array_map(fn ($part) => escapeshellarg($part), $command)), $cwd);
$process = new Process(implode (' ', array_map(fn ($part) => escapeshellarg($part), $command)), $cwd.'/'.$path);

$process->start();

return $process;
}
Expand Down Expand Up @@ -174,20 +191,12 @@ private function executeWorker(
Console\Style\SymfonyStyle $style,
Process $process
): bool {
$process->stdout->on('data', function ($chunk) use ($style) {
$style->text($chunk);
});
$process->stderr->on('data', function ($chunk) use ($style) {
$style->info($chunk);
});

$deferred = new Deferred();

$process->on('exit', function () use ($deferred) {
$deferred->resolve();
});

$process->start();
$style->note(sprintf('Starting process "%s".', $process->getCommand()));

await($deferred->promise());
Expand Down
1 change: 1 addition & 0 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ private function compilePipelineJob(array $config): Satellite\Builder\Repository
'psr/log:*',
'monolog/monolog:*',
'symfony/console:^6.0',
'symfony/dotenv:^6.0',
'symfony/dependency-injection:^6.0',
);

Expand Down

0 comments on commit 9e24b6f

Please sign in to comment.