New in Symfony 6.4: Subprocess Handler

Contributed by
Yanick Witschi

in #48485.

Consider the following Symfony console command:

use SymfonyComponentProcessProcess;
// …

class MyCommand extends Command
{
// …

protected function execute(InputInterface $input, OutputInterface $output): int
{
$subProcess = new Process([‚bin/console‘, ‚cache:pool:prune‘]);

// …
}
}

If you run this command as follows:

$ php -d memory_limit=-1 bin/console app:my-command

Which will be the memory limit of the cache:pool:prune command run from inside
app:my-command? It won’t be -1. The reason is that subprocesses in PHP
don’t inherit the configuration of their parent processes. Instead, they use the
default configuration defined in the corresponding php.ini files.

In Symfony 6.4 we’re introducing a new PhpSubprocess class to solve this problem.
Whenever you need to run a subprocess with the same PHP configuration as the parent
process, use PhpSubprocess instead of Process to run that subprocess:

use SymfonyComponentProcessPhpSubprocess;
// …

class MyCommand extends Command
{
// …

protected function execute(InputInterface $input, OutputInterface $output): int
{
// this subprocess will inherit all the config of the parent process
$subProcess = new PhpSubprocess([‚bin/console‘, ‚cache:pool:prune‘]);

// …
}
}

Sponsor the Symfony project.

Symfony Blog

Read More

Generated by Feedzy