Skip to content

Improve typing for process callback #60679

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Symfony/Component/Process/PhpProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static function fromShellCommandline(string $command, ?string $cwd = null
throw new LogicException(\sprintf('The "%s()" method cannot be called when using "%s".', __METHOD__, self::class));
}

/**
* @param (callable('out'|'err', string):void)|null $callback
*/
public function start(?callable $callback = null, array $env = []): void
{
if (null === $this->getCommandLine()) {
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Process/PhpSubprocess.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public static function fromShellCommandline(string $command, ?string $cwd = null
throw new LogicException(\sprintf('The "%s()" method cannot be called when using "%s".', __METHOD__, self::class));
}

/**
* @param (callable('out'|'err', string):void)|null $callback
*/
public function start(?callable $callback = null, array $env = []): void
{
if (null === $this->getCommandLine()) {
Expand Down
41 changes: 25 additions & 16 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ 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)|null
*/
private ?\Closure $callback = null;
private array|string $commandline;
private ?string $cwd;
Expand Down Expand Up @@ -231,8 +234,8 @@ public function __clone()
* The STDOUT and STDERR are also available after the process is finished
* via the getOutput() and getErrorOutput() methods.
*
* @param callable|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @return int The exit status code
*
Expand All @@ -257,6 +260,9 @@ public function run(?callable $callback = null, array $env = []): int
* This is identical to run() except that an exception is thrown if the process
* exits with a non-zero exit code.
*
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @return $this
*
* @throws ProcessFailedException if the process didn't terminate successfully
Expand Down Expand Up @@ -284,8 +290,8 @@ public function mustRun(?callable $callback = null, array $env = []): static
* the output in real-time while writing the standard input to the process.
* It allows to have feedback from the independent process during execution.
*
* @param callable|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @throws ProcessStartFailedException When process can't be launched
* @throws RuntimeException When process is already running
Expand Down Expand Up @@ -395,8 +401,8 @@ public function start(?callable $callback = null, array $env = []): void
*
* Be warned that the process is cloned before being started.
*
* @param callable|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @throws ProcessStartFailedException When process can't be launched
* @throws RuntimeException When process is already running
Expand Down Expand Up @@ -424,7 +430,8 @@ public function restart(?callable $callback = null, array $env = []): static
* from the output in real-time while writing the standard input to the process.
* It allows to have feedback from the independent process during execution.
*
* @param callable|null $callback A valid PHP callback
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @return int The exitcode of the process
*
Expand Down Expand Up @@ -471,6 +478,9 @@ public function wait(?callable $callback = null): int
* from the output in real-time while writing the standard input to the process.
* It allows to have feedback from the independent process during execution.
*
* @param (callable('out'|'err', string):bool)|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @throws RuntimeException When process timed out
* @throws LogicException When process is not yet started
* @throws ProcessTimedOutException In case the timeout was reached
Expand Down Expand Up @@ -1291,22 +1301,21 @@ private function getDescriptors(bool $hasCallback): array
* The callbacks adds all occurred output to the specific buffer and calls
* the user callback (if present) with the received output.
*
* @param callable|null $callback The user defined PHP callback
* @param callable('out'|'err', string)|null $callback
*
* @return \Closure('out'|'err', string):bool
*/
protected function buildCallback(?callable $callback = null): \Closure
{
if ($this->outputDisabled) {
return fn ($type, $data): bool => null !== $callback && $callback($type, $data);
}

$out = self::OUT;

return function ($type, $data) use ($callback, $out): bool {
if ($out == $type) {
$this->addOutput($data);
} else {
$this->addErrorOutput($data);
}
return function ($type, $data) use ($callback): bool {
match ($type) {
self::OUT => $this->addOutput($data),
self::ERR => $this->addErrorOutput($data),
};

return null !== $callback && $callback($type, $data);
};
Expand Down
Loading