1
0
Fork 0
php-language-server/src/ProtocolStreamWriter.php

48 lines
1.2 KiB
PHP
Raw Normal View History

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;
use RuntimeException;
2016-08-25 15:03:29 +00:00
2016-08-25 13:27:14 +00:00
class ProtocolStreamWriter implements ProtocolWriter
{
private $output;
/**
* @param resource $output
*/
public function __construct($output)
{
$this->output = $output;
}
/**
* Sends a Message to the client
*
* @param Message $msg
* @return void
*/
public function write(Message $msg)
2016-08-25 13:27:14 +00:00
{
$data = (string)$msg;
$msgSize = strlen($data);
$totalBytesWritten = 0;
while ($totalBytesWritten < $msgSize) {
error_clear_last();
$bytesWritten = @fwrite($this->output, substr($data, $totalBytesWritten));
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');
}
}
$totalBytesWritten += $bytesWritten;
}
2016-08-25 13:27:14 +00:00
}
}