public createClient ( string | null $target = null ) : React\Promise\PromiseInterface | ||
$target | string | null | |
return | React\Promise\PromiseInterface | resolves with Client or rejects with \Exception |
public function createClient($target = null)
{
try {
$parts = $this->parseUrl($target);
} catch (InvalidArgumentException $e) {
return Promise\reject($e);
}
$protocol = $this->protocol;
$promise = $this->connector->create($parts['host'], $parts['port'])->then(function (Stream $stream) use($protocol) {
return new StreamingClient($stream, $protocol->createResponseParser(), $protocol->createSerializer());
});
if (isset($parts['auth'])) {
$promise = $promise->then(function (StreamingClient $client) use($parts) {
return $client->auth($parts['auth'])->then(function () use($client) {
return $client;
}, function ($error) use($client) {
$client->close();
throw $error;
});
});
}
if (isset($parts['db'])) {
$promise = $promise->then(function (StreamingClient $client) use($parts) {
return $client->select($parts['db'])->then(function () use($client) {
return $client;
}, function ($error) use($client) {
$client->close();
throw $error;
});
});
}
return $promise;
}
/** * Connect to server and retrun promise with client. * * @return \React\Promise\PromiseInterface */ public function createClient() { return $this->factory->createClient($this->savePath)->then(function ($client) { $this->server->log('Connected with success to Redis server.'); return $client; }, function ($e) { $this->server->log('Application got an error on try to connect with Redis server: %s.', [$e->getMessage()]); throw $e; }); }