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