[Process] Improve process typing

This commit is contained in:
Jack Worman
2025-11-07 19:55:39 -05:00
committed by Nicolas Grekas
parent 17ad335298
commit 74244a4844
3 changed files with 47 additions and 19 deletions

View File

@@ -22,15 +22,17 @@ use Symfony\Component\Process\Exception\RuntimeException;
* print $p->getOutput()."\n";
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @psalm-import-type EnvArray from Process
*/
class PhpProcess extends Process
{
/**
* @param string $script The PHP script to run (as a string)
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process
* @param array|null $env The environment variables or null to use the same environment as the current PHP process
* @param int $timeout The timeout in seconds
* @param array|null $php Path to the PHP binary to use with any additional arguments
* @param string $script The PHP script to run (as a string)
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process
* @param EnvArray|null $env The environment variables or null to use the same environment as the current PHP process
* @param int $timeout The timeout in seconds
* @param array|null $php Path to the PHP binary to use with any additional arguments
*/
public function __construct(string $script, ?string $cwd = null, ?array $env = null, int $timeout = 60, ?array $php = null)
{
@@ -57,6 +59,7 @@ class PhpProcess extends Process
/**
* @param (callable('out'|'err', string):void)|null $callback
* @param EnvArray $env
*/
public function start(?callable $callback = null, array $env = []): void
{

View File

@@ -40,16 +40,18 @@ use Symfony\Component\Process\Exception\RuntimeException;
*
* @author Yanick Witschi <yanick.witschi@terminal42.ch>
* @author Partially copied and heavily inspired from composer/xdebug-handler by John Stevenson <john-stevenson@blueyonder.co.uk>
*
* @psalm-import-type EnvArray from Process
*/
class PhpSubprocess extends Process
{
/**
* @param array $command The command to run and its arguments listed as separate entries. They will automatically
* get prefixed with the PHP binary
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process
* @param array|null $env The environment variables or null to use the same environment as the current PHP process
* @param int $timeout The timeout in seconds
* @param array|null $php Path to the PHP binary to use with any additional arguments
* @param array $command The command to run and its arguments listed as separate entries. They will automatically
* get prefixed with the PHP binary
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process
* @param EnvArray|null $env The environment variables or null to use the same environment as the current PHP process
* @param int $timeout The timeout in seconds
* @param array|null $php Path to the PHP binary to use with any additional arguments
*/
public function __construct(array $command, ?string $cwd = null, ?array $env = null, int $timeout = 60, ?array $php = null)
{

View File

@@ -29,6 +29,8 @@ use Symfony\Component\Process\Pipes\WindowsPipes;
* @author Romain Neutron <imprec@gmail.com>
*
* @implements \IteratorAggregate<string, string>
*
* @psalm-type EnvArray = array<string, array<string|\Stringable|false>>
*/
class Process implements \IteratorAggregate
{
@@ -51,12 +53,12 @@ class Process implements \IteratorAggregate
public const ITER_SKIP_OUT = 4; // Use this flag to skip STDOUT while iterating
public const ITER_SKIP_ERR = 8; // Use this flag to skip STDERR while iterating
/**
* @var \Closure('out'|'err', string):bool|null
*/
/** @var \Closure('out'|'err', string):bool|null */
private ?\Closure $callback = null;
/** @var list<string>|string */
private array|string $commandline;
private ?string $cwd;
/** @var EnvArray */
private array $env = [];
/** @var resource|string|\Iterator|null */
private $input;
@@ -138,9 +140,9 @@ class Process implements \IteratorAggregate
];
/**
* @param array $command The command to run and its arguments listed as separate entries
* @param list<string> $command The command to run and its arguments listed as separate entries
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process
* @param array|null $env The environment variables or null to use the same environment as the current PHP process
* @param EnvArray|null $env The environment variables or null to use the same environment as the current PHP process
* @param mixed $input The input as stream resource, scalar or \Traversable, or null for no input
* @param int|float|null $timeout The timeout in seconds or null to disable
*
@@ -186,7 +188,7 @@ class Process implements \IteratorAggregate
*
* @param string $command The command line to pass to the shell of the OS
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process
* @param array|null $env The environment variables or null to use the same environment as the current PHP process
* @param EnvArray|null $env The environment variables or null to use the same environment as the current PHP process
* @param mixed $input The input as stream resource, scalar or \Traversable, or null for no input
* @param int|float|null $timeout The timeout in seconds or null to disable
*
@@ -236,6 +238,7 @@ class Process implements \IteratorAggregate
*
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
* @param EnvArray $env
*
* @return int The exit status code
*
@@ -262,6 +265,7 @@ class Process implements \IteratorAggregate
*
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
* @param EnvArray $env
*
* @return $this
*
@@ -292,6 +296,7 @@ class Process implements \IteratorAggregate
*
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
* @param EnvArray $env
*
* @throws ProcessStartFailedException When process can't be launched
* @throws RuntimeException When process is already running
@@ -403,6 +408,7 @@ class Process implements \IteratorAggregate
*
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
* @param EnvArray $env
*
* @throws ProcessStartFailedException When process can't be launched
* @throws RuntimeException When process is already running
@@ -1119,6 +1125,8 @@ class Process implements \IteratorAggregate
/**
* Gets the environment variables.
*
* @psalm-return EnvArray
*/
public function getEnv(): array
{
@@ -1128,7 +1136,7 @@ class Process implements \IteratorAggregate
/**
* Sets the environment variables.
*
* @param array<string|\Stringable> $env The new environment variables
* @param EnvArray $env The new environment variables
*
* @return $this
*/
@@ -1301,7 +1309,7 @@ class Process implements \IteratorAggregate
* The callbacks adds all occurred output to the specific buffer and calls
* the user callback (if present) with the received output.
*
* @param callable('out'|'err', string)|null $callback
* @param (callable('out'|'err', string):void)|null $callback
*
* @return \Closure('out'|'err', string):bool
*/
@@ -1535,6 +1543,9 @@ class Process implements \IteratorAggregate
return true;
}
/**
* @param string|list<string> $commandline
*/
private function buildShellCommandline(string|array $commandline): string
{
if (\is_string($commandline)) {
@@ -1550,6 +1561,12 @@ class Process implements \IteratorAggregate
return implode(' ', array_map($this->escapeArgument(...), $commandline));
}
/**
* @param string|list<string> $cmd
* @param EnvArray $env
*
* @param-out EnvArray $env
*/
private function prepareWindowsCommandLine(string|array $cmd, array &$env): string
{
$cmd = $this->buildShellCommandline($cmd);
@@ -1650,6 +1667,9 @@ class Process implements \IteratorAggregate
return '"'.str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument).'"';
}
/**
* @param EnvArray $env
*/
private function replacePlaceholders(string $commandline, array $env): string
{
return preg_replace_callback('/"\$\{:([_a-zA-Z]++[_a-zA-Z0-9]*+)\}"/', function ($matches) use ($commandline, $env) {
@@ -1661,6 +1681,9 @@ class Process implements \IteratorAggregate
}, $commandline);
}
/**
* @return EnvArray
*/
private function getDefaultEnv(): array
{
$env = getenv();