Skip to content

Commit

Permalink
Merge pull request #680 from lucatume/v4-proc-close-fix
Browse files Browse the repository at this point in the history
fix(Adapters/Process) back-compat for create_new_console
  • Loading branch information
lucatume authored Dec 9, 2023
2 parents 0b5e549 + 7e85e13 commit 4edec26
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 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 @@ -50,28 +52,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);
}
}
2 changes: 1 addition & 1 deletion src/ManagedProcess/ChromeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function doStart(): void
{
$command = [$this->chromeDriverBinary, '--port=' . $this->port, ...$this->arguments];
$process = new Process($command);
$process->setOptions(['create_new_console' => true]);
$process->createNewConsole();
$process->start();
$this->confirmStart($process);
$this->pid = $process->getPid();
Expand Down
2 changes: 1 addition & 1 deletion src/ManagedProcess/PhpBuiltInServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function doStart(): void
$this->docRoot,
$this->env
);
$process->setOptions(['create_new_console' => true]);
$process->createNewConsole();
$process->start();
if (!$this->confirmServerRunningOnPort($process)) {
$error = new RuntimeException(
Expand Down

0 comments on commit 4edec26

Please sign in to comment.