2018-10-17 21:52:26 +02:00
|
|
|
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\LanguageServer;
|
2018-10-17 21:52:26 +02:00
|
|
|
|
|
|
|
use AdvancedJsonRpc;
|
2019-01-19 06:58:08 +01:00
|
|
|
use Amp\Promise;
|
2018-10-17 21:52:26 +02:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-10-17 21:52:26 +02:00
|
|
|
class ClientHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ProtocolReader
|
|
|
|
*/
|
|
|
|
public $protocolReader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ProtocolWriter
|
|
|
|
*/
|
|
|
|
public $protocolWriter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IdGenerator
|
|
|
|
*/
|
|
|
|
public $idGenerator;
|
|
|
|
|
|
|
|
public function __construct(ProtocolReader $protocolReader, ProtocolWriter $protocolWriter)
|
|
|
|
{
|
|
|
|
$this->protocolReader = $protocolReader;
|
|
|
|
$this->protocolWriter = $protocolWriter;
|
|
|
|
$this->idGenerator = new IdGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a request to the client and returns a promise that is resolved with the result or rejected with the error
|
|
|
|
*
|
|
|
|
* @param string $method The method to call
|
|
|
|
* @param array|object $params The method parameters
|
2019-01-19 06:58:08 +01:00
|
|
|
* @return Promise Resolved with the result of the request or rejected with an error
|
2018-10-17 21:52:26 +02:00
|
|
|
*/
|
|
|
|
public function request(string $method, $params): Promise
|
|
|
|
{
|
|
|
|
$id = $this->idGenerator->generate();
|
2019-01-19 06:58:08 +01:00
|
|
|
|
|
|
|
$promise = $this->protocolWriter->write(
|
2018-10-17 21:52:26 +02:00
|
|
|
new Message(
|
|
|
|
new AdvancedJsonRpc\Request($id, $method, (object)$params)
|
|
|
|
)
|
2019-01-19 06:58:08 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$promise->onResolve(
|
2018-10-17 21:52:26 +02:00
|
|
|
/**
|
|
|
|
* @return Promise
|
|
|
|
*/
|
|
|
|
function () use ($id) {
|
2019-01-19 06:58:08 +01:00
|
|
|
$deferred = new \Amp\Deferred();
|
|
|
|
|
2018-10-17 21:52:26 +02:00
|
|
|
$listener =
|
|
|
|
/**
|
|
|
|
* @param callable $listener
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-01-19 06:58:08 +01:00
|
|
|
function (Message $msg) use ($id, $deferred, &$listener) {
|
2018-10-17 21:52:26 +02:00
|
|
|
/**
|
|
|
|
* @psalm-suppress UndefinedPropertyFetch
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
if ($msg->body
|
|
|
|
&& AdvancedJsonRpc\Response::isResponse($msg->body)
|
|
|
|
&& $msg->body->id === $id
|
|
|
|
) {
|
|
|
|
// Received a response
|
|
|
|
$this->protocolReader->removeListener('message', $listener);
|
|
|
|
if (AdvancedJsonRpc\SuccessResponse::isSuccessResponse($msg->body)) {
|
2019-01-19 06:58:08 +01:00
|
|
|
$deferred->resolve($msg->body->result);
|
2018-10-17 21:52:26 +02:00
|
|
|
} else {
|
2019-01-19 06:58:08 +01:00
|
|
|
$deferred->fail($msg->body->error);
|
2018-10-17 21:52:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$this->protocolReader->on('message', $listener);
|
2019-01-19 06:58:08 +01:00
|
|
|
return $deferred->promise();
|
2018-10-17 21:52:26 +02:00
|
|
|
}
|
|
|
|
);
|
2019-01-19 06:58:08 +01:00
|
|
|
|
|
|
|
return $promise;
|
2018-10-17 21:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a notification to the client
|
|
|
|
*
|
|
|
|
* @param string $method The method to call
|
|
|
|
* @param array|object $params The method parameters
|
|
|
|
* @return Promise <null> Will be resolved as soon as the notification has been sent
|
|
|
|
*/
|
|
|
|
public function notify(string $method, $params): Promise
|
|
|
|
{
|
|
|
|
return $this->protocolWriter->write(
|
|
|
|
new Message(
|
|
|
|
new AdvancedJsonRpc\Notification($method, (object)$params)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|