Skip to content

Commit 04cd6f7

Browse files
committed
Add PHPMemory, improve Swoole compatibility.
1 parent ef2f1d8 commit 04cd6f7

File tree

3 files changed

+553
-246
lines changed

3 files changed

+553
-246
lines changed

Diff for: src/BufferStream.php

+6-246
Original file line numberDiff line numberDiff line change
@@ -2,253 +2,13 @@
22
/**
33
* Copyright: Swlib
44
* Author: Twosee <[email protected]>
5-
* Date: 2018/1/23 下午2:23
5+
* Date: 2018/10/30 下午20:00
66
*/
77

88
namespace Swlib\Http;
99

10-
class BufferStream implements StreamInterface
11-
{
12-
public $buffer;
13-
public $pos = 0;
14-
private $writable = true;
15-
16-
public function __construct(string $data = '', ?int $length = null)
17-
{
18-
$length = $length ?? max(strlen($data), 32);
19-
$this->buffer = new \Swoole\Buffer($length);
20-
$this->write($data);
21-
}
22-
23-
public function getSize(): int
24-
{
25-
return $this->buffer->length;
26-
}
27-
28-
/**
29-
* Add data to buffer
30-
*
31-
* @param string $data
32-
*
33-
* @return $this
34-
*/
35-
public function write($data = ''): self
36-
{
37-
If ($this->writable && $data !== '') {
38-
$this->buffer->append($data);
39-
}
40-
41-
return $this;
42-
}
43-
44-
/**
45-
* Overwrite the entire buffer
46-
*
47-
* @param string $data
48-
*
49-
* @return $this
50-
*/
51-
public function overWrite(string $data = ''): self
52-
{
53-
If ($this->writable && $data !== '') {
54-
$this->buffer->clear();
55-
$this->buffer->write(0, $data);
56-
}
57-
58-
return $this;
59-
}
60-
61-
/**
62-
* Clear the buffer
63-
*
64-
* @return $this
65-
*/
66-
public function clear(): self
67-
{
68-
$this->buffer->clear();
69-
70-
return $this;
71-
}
72-
73-
/**
74-
* @return string
75-
*/
76-
public function __toString(): string
77-
{
78-
return $this->buffer->read(0, $this->buffer->length);
79-
}
80-
81-
/**
82-
* Close the buffer
83-
*/
84-
public function close(): void
85-
{
86-
$this->writable = false;
87-
}
88-
89-
/**
90-
* Clear totally
91-
*/
92-
public function __destruct()
93-
{
94-
$this->buffer->clear();
95-
$this->buffer = null;
96-
}
97-
98-
/**
99-
* Separates any underlying resources from the stream.
100-
*
101-
* After the stream has been detached, the stream is in an unusable state.
102-
*
103-
* @return resource|null Underlying PHP stream, if any
104-
*/
105-
public function detach(): void
106-
{
107-
$this->close();
108-
}
109-
110-
/**
111-
* Returns the current position of the file read/write pointer
112-
*
113-
* @return int Position of the file pointer
114-
* @throws \RuntimeException on error.
115-
*/
116-
public function tell(): int
117-
{
118-
return $this->pos;
119-
}
120-
121-
/**
122-
* Returns true if the stream is at the end of the stream.
123-
*
124-
* @return bool
125-
*/
126-
public function eof()
127-
{
128-
return $this->pos >= $this->buffer->length;
129-
}
130-
131-
/**
132-
* Returns whether or not the stream is seekable.
133-
*
134-
* @return bool
135-
*/
136-
public function isSeekable(): bool
137-
{
138-
return true;
139-
}
140-
141-
/**
142-
* Seek to a position in the stream.
143-
*
144-
* @link http://www.php.net/manual/en/function.fseek.php
145-
* @param int $offset Stream offset
146-
* @param int $whence Specifies how the cursor position will be calculated
147-
* based on the seek offset. Valid values are identical to the built-in
148-
* PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
149-
* offset bytes SEEK_CUR: Set position to current location plus offset
150-
* SEEK_END: Set position to end-of-stream plus offset.
151-
* @throws \RuntimeException on failure.
152-
*/
153-
public function seek($offset, $whence = SEEK_SET): void
154-
{
155-
$size = $this->getSize();
156-
switch ($whence) {
157-
case SEEK_CUR:
158-
$pos = $this->pos + $offset;
159-
break;
160-
case SEEK_END:
161-
$pos = $size - 1 - $offset;
162-
break;
163-
case SEEK_SET:
164-
default:
165-
$pos = $offset;
166-
}
167-
if ($pos < 0 || $pos >= $size) {
168-
throw new \RuntimeException("Wrong Offset number $offset !");
169-
}
170-
}
171-
172-
/**
173-
* Seek to the beginning of the stream.
174-
*
175-
* If the stream is not seekable, this method will raise an exception;
176-
* otherwise, it will perform a seek(0).
177-
*
178-
* @see seek()
179-
* @link http://www.php.net/manual/en/function.fseek.php
180-
* @throws \RuntimeException on failure.
181-
*/
182-
public function rewind(): void
183-
{
184-
$this->seek(0);
185-
}
186-
187-
/**
188-
* Returns whether or not the stream is writable.
189-
*
190-
* @return bool
191-
*/
192-
public function isWritable(): bool
193-
{
194-
return true;
195-
}
196-
197-
/**
198-
* Returns whether or not the stream is readable.
199-
*
200-
* @return bool
201-
*/
202-
public function isReadable(): bool
203-
{
204-
return true;
205-
}
206-
207-
/**
208-
* Read data from the stream.
209-
*
210-
* @param int $length Read up to $length bytes from the object and return
211-
* them. Fewer than $length bytes may be returned if underlying stream
212-
* call returns fewer bytes.
213-
* @return string Returns the data read from the stream, or an empty string
214-
* if no bytes are available.
215-
* @throws \RuntimeException if an error occurs.
216-
*/
217-
public function read($length): string
218-
{
219-
$pos = $this->pos;
220-
$this->pos = min($pos + $length, $this->getSize());
221-
$length = $this->pos - $pos;
222-
return $this->buffer->read($pos, $length) ?: '';
223-
}
224-
225-
/**
226-
* Returns the remaining contents in a string
227-
*
228-
* @return string
229-
* @throws \RuntimeException if unable to read or an error occurs while
230-
* reading.
231-
*/
232-
public function getContents()
233-
{
234-
throw new \BadMethodCallException('Not implement!');
235-
}
236-
237-
/**
238-
* Get stream metadata as an associative array or retrieve a specific key.
239-
*
240-
* The keys returned are identical to the keys returned from PHP's
241-
* stream_get_meta_data() function.
242-
*
243-
* @link http://php.net/manual/en/function.stream-get-meta-data.php
244-
* @param string $key Specific metadata to retrieve.
245-
* @return array|mixed|null Returns an associative array if no key is
246-
* provided. Returns a specific key value if a key is provided and the
247-
* value is found, or null if the key is not found.
248-
*/
249-
public function getMetadata($key = null)
250-
{
251-
return null;
252-
}
253-
254-
}
10+
if (version_compare(SWOOLE_VERSION, '4.2.3', '>')) {
11+
class_alias(SwooleBuffer::class, 'Swlib\\Http\\BufferStream');
12+
} else {
13+
class_alias(PHPMemory::class, 'Swlib\\Http\\BufferStream');
14+
}

0 commit comments

Comments
 (0)