This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
WriteBufferStream.php
84 lines (66 loc) · 2.15 KB
/
WriteBufferStream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace Pydio\Access\Core\Stream;
use GuzzleHttp\Stream\Stream as GuzzleStream;
use GuzzleHttp\Stream\StreamDecoratorTrait;
use GuzzleHttp\Stream\StreamInterface;
use Pydio\Access\Core\Model\AJXP_Node;
/**
* Provides a buffer stream that can be written to to fill a buffer, and read
* from to remove bytes from the buffer.
*
* This stream returns a "hwm" metadata value that tells upstream consumers
* what the configured high water mark of the stream is, or the maximum
* preferred size of the buffer.
*
* @package GuzzleHttp\Stream
*/
class WriteBufferStream implements StreamInterface {
use StreamDecoratorTrait;
/** @var StreamInterface $bufferStream */
private $bufferStream;
private $size;
private $hwm;
/**
* @param StreamInterface $stream
* @param AJXP_Node $node
* @param int $hwm High water mark, representing the preferred maximum
* buffer size. If the size of the buffer exceeds the high
* water mark, then calls to write will continue to succeed
* but will return false to inform writers to slow down
* until the buffer has been drained by reading from it.
*/
public function __construct(
StreamInterface $stream,
AJXP_Node $node,
$hwm = -1) {
$this->bufferStream = new GuzzleStream(fopen('php://temp', 'w+'));
$this->stream = $stream;
$this->hwm = $hwm;
}
/**
* @return string
*/
public function getContents() {
return $this->stream->getContents();
}
public function close() {
$this->stream->write($this->bufferStream);
$this->size = 0;
return $this->stream->close();
}
/**
* Writes data to the buffer.
* @param string $string
* @return bool|int
*/
public function write($string)
{
$this->size += $this->bufferStream->write($string);
if ($this->hwm > 0 && $this->size >= $this->hwm) {
$this->stream->write($this->bufferStream->getContents());
$this->size = 0;
return false;
}
return strlen($string);
}
}