2016-08-25 13:27:14 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace LanguageServer;
|
|
|
|
|
2016-08-25 15:03:29 +00:00
|
|
|
use LanguageServer\Protocol\Message;
|
2016-10-24 21:08:59 +00:00
|
|
|
use Sabre\Event\Loop;
|
2016-10-24 21:20:15 +00:00
|
|
|
use RuntimeException;
|
2016-08-25 15:03:29 +00:00
|
|
|
|
2016-08-25 13:27:14 +00:00
|
|
|
class ProtocolStreamWriter implements ProtocolWriter
|
|
|
|
{
|
|
|
|
private $output;
|
|
|
|
|
2016-10-24 21:08:59 +00:00
|
|
|
/**
|
|
|
|
* @var string $buffer
|
|
|
|
*/
|
|
|
|
private $buffer;
|
|
|
|
|
2016-08-25 13:27:14 +00:00
|
|
|
/**
|
|
|
|
* @param resource $output
|
|
|
|
*/
|
|
|
|
public function __construct($output)
|
|
|
|
{
|
|
|
|
$this->output = $output;
|
2016-10-24 21:22:41 +00:00
|
|
|
Loop\addWriteStream($this->output, function () {
|
2016-10-24 21:50:27 +00:00
|
|
|
error_clear_last();
|
2016-10-24 21:08:59 +00:00
|
|
|
$bytesWritten = @fwrite($this->output, $this->buffer);
|
2016-10-24 21:50:27 +00:00
|
|
|
if ($bytesWritten === false) {
|
|
|
|
$error = error_get_last();
|
|
|
|
if ($error !== null) {
|
|
|
|
throw new RuntimeException('Could not write message: ' . error_get_last()['message']);
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException('Could not write message');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ($bytesWritten > 0) {
|
2016-10-24 21:08:59 +00:00
|
|
|
$this->buffer = substr($this->buffer, $bytesWritten);
|
|
|
|
}
|
|
|
|
});
|
2016-08-25 13:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a Message to the client
|
|
|
|
*
|
|
|
|
* @param Message $msg
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-25 15:01:29 +00:00
|
|
|
public function write(Message $msg)
|
2016-08-25 13:27:14 +00:00
|
|
|
{
|
2016-10-24 21:08:59 +00:00
|
|
|
$this->buffer .= $msg;
|
2016-08-25 13:27:14 +00:00
|
|
|
}
|
|
|
|
}
|