Skip to content

Commit

Permalink
fix(Adapters/Symfony/Process) correctly close pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Dec 9, 2023
1 parent 0d70b4d commit 4f2344d
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/Adapters/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace lucatume\WPBrowser\Adapters\Symfony\Component\Process;

use Symfony\Component\Process\Exception\LogicException;
use Symfony\Component\Process\Pipes\PipesInterface;
use Symfony\Component\Process\Process as SymfonyProcess;

class Process extends SymfonyProcess
{
/**
* @var array<string,mixed>
* @var bool
*/
private $options = [];
private $createNewConsole = false;

/**
* @param string[] $command
* @param array<string,mixed>|null $env
Expand Down Expand Up @@ -51,28 +53,31 @@ public function getStartTime(): float
return $startTime;
}

/**
* @param string $name
* @param array<mixed> $arguments
* @return void
*/
public function __call(string $name, array $arguments)
public function __destruct()
{
if ($name === 'setOptions') {
$this->options = $arguments[0] ?? [];
if ($this->createNewConsole) {
$processPipesProperty = new \ReflectionProperty(SymfonyProcess::class, 'processPipes');
$processPipesProperty->setAccessible(true);
/** @var PipesInterface $processPipes */
$processPipes = $processPipesProperty->getValue($this);
$processPipes->close();

return;
}
return;

$this->stop(0);
}

public function __destruct()
public function createNewConsole(): void
{
if (($this->options['create_new_console'] ?? false) || method_exists($this, 'setOptions')) {
parent::__destruct();
return;
}
$this->createNewConsole = true;

$closeMethodReflection = new \ReflectionMethod(SymfonyProcess::class, 'close');
$closeMethodReflection->setAccessible(true);
$closeMethodReflection->invoke($this);
$optionsReflectionProperty = new \ReflectionProperty(SymfonyProcess::class, 'options');
$optionsReflectionProperty->setAccessible(true);
$options = $optionsReflectionProperty->getValue($this);
$options = is_array($options) ? $options : [];
$options['create_new_console'] = true;
$options['bypass_shell'] = true;
$optionsReflectionProperty->setValue($this, $options);
}
}

0 comments on commit 4f2344d

Please sign in to comment.