From 9e24b6f02df03040c87a1d8c9040876b9e4b8556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Planchat?= Date: Tue, 21 Nov 2023 15:17:14 +0100 Subject: [PATCH] Migrated code from symfony/process to react/child-process --- src/Console/Command/RunCommand.php | 59 +++++++++++++++++------------- src/Service.php | 1 + 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Console/Command/RunCommand.php b/src/Console/Command/RunCommand.php index f214c0b1..dd82d70d 100644 --- a/src/Console/Command/RunCommand.php +++ b/src/Console/Command/RunCommand.php @@ -69,26 +69,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int private function dataflowWorker(Console\Style\SymfonyStyle $style, string $cwd, string $path, string $entrypoint): Process { $source =<<usePutenv(); - - if (file_exists(\$file = $cwd.'/.env')) { - \$dotenv->loadEnv(\$file); - } - if (file_exists(\$file = $cwd.'/'.{$path}.'/.env')) { - \$dotenv->loadEnv(\$file); - } + 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() @@ -96,7 +99,7 @@ private function dataflowWorker(Console\Style\SymfonyStyle $style, string $cwd, ), ); - \$satellite = include '$entrypoint'; + \$satellite = include '{$cwd}/{$path}/$entrypoint'; \$satellite(\$runtime); \$runtime->run(); @@ -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; @@ -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; } @@ -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()); diff --git a/src/Service.php b/src/Service.php index 5cf96f33..300501b3 100644 --- a/src/Service.php +++ b/src/Service.php @@ -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', );