Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Stream.php
001 <?php
002 namespace GuzzleHttp\Stream;
003
004 /**
005 * PHP stream implementation
006 */
007 class Stream implements StreamInterface
008 {
009 private $stream;
010 private $size;
011 private $seekable;
012 private $readable;
013 private $writable;
014 private $uri;
015 private $customMetadata;
016
017 /** @var array Hash of readable and writable stream types */
018 private static $readWriteHash = [
019 'read' => [
020 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,
021 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,
022 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true,
023 'x+t' => true, 'c+t' => true, 'a+' => true
024 ],
025 'write' => [
026 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true,
027 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true,
028 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true,
029 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true
030 ]
031 ];
032
033 /**
034 * Create a new stream based on the input type.
035 *
036 * This factory accepts the same associative array of options as described
037 * in the constructor.
038 *
039 * @param resource|string|StreamInterface $resource Entity body data
040 * @param array $options Additional options
041 *
042 * @return Stream
043 * @throws \InvalidArgumentException if the $resource arg is not valid.
044 */
045 public static function factory($resource = '', array $options = [])
046 {
047 $type = gettype($resource);
048
049 if ($type == 'string') {
050 $stream = fopen('php://temp', 'r+');
051 if ($resource !== '') {
052 fwrite($stream, $resource);
053 fseek($stream, 0);
054 }
055 return new self($stream, $options);
056 }
057
058 if ($type == 'resource') {
059 return new self($resource, $options);
060 }
061
062 if ($resource instanceof StreamInterface) {
063 return $resource;
064 }
065
066 if ($type == 'object' && method_exists($resource, '__toString')) {
067 return self::factory((string) $resource, $options);
068 }
069
070 if (is_callable($resource)) {
071 return new PumpStream($resource, $options);
072 }
073
074 if ($resource instanceof \Iterator) {
075 return new PumpStream(function () use ($resource) {
076 if (!$resource->valid()) {
077 return false;
078 }
079 $result = $resource->current();
080 $resource->next();
081 return $result;
082 }, $options);
083 }
084
085 throw new \InvalidArgumentException('Invalid resource type: ' . $type);
086 }
087
088 /**
089 * This constructor accepts an associative array of options.
090 *
091 * - size: (int) If a read stream would otherwise have an indeterminate
092 * size, but the size is known due to foreknownledge, then you can
093 * provide that size, in bytes.
094 * - metadata: (array) Any additional metadata to return when the metadata
095 * of the stream is accessed.
096 *
097 * @param resource $stream Stream resource to wrap.
098 * @param array $options Associative array of options.
099 *
100 * @throws \InvalidArgumentException if the stream is not a stream resource
101 */
102 public function __construct($stream, $options = [])
103 {
104 if (!is_resource($stream)) {
105 throw new \InvalidArgumentException('Stream must be a resource');
106 }
107
108 if (isset($options['size'])) {
109 $this->size = $options['size'];
110 }
111
112 $this->customMetadata = isset($options['metadata'])
113 ? $options['metadata']
114 : [];
115
116 $this->attach($stream);
117 }
118
119 /**
120 * Closes the stream when the destructed
121 */
122 public function __destruct()
123 {
124 $this->close();
125 }
126
127 public function __toString()
128 {
129 if (!$this->stream) {
130 return '';
131 }
132
133 $this->seek(0);
134
135 return (string) stream_get_contents($this->stream);
136 }
137
138 public function getContents()
139 {
140 return $this->stream ? stream_get_contents($this->stream) : '';
141 }
142
143 public function close()
144 {
145 if (is_resource($this->stream)) {
146 fclose($this->stream);
147 }
148
149 $this->detach();
150 }
151
152 public function detach()
153 {
154 $result = $this->stream;
155 $this->stream = $this->size = $this->uri = null;
156 $this->readable = $this->writable = $this->seekable = false;
157
158 return $result;
159 }
160
161 public function attach($stream)
162 {
163 $this->stream = $stream;
164 $meta = stream_get_meta_data($this->stream);
165 $this->seekable = $meta['seekable'];
166 $this->readable = isset(self::$readWriteHash['read'][$meta['mode']]);
167 $this->writable = isset(self::$readWriteHash['write'][$meta['mode']]);
168 $this->uri = $this->getMetadata('uri');
169 }
170
171 public function getSize()
172 {
173 if ($this->size !== null) {
174 return $this->size;
175 }
176
177 if (!$this->stream) {
178 return null;
179 }
180
181 // Clear the stat cache if the stream has a URI
182 if ($this->uri) {
183 clearstatcache(true, $this->uri);
184 }
185
186 $stats = fstat($this->stream);
187 if (isset($stats['size'])) {
188 $this->size = $stats['size'];
189 return $this->size;
190 }
191
192 return null;
193 }
194
195 public function isReadable()
196 {
197 return $this->readable;
198 }
199
200 public function isWritable()
201 {
202 return $this->writable;
203 }
204
205 public function isSeekable()
206 {
207 return $this->seekable;
208 }
209
210 public function eof()
211 {
212 return !$this->stream || feof($this->stream);
213 }
214
215 public function tell()
216 {
217 return $this->stream ? ftell($this->stream) : false;
218 }
219
220 public function setSize($size)
221 {
222 $this->size = $size;
223
224 return $this;
225 }
226
227 public function seek($offset, $whence = SEEK_SET)
228 {
229 return $this->seekable
230 ? fseek($this->stream, $offset, $whence) === 0
231 : false;
232 }
233
234 public function read($length)
235 {
236 return $this->readable ? fread($this->stream, $length) : false;
237 }
238
239 public function write($string)
240 {
241 // We can't know the size after writing anything
242 $this->size = null;
243
244 return $this->writable ? fwrite($this->stream, $string) : false;
245 }
246
247 public function getMetadata($key = null)
248 {
249 if (!$this->stream) {
250 return $key ? null : [];
251 } elseif (!$key) {
252 return $this->customMetadata + stream_get_meta_data($this->stream);
253 } elseif (isset($this->customMetadata[$key])) {
254 return $this->customMetadata[$key];
255 }
256
257 $meta = stream_get_meta_data($this->stream);
258
259 return isset($meta[$key]) ? $meta[$key] : null;
260 }
261 }
262