Skip to content
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

[bugfix] Make possible to create sync tcp-connection #27

Open
wants to merge 1 commit into
base: 6.1
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions Dumper/ServerDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ class ServerDumper implements DataDumperInterface
* @param string $host The server host
* @param DataDumperInterface|null $wrappedDumper A wrapped instance used whenever we failed contacting the server
* @param ContextProviderInterface[] $contextProviders Context providers indexed by context name
* @param bool $asyncClient Create async connection to TCP-server
*/
public function __construct(string $host, DataDumperInterface $wrappedDumper = null, array $contextProviders = [])
public function __construct(string $host, DataDumperInterface $wrappedDumper = null, array $contextProviders = [], bool $asyncClient = true)
{
$this->connection = new Connection($host, $contextProviders);
$this->connection = new Connection($host, $contextProviders, $asyncClient);
$this->wrappedDumper = $wrappedDumper;
}

Expand Down
11 changes: 9 additions & 2 deletions Server/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ class Connection
*/
private $socket;

/**
* @var bool Make connection async
*/
private bool $asyncClient;

/**
* @param string $host The server host
* @param ContextProviderInterface[] $contextProviders Context providers indexed by context name
* @param bool $asyncClient Make connection async
*/
public function __construct(string $host, array $contextProviders = [])
public function __construct(string $host, array $contextProviders = [], bool $asyncClient = true)
{
if (!str_contains($host, '://')) {
$host = 'tcp://'.$host;
}

$this->host = $host;
$this->asyncClient = $asyncClient;
$this->contextProviders = $contextProviders;
}

Expand Down Expand Up @@ -91,7 +98,7 @@ private function createSocket()
{
set_error_handler([self::class, 'nullErrorHandler']);
try {
return stream_socket_client($this->host, $errno, $errstr, 3, \STREAM_CLIENT_CONNECT | \STREAM_CLIENT_ASYNC_CONNECT);
return stream_socket_client($this->host, $errno, $errstr, 3, $this->asyncClient ? \STREAM_CLIENT_CONNECT | \STREAM_CLIENT_ASYNC_CONNECT : \STREAM_CLIENT_CONNECT);
} finally {
restore_error_handler();
}
Expand Down
2 changes: 1 addition & 1 deletion VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static function register(): void
case $format && 'tcp' === parse_url($format, \PHP_URL_SCHEME):
$host = 'server' === $format ? $_SERVER['VAR_DUMPER_SERVER'] ?? '127.0.0.1:9912' : $format;
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
$dumper = new ServerDumper($host, $dumper, self::getDefaultContextProviders());
$dumper = new ServerDumper($host, $dumper, self::getDefaultContextProviders(), $_SERVER['VAR_DUMPER_ASYNC'] ?? true);
break;
default:
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
Expand Down