Skip to content

Commit a7d1b27

Browse files
committed
Improve stream clear and overWrite.
1 parent 04cd6f7 commit a7d1b27

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/PHPMemory.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Swlib\Http;
99

10-
Class PHPMemory implements \Psr\Http\Message\StreamInterface
10+
Class PHPMemory implements StreamInterface
1111
{
1212

1313
private $stream;
@@ -52,7 +52,7 @@
5252
'a+' => true
5353
];
5454

55-
function __construct($resource = '', $mode = 'r+')
55+
public function __construct($resource = '', $mode = 'r+')
5656
{
5757
switch (gettype($resource)) {
5858
case 'resource':
@@ -100,8 +100,7 @@ function __construct($resource = '', $mode = 'r+')
100100
public function __toString()
101101
{
102102
try {
103-
$this->seek(0);
104-
103+
$this->rewind();
105104
return (string)stream_get_contents($this->stream);
106105
} catch (\Exception $e) {
107106
return '';
@@ -290,4 +289,20 @@ public function truncate($size = 0)
290289
return ftruncate($this->stream, $size);
291290
}
292291

292+
public function clear()
293+
{
294+
return $this->truncate(0);
295+
}
296+
297+
public function overWrite(string $data = null)
298+
{
299+
if ($data === '' || $data === null) {
300+
$this->clear();
301+
} else {
302+
$this->rewind();
303+
$this->write($data);
304+
$this->truncate(strlen($data));
305+
}
306+
}
307+
293308
}

src/StreamInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ interface StreamInterface extends \Psr\Http\Message\StreamInterface
1212

1313
public function clear();
1414

15-
public function overWrite();
15+
public function overWrite(string $data = null);
1616

17-
}
17+
}

src/SwooleBuffer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function write($data = ''): self
4848
*
4949
* @return $this
5050
*/
51-
public function overWrite(string $data = ''): self
51+
public function overWrite(string $data = null): self
5252
{
5353
If ($this->writable && $data !== '') {
5454
$this->buffer->clear();

0 commit comments

Comments
 (0)