1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-11-30 04:19:23 +01:00

Use \fclose for unidirectional resources to cover pipes

This commit is contained in:
Niklas Keller 2017-06-18 22:57:19 +02:00
parent 93540890fd
commit 798723e942

View File

@ -27,6 +27,9 @@ final class ResourceOutputStream implements OutputStream {
/** @var int|null */
private $chunkSize;
/** @var bool Flag to avoid \fclose() inside destructor */
private $inDestructor = false;
/**
* @param resource $stream Stream resource.
* @param int|null $chunkSize Chunk size per `fwrite()` operation.
@ -203,8 +206,14 @@ final class ResourceOutputStream implements OutputStream {
* @return void
*/
public function close() {
if ($this->resource) {
\stream_socket_shutdown($this->resource, \STREAM_SHUT_WR);
if ($this->resource && !$this->inDestructor) {
$meta = \stream_get_meta_data($this->resource);
if (strpos($meta["mode"], "+") !== false) {
\stream_socket_shutdown($this->resource, \STREAM_SHUT_WR);
} else {
\fclose($this->resource);
}
}
$this->resource = null;
@ -230,6 +239,8 @@ final class ResourceOutputStream implements OutputStream {
}
public function __destruct() {
$this->inDestructor = true;
if ($this->resource !== null) {
$this->close();
}