| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,151 +0,0 @@ |
| 1 |
-<?php |
|
| 2 |
- |
|
| 3 |
-namespace GuzzleHttp\Psr7; |
|
| 4 |
- |
|
| 5 |
-use Psr\Http\Message\StreamInterface; |
|
| 6 |
- |
|
| 7 |
-/** |
|
| 8 |
- * Stream decorator trait |
|
| 9 |
- * @property StreamInterface stream |
|
| 10 |
- */ |
|
| 11 |
-trait StreamDecoratorTrait |
|
| 12 |
-{
|
|
| 13 |
- /** |
|
| 14 |
- * @param StreamInterface $stream Stream to decorate |
|
| 15 |
- */ |
|
| 16 |
- public function __construct(StreamInterface $stream) |
|
| 17 |
- {
|
|
| 18 |
- $this->stream = $stream; |
|
| 19 |
- } |
|
| 20 |
- |
|
| 21 |
- /** |
|
| 22 |
- * Magic method used to create a new stream if streams are not added in |
|
| 23 |
- * the constructor of a decorator (e.g., LazyOpenStream). |
|
| 24 |
- * |
|
| 25 |
- * @param string $name Name of the property (allows "stream" only). |
|
| 26 |
- * |
|
| 27 |
- * @return StreamInterface |
|
| 28 |
- */ |
|
| 29 |
- public function __get($name) |
|
| 30 |
- {
|
|
| 31 |
- if ($name == 'stream') {
|
|
| 32 |
- $this->stream = $this->createStream(); |
|
| 33 |
- return $this->stream; |
|
| 34 |
- } |
|
| 35 |
- |
|
| 36 |
- throw new \UnexpectedValueException("$name not found on class");
|
|
| 37 |
- } |
|
| 38 |
- |
|
| 39 |
- public function __toString() |
|
| 40 |
- {
|
|
| 41 |
- try {
|
|
| 42 |
- if ($this->isSeekable()) {
|
|
| 43 |
- $this->seek(0); |
|
| 44 |
- } |
|
| 45 |
- return $this->getContents(); |
|
| 46 |
- } catch (\Exception $e) {
|
|
| 47 |
- // Really, PHP? https://bugs.php.net/bug.php?id=53648 |
|
| 48 |
- trigger_error('StreamDecorator::__toString exception: '
|
|
| 49 |
- . (string) $e, E_USER_ERROR); |
|
| 50 |
- return ''; |
|
| 51 |
- } |
|
| 52 |
- } |
|
| 53 |
- |
|
| 54 |
- public function getContents() |
|
| 55 |
- {
|
|
| 56 |
- return Utils::copyToString($this); |
|
| 57 |
- } |
|
| 58 |
- |
|
| 59 |
- /** |
|
| 60 |
- * Allow decorators to implement custom methods |
|
| 61 |
- * |
|
| 62 |
- * @param string $method Missing method name |
|
| 63 |
- * @param array $args Method arguments |
|
| 64 |
- * |
|
| 65 |
- * @return mixed |
|
| 66 |
- */ |
|
| 67 |
- public function __call($method, array $args) |
|
| 68 |
- {
|
|
| 69 |
- $result = call_user_func_array([$this->stream, $method], $args); |
|
| 70 |
- |
|
| 71 |
- // Always return the wrapped object if the result is a return $this |
|
| 72 |
- return $result === $this->stream ? $this : $result; |
|
| 73 |
- } |
|
| 74 |
- |
|
| 75 |
- public function close() |
|
| 76 |
- {
|
|
| 77 |
- $this->stream->close(); |
|
| 78 |
- } |
|
| 79 |
- |
|
| 80 |
- public function getMetadata($key = null) |
|
| 81 |
- {
|
|
| 82 |
- return $this->stream->getMetadata($key); |
|
| 83 |
- } |
|
| 84 |
- |
|
| 85 |
- public function detach() |
|
| 86 |
- {
|
|
| 87 |
- return $this->stream->detach(); |
|
| 88 |
- } |
|
| 89 |
- |
|
| 90 |
- public function getSize() |
|
| 91 |
- {
|
|
| 92 |
- return $this->stream->getSize(); |
|
| 93 |
- } |
|
| 94 |
- |
|
| 95 |
- public function eof() |
|
| 96 |
- {
|
|
| 97 |
- return $this->stream->eof(); |
|
| 98 |
- } |
|
| 99 |
- |
|
| 100 |
- public function tell() |
|
| 101 |
- {
|
|
| 102 |
- return $this->stream->tell(); |
|
| 103 |
- } |
|
| 104 |
- |
|
| 105 |
- public function isReadable() |
|
| 106 |
- {
|
|
| 107 |
- return $this->stream->isReadable(); |
|
| 108 |
- } |
|
| 109 |
- |
|
| 110 |
- public function isWritable() |
|
| 111 |
- {
|
|
| 112 |
- return $this->stream->isWritable(); |
|
| 113 |
- } |
|
| 114 |
- |
|
| 115 |
- public function isSeekable() |
|
| 116 |
- {
|
|
| 117 |
- return $this->stream->isSeekable(); |
|
| 118 |
- } |
|
| 119 |
- |
|
| 120 |
- public function rewind() |
|
| 121 |
- {
|
|
| 122 |
- $this->seek(0); |
|
| 123 |
- } |
|
| 124 |
- |
|
| 125 |
- public function seek($offset, $whence = SEEK_SET) |
|
| 126 |
- {
|
|
| 127 |
- $this->stream->seek($offset, $whence); |
|
| 128 |
- } |
|
| 129 |
- |
|
| 130 |
- public function read($length) |
|
| 131 |
- {
|
|
| 132 |
- return $this->stream->read($length); |
|
| 133 |
- } |
|
| 134 |
- |
|
| 135 |
- public function write($string) |
|
| 136 |
- {
|
|
| 137 |
- return $this->stream->write($string); |
|
| 138 |
- } |
|
| 139 |
- |
|
| 140 |
- /** |
|
| 141 |
- * Implement in subclasses to dynamically create streams when requested. |
|
| 142 |
- * |
|
| 143 |
- * @return StreamInterface |
|
| 144 |
- * |
|
| 145 |
- * @throws \BadMethodCallException |
|
| 146 |
- */ |
|
| 147 |
- protected function createStream() |
|
| 148 |
- {
|
|
| 149 |
- throw new \BadMethodCallException('Not implemented');
|
|
| 150 |
- } |
|
| 151 |
-} |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,151 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace GuzzleHttp\Psr7; |
|
| 4 |
+ |
|
| 5 |
+use Psr\Http\Message\StreamInterface; |
|
| 6 |
+ |
|
| 7 |
+/** |
|
| 8 |
+ * Stream decorator trait |
|
| 9 |
+ * @property StreamInterface stream |
|
| 10 |
+ */ |
|
| 11 |
+trait StreamDecoratorTrait |
|
| 12 |
+{
|
|
| 13 |
+ /** |
|
| 14 |
+ * @param StreamInterface $stream Stream to decorate |
|
| 15 |
+ */ |
|
| 16 |
+ public function __construct(StreamInterface $stream) |
|
| 17 |
+ {
|
|
| 18 |
+ $this->stream = $stream; |
|
| 19 |
+ } |
|
| 20 |
+ |
|
| 21 |
+ /** |
|
| 22 |
+ * Magic method used to create a new stream if streams are not added in |
|
| 23 |
+ * the constructor of a decorator (e.g., LazyOpenStream). |
|
| 24 |
+ * |
|
| 25 |
+ * @param string $name Name of the property (allows "stream" only). |
|
| 26 |
+ * |
|
| 27 |
+ * @return StreamInterface |
|
| 28 |
+ */ |
|
| 29 |
+ public function __get($name) |
|
| 30 |
+ {
|
|
| 31 |
+ if ($name == 'stream') {
|
|
| 32 |
+ $this->stream = $this->createStream(); |
|
| 33 |
+ return $this->stream; |
|
| 34 |
+ } |
|
| 35 |
+ |
|
| 36 |
+ throw new \UnexpectedValueException("$name not found on class");
|
|
| 37 |
+ } |
|
| 38 |
+ |
|
| 39 |
+ public function __toString() |
|
| 40 |
+ {
|
|
| 41 |
+ try {
|
|
| 42 |
+ if ($this->isSeekable()) {
|
|
| 43 |
+ $this->seek(0); |
|
| 44 |
+ } |
|
| 45 |
+ return $this->getContents(); |
|
| 46 |
+ } catch (\Exception $e) {
|
|
| 47 |
+ // Really, PHP? https://bugs.php.net/bug.php?id=53648 |
|
| 48 |
+ trigger_error('StreamDecorator::__toString exception: '
|
|
| 49 |
+ . (string) $e, E_USER_ERROR); |
|
| 50 |
+ return ''; |
|
| 51 |
+ } |
|
| 52 |
+ } |
|
| 53 |
+ |
|
| 54 |
+ public function getContents() |
|
| 55 |
+ {
|
|
| 56 |
+ return Utils::copyToString($this); |
|
| 57 |
+ } |
|
| 58 |
+ |
|
| 59 |
+ /** |
|
| 60 |
+ * Allow decorators to implement custom methods |
|
| 61 |
+ * |
|
| 62 |
+ * @param string $method Missing method name |
|
| 63 |
+ * @param array $args Method arguments |
|
| 64 |
+ * |
|
| 65 |
+ * @return mixed |
|
| 66 |
+ */ |
|
| 67 |
+ public function __call($method, array $args) |
|
| 68 |
+ {
|
|
| 69 |
+ $result = call_user_func_array([$this->stream, $method], $args); |
|
| 70 |
+ |
|
| 71 |
+ // Always return the wrapped object if the result is a return $this |
|
| 72 |
+ return $result === $this->stream ? $this : $result; |
|
| 73 |
+ } |
|
| 74 |
+ |
|
| 75 |
+ public function close() |
|
| 76 |
+ {
|
|
| 77 |
+ $this->stream->close(); |
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 80 |
+ public function getMetadata($key = null) |
|
| 81 |
+ {
|
|
| 82 |
+ return $this->stream->getMetadata($key); |
|
| 83 |
+ } |
|
| 84 |
+ |
|
| 85 |
+ public function detach() |
|
| 86 |
+ {
|
|
| 87 |
+ return $this->stream->detach(); |
|
| 88 |
+ } |
|
| 89 |
+ |
|
| 90 |
+ public function getSize() |
|
| 91 |
+ {
|
|
| 92 |
+ return $this->stream->getSize(); |
|
| 93 |
+ } |
|
| 94 |
+ |
|
| 95 |
+ public function eof() |
|
| 96 |
+ {
|
|
| 97 |
+ return $this->stream->eof(); |
|
| 98 |
+ } |
|
| 99 |
+ |
|
| 100 |
+ public function tell() |
|
| 101 |
+ {
|
|
| 102 |
+ return $this->stream->tell(); |
|
| 103 |
+ } |
|
| 104 |
+ |
|
| 105 |
+ public function isReadable() |
|
| 106 |
+ {
|
|
| 107 |
+ return $this->stream->isReadable(); |
|
| 108 |
+ } |
|
| 109 |
+ |
|
| 110 |
+ public function isWritable() |
|
| 111 |
+ {
|
|
| 112 |
+ return $this->stream->isWritable(); |
|
| 113 |
+ } |
|
| 114 |
+ |
|
| 115 |
+ public function isSeekable() |
|
| 116 |
+ {
|
|
| 117 |
+ return $this->stream->isSeekable(); |
|
| 118 |
+ } |
|
| 119 |
+ |
|
| 120 |
+ public function rewind() |
|
| 121 |
+ {
|
|
| 122 |
+ $this->seek(0); |
|
| 123 |
+ } |
|
| 124 |
+ |
|
| 125 |
+ public function seek($offset, $whence = SEEK_SET) |
|
| 126 |
+ {
|
|
| 127 |
+ $this->stream->seek($offset, $whence); |
|
| 128 |
+ } |
|
| 129 |
+ |
|
| 130 |
+ public function read($length) |
|
| 131 |
+ {
|
|
| 132 |
+ return $this->stream->read($length); |
|
| 133 |
+ } |
|
| 134 |
+ |
|
| 135 |
+ public function write($string) |
|
| 136 |
+ {
|
|
| 137 |
+ return $this->stream->write($string); |
|
| 138 |
+ } |
|
| 139 |
+ |
|
| 140 |
+ /** |
|
| 141 |
+ * Implement in subclasses to dynamically create streams when requested. |
|
| 142 |
+ * |
|
| 143 |
+ * @return StreamInterface |
|
| 144 |
+ * |
|
| 145 |
+ * @throws \BadMethodCallException |
|
| 146 |
+ */ |
|
| 147 |
+ protected function createStream() |
|
| 148 |
+ {
|
|
| 149 |
+ throw new \BadMethodCallException('Not implemented');
|
|
| 150 |
+ } |
|
| 151 |
+} |