@@ -13,83 +13,83 @@ |
||
13 | 13 | |
14 | 14 | class ServerRequest extends Request implements ServerRequestInterface, ServerMessageInterface |
15 | 15 | { |
16 | - use ServerMessage; |
|
17 | - |
|
18 | - protected array $cookieParams = []; |
|
19 | - |
|
20 | - /** @var array|object|null */ |
|
21 | - protected $parsedBody; |
|
22 | - protected array $queryParams = []; |
|
23 | - protected array $serverParams; |
|
24 | - |
|
25 | - /** @var UploadedFileInterface[] */ |
|
26 | - protected array $uploadedFiles = []; |
|
27 | - |
|
28 | - |
|
29 | - public function __construct( |
|
30 | - string $method, |
|
31 | - UriInterface $uri, |
|
32 | - array $headers = [], |
|
33 | - $body = null, |
|
34 | - string $version = '1.1', |
|
35 | - array $serverParams = [] |
|
36 | - ) { |
|
37 | - parent::__construct($method, $uri, $headers, $body, $version); |
|
38 | - |
|
39 | - $this->serverParams = $serverParams; |
|
40 | - parse_str($uri->getQuery(), $this->queryParams); |
|
41 | - } |
|
42 | - |
|
43 | - public function getServerParams(): array |
|
44 | - { |
|
45 | - return $this->serverParams; |
|
46 | - } |
|
47 | - |
|
48 | - public function getUploadedFiles(): array |
|
49 | - { |
|
50 | - return $this->uploadedFiles; |
|
51 | - } |
|
52 | - |
|
53 | - public function withUploadedFiles(array $uploadedFiles) |
|
54 | - { |
|
55 | - $this->uploadedFiles = $uploadedFiles; |
|
56 | - return $this; |
|
57 | - } |
|
58 | - |
|
59 | - public function getCookieParams(): array |
|
60 | - { |
|
61 | - return $this->cookieParams; |
|
62 | - } |
|
63 | - |
|
64 | - public function withCookieParams(array $cookies) |
|
65 | - { |
|
66 | - $this->cookieParams = $cookies; |
|
67 | - return $this; |
|
68 | - } |
|
69 | - |
|
70 | - public function getQueryParams(): array |
|
71 | - { |
|
72 | - return $this->queryParams; |
|
73 | - } |
|
74 | - |
|
75 | - public function withQueryParams(array $query) |
|
76 | - { |
|
77 | - $this->queryParams = $query; |
|
78 | - return $this; |
|
79 | - } |
|
80 | - |
|
81 | - public function getParsedBody() |
|
82 | - { |
|
83 | - return $this->parsedBody; |
|
84 | - } |
|
85 | - |
|
86 | - public function withParsedBody($data) |
|
87 | - { |
|
88 | - if (!is_array($data) && !is_object($data) && null !== $data) { |
|
89 | - throw new InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null'); |
|
90 | - } |
|
91 | - |
|
92 | - $this->parsedBody = $data; |
|
93 | - return $this; |
|
94 | - } |
|
16 | + use ServerMessage; |
|
17 | + |
|
18 | + protected array $cookieParams = []; |
|
19 | + |
|
20 | + /** @var array|object|null */ |
|
21 | + protected $parsedBody; |
|
22 | + protected array $queryParams = []; |
|
23 | + protected array $serverParams; |
|
24 | + |
|
25 | + /** @var UploadedFileInterface[] */ |
|
26 | + protected array $uploadedFiles = []; |
|
27 | + |
|
28 | + |
|
29 | + public function __construct( |
|
30 | + string $method, |
|
31 | + UriInterface $uri, |
|
32 | + array $headers = [], |
|
33 | + $body = null, |
|
34 | + string $version = '1.1', |
|
35 | + array $serverParams = [] |
|
36 | + ) { |
|
37 | + parent::__construct($method, $uri, $headers, $body, $version); |
|
38 | + |
|
39 | + $this->serverParams = $serverParams; |
|
40 | + parse_str($uri->getQuery(), $this->queryParams); |
|
41 | + } |
|
42 | + |
|
43 | + public function getServerParams(): array |
|
44 | + { |
|
45 | + return $this->serverParams; |
|
46 | + } |
|
47 | + |
|
48 | + public function getUploadedFiles(): array |
|
49 | + { |
|
50 | + return $this->uploadedFiles; |
|
51 | + } |
|
52 | + |
|
53 | + public function withUploadedFiles(array $uploadedFiles) |
|
54 | + { |
|
55 | + $this->uploadedFiles = $uploadedFiles; |
|
56 | + return $this; |
|
57 | + } |
|
58 | + |
|
59 | + public function getCookieParams(): array |
|
60 | + { |
|
61 | + return $this->cookieParams; |
|
62 | + } |
|
63 | + |
|
64 | + public function withCookieParams(array $cookies) |
|
65 | + { |
|
66 | + $this->cookieParams = $cookies; |
|
67 | + return $this; |
|
68 | + } |
|
69 | + |
|
70 | + public function getQueryParams(): array |
|
71 | + { |
|
72 | + return $this->queryParams; |
|
73 | + } |
|
74 | + |
|
75 | + public function withQueryParams(array $query) |
|
76 | + { |
|
77 | + $this->queryParams = $query; |
|
78 | + return $this; |
|
79 | + } |
|
80 | + |
|
81 | + public function getParsedBody() |
|
82 | + { |
|
83 | + return $this->parsedBody; |
|
84 | + } |
|
85 | + |
|
86 | + public function withParsedBody($data) |
|
87 | + { |
|
88 | + if (!is_array($data) && !is_object($data) && null !== $data) { |
|
89 | + throw new InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null'); |
|
90 | + } |
|
91 | + |
|
92 | + $this->parsedBody = $data; |
|
93 | + return $this; |
|
94 | + } |
|
95 | 95 | } |
@@ -14,131 +14,131 @@ |
||
14 | 14 | class Message implements MessageInterface |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * RFC-2616, RFC-7230 - case-insensitive; HTTP2 pack convert all header to lowercase |
|
19 | - * |
|
20 | - * @var array - all header name will be convert to lowercase |
|
21 | - */ |
|
22 | - protected array $headers = []; |
|
23 | - protected string $protocol = '1.1'; |
|
24 | - protected ?StreamInterface $stream = null; |
|
25 | - |
|
26 | - public function getProtocolVersion(): string |
|
27 | - { |
|
28 | - return $this->protocol; |
|
29 | - } |
|
30 | - |
|
31 | - public function withProtocolVersion($version): self |
|
32 | - { |
|
33 | - if ($this->protocol !== $version) { |
|
34 | - $this->protocol = $version; |
|
35 | - } |
|
36 | - |
|
37 | - return $this; |
|
38 | - } |
|
39 | - |
|
40 | - public function getHeaders(): array |
|
41 | - { |
|
42 | - return $this->headers; |
|
43 | - } |
|
44 | - |
|
45 | - public function hasHeader($name): bool |
|
46 | - { |
|
47 | - $name = strtolower($name); |
|
48 | - $hasHeader = $this->headers[$name] ?? null; |
|
49 | - return $hasHeader !== null; |
|
50 | - } |
|
51 | - |
|
52 | - public function getHeader($name): array |
|
53 | - { |
|
54 | - $name = strtolower($name); |
|
55 | - return $this->headers[$name] ?? []; |
|
56 | - } |
|
57 | - |
|
58 | - public function getHeaderLine($name): string |
|
59 | - { |
|
60 | - return implode(', ', $this->getHeader($name)); |
|
61 | - } |
|
62 | - |
|
63 | - public function withHeader($name, $value): self |
|
64 | - { |
|
65 | - $name = strtolower($name); |
|
66 | - $this->headers[$name] = $this->validateAndTrimHeader($name, (array)$value); |
|
67 | - return $this; |
|
68 | - } |
|
69 | - |
|
70 | - public function withoutHeader($name): self |
|
71 | - { |
|
72 | - $name = strtolower($name); |
|
73 | - unset($this->headers[$name]); |
|
74 | - return $this; |
|
75 | - } |
|
76 | - |
|
77 | - public function getBody(): StreamInterface |
|
78 | - { |
|
79 | - if (!$this->stream) { |
|
80 | - $this->stream = Stream::create(''); |
|
81 | - } |
|
82 | - |
|
83 | - return $this->stream; |
|
84 | - } |
|
85 | - |
|
86 | - public function withBody(StreamInterface $body): self |
|
87 | - { |
|
88 | - $this->stream = $body; |
|
89 | - return $this; |
|
90 | - } |
|
91 | - |
|
92 | - public function reset(): self |
|
93 | - { |
|
94 | - $this->headers = []; |
|
95 | - if ($this->stream) { |
|
96 | - $this->stream->close(); |
|
97 | - $this->stream = null; |
|
98 | - } |
|
99 | - |
|
100 | - return $this; |
|
101 | - } |
|
102 | - |
|
103 | - protected function validateAndTrimHeader(string $name, array $values): array |
|
104 | - { |
|
105 | - if (preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $name) !== 1) { |
|
106 | - throw new InvalidArgumentException('Header name must be an RFC 7230 compatible string.'); |
|
107 | - } |
|
108 | - |
|
109 | - $returnValues = []; |
|
110 | - foreach ($values as $v) { |
|
111 | - if ((!is_numeric($v) && !is_string($v)) || 1 !== preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string)$v)) { |
|
112 | - throw new InvalidArgumentException('Header values must be RFC 7230 compatible strings.'); |
|
113 | - } |
|
114 | - |
|
115 | - $returnValues[] = trim((string)$v); |
|
116 | - } |
|
117 | - |
|
118 | - return $returnValues; |
|
119 | - } |
|
120 | - |
|
121 | - public function withAddedHeader($name, $value): self |
|
122 | - { |
|
123 | - $this->setHeaders([$name => $value]); |
|
124 | - return $this; |
|
125 | - } |
|
126 | - |
|
127 | - protected function setHeaders(array $headers): void |
|
128 | - { |
|
129 | - foreach ($headers as $name => $values) { |
|
130 | - $values = (array)$values; |
|
131 | - $name = strtolower((string)$name); |
|
132 | - |
|
133 | - if (!$this->hasHeader($name)) { |
|
134 | - $this->headers[$name] = []; |
|
135 | - } |
|
136 | - |
|
137 | - $values = $this->validateAndTrimHeader($name, $values); |
|
138 | - |
|
139 | - foreach ($values as $value) { |
|
140 | - $this->headers[$name][] = $value; |
|
141 | - } |
|
142 | - } |
|
143 | - } |
|
17 | + /** |
|
18 | + * RFC-2616, RFC-7230 - case-insensitive; HTTP2 pack convert all header to lowercase |
|
19 | + * |
|
20 | + * @var array - all header name will be convert to lowercase |
|
21 | + */ |
|
22 | + protected array $headers = []; |
|
23 | + protected string $protocol = '1.1'; |
|
24 | + protected ?StreamInterface $stream = null; |
|
25 | + |
|
26 | + public function getProtocolVersion(): string |
|
27 | + { |
|
28 | + return $this->protocol; |
|
29 | + } |
|
30 | + |
|
31 | + public function withProtocolVersion($version): self |
|
32 | + { |
|
33 | + if ($this->protocol !== $version) { |
|
34 | + $this->protocol = $version; |
|
35 | + } |
|
36 | + |
|
37 | + return $this; |
|
38 | + } |
|
39 | + |
|
40 | + public function getHeaders(): array |
|
41 | + { |
|
42 | + return $this->headers; |
|
43 | + } |
|
44 | + |
|
45 | + public function hasHeader($name): bool |
|
46 | + { |
|
47 | + $name = strtolower($name); |
|
48 | + $hasHeader = $this->headers[$name] ?? null; |
|
49 | + return $hasHeader !== null; |
|
50 | + } |
|
51 | + |
|
52 | + public function getHeader($name): array |
|
53 | + { |
|
54 | + $name = strtolower($name); |
|
55 | + return $this->headers[$name] ?? []; |
|
56 | + } |
|
57 | + |
|
58 | + public function getHeaderLine($name): string |
|
59 | + { |
|
60 | + return implode(', ', $this->getHeader($name)); |
|
61 | + } |
|
62 | + |
|
63 | + public function withHeader($name, $value): self |
|
64 | + { |
|
65 | + $name = strtolower($name); |
|
66 | + $this->headers[$name] = $this->validateAndTrimHeader($name, (array)$value); |
|
67 | + return $this; |
|
68 | + } |
|
69 | + |
|
70 | + public function withoutHeader($name): self |
|
71 | + { |
|
72 | + $name = strtolower($name); |
|
73 | + unset($this->headers[$name]); |
|
74 | + return $this; |
|
75 | + } |
|
76 | + |
|
77 | + public function getBody(): StreamInterface |
|
78 | + { |
|
79 | + if (!$this->stream) { |
|
80 | + $this->stream = Stream::create(''); |
|
81 | + } |
|
82 | + |
|
83 | + return $this->stream; |
|
84 | + } |
|
85 | + |
|
86 | + public function withBody(StreamInterface $body): self |
|
87 | + { |
|
88 | + $this->stream = $body; |
|
89 | + return $this; |
|
90 | + } |
|
91 | + |
|
92 | + public function reset(): self |
|
93 | + { |
|
94 | + $this->headers = []; |
|
95 | + if ($this->stream) { |
|
96 | + $this->stream->close(); |
|
97 | + $this->stream = null; |
|
98 | + } |
|
99 | + |
|
100 | + return $this; |
|
101 | + } |
|
102 | + |
|
103 | + protected function validateAndTrimHeader(string $name, array $values): array |
|
104 | + { |
|
105 | + if (preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $name) !== 1) { |
|
106 | + throw new InvalidArgumentException('Header name must be an RFC 7230 compatible string.'); |
|
107 | + } |
|
108 | + |
|
109 | + $returnValues = []; |
|
110 | + foreach ($values as $v) { |
|
111 | + if ((!is_numeric($v) && !is_string($v)) || 1 !== preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string)$v)) { |
|
112 | + throw new InvalidArgumentException('Header values must be RFC 7230 compatible strings.'); |
|
113 | + } |
|
114 | + |
|
115 | + $returnValues[] = trim((string)$v); |
|
116 | + } |
|
117 | + |
|
118 | + return $returnValues; |
|
119 | + } |
|
120 | + |
|
121 | + public function withAddedHeader($name, $value): self |
|
122 | + { |
|
123 | + $this->setHeaders([$name => $value]); |
|
124 | + return $this; |
|
125 | + } |
|
126 | + |
|
127 | + protected function setHeaders(array $headers): void |
|
128 | + { |
|
129 | + foreach ($headers as $name => $values) { |
|
130 | + $values = (array)$values; |
|
131 | + $name = strtolower((string)$name); |
|
132 | + |
|
133 | + if (!$this->hasHeader($name)) { |
|
134 | + $this->headers[$name] = []; |
|
135 | + } |
|
136 | + |
|
137 | + $values = $this->validateAndTrimHeader($name, $values); |
|
138 | + |
|
139 | + foreach ($values as $value) { |
|
140 | + $this->headers[$name][] = $value; |
|
141 | + } |
|
142 | + } |
|
143 | + } |
|
144 | 144 | } |
@@ -23,227 +23,227 @@ |
||
23 | 23 | |
24 | 24 | class Stream implements StreamInterface |
25 | 25 | { |
26 | - /** @var resource|null A resource reference */ |
|
27 | - protected $stream = null; |
|
28 | - protected bool $seekable = false; |
|
29 | - protected bool $readable = false; |
|
30 | - protected bool $writable = false; |
|
31 | - /** @var array|mixed|void|null */ |
|
32 | - protected $uri; |
|
33 | - /** @var int|null */ |
|
34 | - protected ?int $size = null; |
|
35 | - |
|
36 | - protected const READ_WRITE_HASH = [ |
|
37 | - 'read' => [ |
|
38 | - 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true, |
|
39 | - 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, |
|
40 | - 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true, |
|
41 | - 'x+t' => true, 'c+t' => true, 'a+' => true, |
|
42 | - ], |
|
43 | - 'write' => [ |
|
44 | - 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true, |
|
45 | - 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true, |
|
46 | - 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true, |
|
47 | - 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true, |
|
48 | - ], |
|
49 | - ]; |
|
50 | - |
|
51 | - /** |
|
52 | - * Creates a new PSR-7 stream. |
|
53 | - * |
|
54 | - * @param string|resource|StreamInterface $body |
|
55 | - * |
|
56 | - * @return StreamInterface |
|
57 | - * |
|
58 | - * @throws InvalidArgumentException |
|
59 | - */ |
|
60 | - public static function create($body = ''): StreamInterface |
|
61 | - { |
|
62 | - if ($body instanceof StreamInterface) { |
|
63 | - return $body; |
|
64 | - } |
|
65 | - |
|
66 | - if (is_string($body)) { |
|
67 | - $resource = fopen('php://temp', 'rw+'); |
|
68 | - fwrite($resource, $body); |
|
69 | - $body = $resource; |
|
70 | - } |
|
71 | - |
|
72 | - if (is_resource($body)) { |
|
73 | - $new = new self(); |
|
74 | - $new->stream = $body; |
|
75 | - $meta = stream_get_meta_data($new->stream); |
|
76 | - $new->seekable = $meta['seekable'] && 0 === \fseek($new->stream, 0, \SEEK_CUR); |
|
77 | - $new->readable = isset(self::READ_WRITE_HASH['read'][$meta['mode']]); |
|
78 | - $new->writable = isset(self::READ_WRITE_HASH['write'][$meta['mode']]); |
|
79 | - $new->uri = $new->getMetadata('uri'); |
|
80 | - |
|
81 | - return $new; |
|
82 | - } |
|
83 | - |
|
84 | - throw new InvalidArgumentException('First argument to Stream::create() must be a string, resource or StreamInterface.'); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Closes the stream when the destructed. |
|
89 | - */ |
|
90 | - public function __destruct() |
|
91 | - { |
|
92 | - $this->close(); |
|
93 | - } |
|
94 | - |
|
95 | - public function __toString(): string |
|
96 | - { |
|
97 | - if ($this->isSeekable()) { |
|
98 | - $this->seek(0); |
|
99 | - } |
|
100 | - |
|
101 | - return $this->getContents(); |
|
102 | - } |
|
103 | - |
|
104 | - public function close(): void |
|
105 | - { |
|
106 | - if (isset($this->stream) && is_resource($this->stream)) { |
|
107 | - fclose($this->stream); |
|
108 | - $this->detach(); |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - public function detach() |
|
113 | - { |
|
114 | - if ($this->stream === null) { |
|
115 | - return null; |
|
116 | - } |
|
117 | - |
|
118 | - $result = $this->stream; |
|
119 | - $this->stream = null; |
|
120 | - |
|
121 | - $this->size = $this->uri = null; |
|
122 | - $this->readable = $this->writable = $this->seekable = false; |
|
123 | - |
|
124 | - return $result; |
|
125 | - } |
|
126 | - |
|
127 | - public function getSize(): ?int |
|
128 | - { |
|
129 | - if (null !== $this->size) { |
|
130 | - return $this->size; |
|
131 | - } |
|
132 | - |
|
133 | - if (!isset($this->stream)) { |
|
134 | - return null; |
|
135 | - } |
|
136 | - |
|
137 | - // Clear the stat cache if the stream has a URI |
|
138 | - if ($this->uri) { |
|
139 | - clearstatcache(true, $this->uri); |
|
140 | - } |
|
141 | - |
|
142 | - $stats = fstat($this->stream); |
|
143 | - if (isset($stats['size'])) { |
|
144 | - $this->size = $stats['size']; |
|
145 | - return $this->size; |
|
146 | - } |
|
147 | - |
|
148 | - return null; |
|
149 | - } |
|
150 | - |
|
151 | - public function tell(): int |
|
152 | - { |
|
153 | - $position = $this->stream === null ? false : ftell($this->stream); |
|
154 | - if (false === $position) { |
|
155 | - throw new RuntimeException('Unable to determine stream position'); |
|
156 | - } |
|
157 | - |
|
158 | - return $position; |
|
159 | - } |
|
160 | - |
|
161 | - public function eof(): bool |
|
162 | - { |
|
163 | - return !$this->stream || feof($this->stream); |
|
164 | - } |
|
165 | - |
|
166 | - public function isSeekable(): bool |
|
167 | - { |
|
168 | - return $this->seekable; |
|
169 | - } |
|
170 | - |
|
171 | - public function seek($offset, $whence = SEEK_SET): void |
|
172 | - { |
|
173 | - if (!$this->seekable) { |
|
174 | - throw new RuntimeException('Stream is not seekable'); |
|
175 | - } |
|
176 | - |
|
177 | - if (-1 === fseek($this->stream, $offset, $whence)) { |
|
178 | - throw new RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, true)); |
|
179 | - } |
|
180 | - } |
|
181 | - |
|
182 | - public function rewind(): void |
|
183 | - { |
|
184 | - $this->seek(0); |
|
185 | - } |
|
186 | - |
|
187 | - public function isWritable(): bool |
|
188 | - { |
|
189 | - return $this->writable; |
|
190 | - } |
|
191 | - |
|
192 | - public function write($string): int |
|
193 | - { |
|
194 | - if (!$this->writable) { |
|
195 | - throw new RuntimeException('Cannot write to a non-writable stream'); |
|
196 | - } |
|
197 | - |
|
198 | - // We can't know the size after writing anything |
|
199 | - $this->size = null; |
|
200 | - |
|
201 | - if (false === $result = fwrite($this->stream, $string)) { |
|
202 | - throw new RuntimeException('Unable to write to stream'); |
|
203 | - } |
|
204 | - |
|
205 | - return $result; |
|
206 | - } |
|
207 | - |
|
208 | - public function isReadable(): bool |
|
209 | - { |
|
210 | - return $this->readable; |
|
211 | - } |
|
212 | - |
|
213 | - public function read($length): string |
|
214 | - { |
|
215 | - if (!$this->readable) { |
|
216 | - throw new RuntimeException('Cannot read from non-readable stream'); |
|
217 | - } |
|
218 | - |
|
219 | - return fread($this->stream, $length); |
|
220 | - } |
|
221 | - |
|
222 | - public function getContents(): string |
|
223 | - { |
|
224 | - if (!isset($this->stream)) { |
|
225 | - throw new RuntimeException('Unable to read stream contents'); |
|
226 | - } |
|
227 | - |
|
228 | - if (false === $contents = stream_get_contents($this->stream)) { |
|
229 | - throw new RuntimeException('Unable to read stream contents'); |
|
230 | - } |
|
231 | - |
|
232 | - return $contents; |
|
233 | - } |
|
234 | - |
|
235 | - public function getMetadata($key = null) |
|
236 | - { |
|
237 | - if (!isset($this->stream)) { |
|
238 | - return $key ? null : []; |
|
239 | - } |
|
240 | - |
|
241 | - $meta = stream_get_meta_data($this->stream); |
|
242 | - |
|
243 | - if (null === $key) { |
|
244 | - return $meta; |
|
245 | - } |
|
246 | - |
|
247 | - return $meta[$key] ?? null; |
|
248 | - } |
|
26 | + /** @var resource|null A resource reference */ |
|
27 | + protected $stream = null; |
|
28 | + protected bool $seekable = false; |
|
29 | + protected bool $readable = false; |
|
30 | + protected bool $writable = false; |
|
31 | + /** @var array|mixed|void|null */ |
|
32 | + protected $uri; |
|
33 | + /** @var int|null */ |
|
34 | + protected ?int $size = null; |
|
35 | + |
|
36 | + protected const READ_WRITE_HASH = [ |
|
37 | + 'read' => [ |
|
38 | + 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true, |
|
39 | + 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, |
|
40 | + 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true, |
|
41 | + 'x+t' => true, 'c+t' => true, 'a+' => true, |
|
42 | + ], |
|
43 | + 'write' => [ |
|
44 | + 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true, |
|
45 | + 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true, |
|
46 | + 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true, |
|
47 | + 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true, |
|
48 | + ], |
|
49 | + ]; |
|
50 | + |
|
51 | + /** |
|
52 | + * Creates a new PSR-7 stream. |
|
53 | + * |
|
54 | + * @param string|resource|StreamInterface $body |
|
55 | + * |
|
56 | + * @return StreamInterface |
|
57 | + * |
|
58 | + * @throws InvalidArgumentException |
|
59 | + */ |
|
60 | + public static function create($body = ''): StreamInterface |
|
61 | + { |
|
62 | + if ($body instanceof StreamInterface) { |
|
63 | + return $body; |
|
64 | + } |
|
65 | + |
|
66 | + if (is_string($body)) { |
|
67 | + $resource = fopen('php://temp', 'rw+'); |
|
68 | + fwrite($resource, $body); |
|
69 | + $body = $resource; |
|
70 | + } |
|
71 | + |
|
72 | + if (is_resource($body)) { |
|
73 | + $new = new self(); |
|
74 | + $new->stream = $body; |
|
75 | + $meta = stream_get_meta_data($new->stream); |
|
76 | + $new->seekable = $meta['seekable'] && 0 === \fseek($new->stream, 0, \SEEK_CUR); |
|
77 | + $new->readable = isset(self::READ_WRITE_HASH['read'][$meta['mode']]); |
|
78 | + $new->writable = isset(self::READ_WRITE_HASH['write'][$meta['mode']]); |
|
79 | + $new->uri = $new->getMetadata('uri'); |
|
80 | + |
|
81 | + return $new; |
|
82 | + } |
|
83 | + |
|
84 | + throw new InvalidArgumentException('First argument to Stream::create() must be a string, resource or StreamInterface.'); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Closes the stream when the destructed. |
|
89 | + */ |
|
90 | + public function __destruct() |
|
91 | + { |
|
92 | + $this->close(); |
|
93 | + } |
|
94 | + |
|
95 | + public function __toString(): string |
|
96 | + { |
|
97 | + if ($this->isSeekable()) { |
|
98 | + $this->seek(0); |
|
99 | + } |
|
100 | + |
|
101 | + return $this->getContents(); |
|
102 | + } |
|
103 | + |
|
104 | + public function close(): void |
|
105 | + { |
|
106 | + if (isset($this->stream) && is_resource($this->stream)) { |
|
107 | + fclose($this->stream); |
|
108 | + $this->detach(); |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + public function detach() |
|
113 | + { |
|
114 | + if ($this->stream === null) { |
|
115 | + return null; |
|
116 | + } |
|
117 | + |
|
118 | + $result = $this->stream; |
|
119 | + $this->stream = null; |
|
120 | + |
|
121 | + $this->size = $this->uri = null; |
|
122 | + $this->readable = $this->writable = $this->seekable = false; |
|
123 | + |
|
124 | + return $result; |
|
125 | + } |
|
126 | + |
|
127 | + public function getSize(): ?int |
|
128 | + { |
|
129 | + if (null !== $this->size) { |
|
130 | + return $this->size; |
|
131 | + } |
|
132 | + |
|
133 | + if (!isset($this->stream)) { |
|
134 | + return null; |
|
135 | + } |
|
136 | + |
|
137 | + // Clear the stat cache if the stream has a URI |
|
138 | + if ($this->uri) { |
|
139 | + clearstatcache(true, $this->uri); |
|
140 | + } |
|
141 | + |
|
142 | + $stats = fstat($this->stream); |
|
143 | + if (isset($stats['size'])) { |
|
144 | + $this->size = $stats['size']; |
|
145 | + return $this->size; |
|
146 | + } |
|
147 | + |
|
148 | + return null; |
|
149 | + } |
|
150 | + |
|
151 | + public function tell(): int |
|
152 | + { |
|
153 | + $position = $this->stream === null ? false : ftell($this->stream); |
|
154 | + if (false === $position) { |
|
155 | + throw new RuntimeException('Unable to determine stream position'); |
|
156 | + } |
|
157 | + |
|
158 | + return $position; |
|
159 | + } |
|
160 | + |
|
161 | + public function eof(): bool |
|
162 | + { |
|
163 | + return !$this->stream || feof($this->stream); |
|
164 | + } |
|
165 | + |
|
166 | + public function isSeekable(): bool |
|
167 | + { |
|
168 | + return $this->seekable; |
|
169 | + } |
|
170 | + |
|
171 | + public function seek($offset, $whence = SEEK_SET): void |
|
172 | + { |
|
173 | + if (!$this->seekable) { |
|
174 | + throw new RuntimeException('Stream is not seekable'); |
|
175 | + } |
|
176 | + |
|
177 | + if (-1 === fseek($this->stream, $offset, $whence)) { |
|
178 | + throw new RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, true)); |
|
179 | + } |
|
180 | + } |
|
181 | + |
|
182 | + public function rewind(): void |
|
183 | + { |
|
184 | + $this->seek(0); |
|
185 | + } |
|
186 | + |
|
187 | + public function isWritable(): bool |
|
188 | + { |
|
189 | + return $this->writable; |
|
190 | + } |
|
191 | + |
|
192 | + public function write($string): int |
|
193 | + { |
|
194 | + if (!$this->writable) { |
|
195 | + throw new RuntimeException('Cannot write to a non-writable stream'); |
|
196 | + } |
|
197 | + |
|
198 | + // We can't know the size after writing anything |
|
199 | + $this->size = null; |
|
200 | + |
|
201 | + if (false === $result = fwrite($this->stream, $string)) { |
|
202 | + throw new RuntimeException('Unable to write to stream'); |
|
203 | + } |
|
204 | + |
|
205 | + return $result; |
|
206 | + } |
|
207 | + |
|
208 | + public function isReadable(): bool |
|
209 | + { |
|
210 | + return $this->readable; |
|
211 | + } |
|
212 | + |
|
213 | + public function read($length): string |
|
214 | + { |
|
215 | + if (!$this->readable) { |
|
216 | + throw new RuntimeException('Cannot read from non-readable stream'); |
|
217 | + } |
|
218 | + |
|
219 | + return fread($this->stream, $length); |
|
220 | + } |
|
221 | + |
|
222 | + public function getContents(): string |
|
223 | + { |
|
224 | + if (!isset($this->stream)) { |
|
225 | + throw new RuntimeException('Unable to read stream contents'); |
|
226 | + } |
|
227 | + |
|
228 | + if (false === $contents = stream_get_contents($this->stream)) { |
|
229 | + throw new RuntimeException('Unable to read stream contents'); |
|
230 | + } |
|
231 | + |
|
232 | + return $contents; |
|
233 | + } |
|
234 | + |
|
235 | + public function getMetadata($key = null) |
|
236 | + { |
|
237 | + if (!isset($this->stream)) { |
|
238 | + return $key ? null : []; |
|
239 | + } |
|
240 | + |
|
241 | + $meta = stream_get_meta_data($this->stream); |
|
242 | + |
|
243 | + if (null === $key) { |
|
244 | + return $meta; |
|
245 | + } |
|
246 | + |
|
247 | + return $meta[$key] ?? null; |
|
248 | + } |
|
249 | 249 | } |
250 | 250 | \ No newline at end of file |
@@ -10,64 +10,64 @@ |
||
10 | 10 | |
11 | 11 | class JsonResponse extends HttpResponse implements JsonResponseInterface |
12 | 12 | { |
13 | - protected array $data = []; |
|
14 | - protected bool $isSyncBody = false; |
|
13 | + protected array $data = []; |
|
14 | + protected bool $isSyncBody = false; |
|
15 | 15 | |
16 | - public function __construct( |
|
17 | - array $data, |
|
18 | - int $status = 200, |
|
19 | - array $headers = [], |
|
20 | - string $version = '1.1' |
|
21 | - ) { |
|
22 | - $headers['Content-Type'] = 'application/json'; |
|
23 | - parent::__construct($status, $headers, null, $version); |
|
24 | - $this->data = $data; |
|
25 | - } |
|
16 | + public function __construct( |
|
17 | + array $data, |
|
18 | + int $status = 200, |
|
19 | + array $headers = [], |
|
20 | + string $version = '1.1' |
|
21 | + ) { |
|
22 | + $headers['Content-Type'] = 'application/json'; |
|
23 | + parent::__construct($status, $headers, null, $version); |
|
24 | + $this->data = $data; |
|
25 | + } |
|
26 | 26 | |
27 | - public function getData(): array |
|
28 | - { |
|
29 | - return $this->data; |
|
30 | - } |
|
27 | + public function getData(): array |
|
28 | + { |
|
29 | + return $this->data; |
|
30 | + } |
|
31 | 31 | |
32 | - public function getBody(): StreamInterface |
|
33 | - { |
|
34 | - if (!$this->isSyncBody) { |
|
35 | - $this->isSyncBody = true; |
|
36 | - $data = json_encode($this->data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE, 512); |
|
37 | - parent::withBody(Stream::create($data)); |
|
38 | - } |
|
32 | + public function getBody(): StreamInterface |
|
33 | + { |
|
34 | + if (!$this->isSyncBody) { |
|
35 | + $this->isSyncBody = true; |
|
36 | + $data = json_encode($this->data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE, 512); |
|
37 | + parent::withBody(Stream::create($data)); |
|
38 | + } |
|
39 | 39 | |
40 | - return parent::getBody(); |
|
41 | - } |
|
40 | + return parent::getBody(); |
|
41 | + } |
|
42 | 42 | |
43 | - public function withBody(StreamInterface $body): Message |
|
44 | - { |
|
45 | - $this->data = json_decode((string)$body, true, 512, JSON_THROW_ON_ERROR); |
|
46 | - $this->isSyncBody = true; |
|
47 | - return parent::withBody($body); |
|
48 | - } |
|
43 | + public function withBody(StreamInterface $body): Message |
|
44 | + { |
|
45 | + $this->data = json_decode((string)$body, true, 512, JSON_THROW_ON_ERROR); |
|
46 | + $this->isSyncBody = true; |
|
47 | + return parent::withBody($body); |
|
48 | + } |
|
49 | 49 | |
50 | - public function setData(array $data): self |
|
51 | - { |
|
52 | - $this->isSyncBody = false; |
|
53 | - $this->data = $data; |
|
54 | - return $this; |
|
55 | - } |
|
50 | + public function setData(array $data): self |
|
51 | + { |
|
52 | + $this->isSyncBody = false; |
|
53 | + $this->data = $data; |
|
54 | + return $this; |
|
55 | + } |
|
56 | 56 | |
57 | - public function reset(): self |
|
58 | - { |
|
59 | - $this->setData([]); |
|
60 | - $this->protocol = '1.1'; |
|
61 | - $this->statusCode = 200; |
|
62 | - $this->attributes = []; |
|
63 | - $this->headers = []; |
|
64 | - $this->withHeader('Content-Type', 'application/json'); |
|
57 | + public function reset(): self |
|
58 | + { |
|
59 | + $this->setData([]); |
|
60 | + $this->protocol = '1.1'; |
|
61 | + $this->statusCode = 200; |
|
62 | + $this->attributes = []; |
|
63 | + $this->headers = []; |
|
64 | + $this->withHeader('Content-Type', 'application/json'); |
|
65 | 65 | |
66 | - if ($this->stream) { |
|
67 | - $this->stream->close(); |
|
68 | - $this->stream = null; |
|
69 | - } |
|
66 | + if ($this->stream) { |
|
67 | + $this->stream->close(); |
|
68 | + $this->stream = null; |
|
69 | + } |
|
70 | 70 | |
71 | - return $this; |
|
72 | - } |
|
71 | + return $this; |
|
72 | + } |
|
73 | 73 | } |
74 | 74 | \ No newline at end of file |