scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2 namespace GuzzleHttp\Stream;
3  
4 /**
5 * Converts Guzzle streams into PHP stream resources.
6 */
7 class GuzzleStreamWrapper
8 {
9 /** @var resource */
10 public $context;
11  
12 /** @var StreamInterface */
13 private $stream;
14  
15 /** @var string r, r+, or w */
16 private $mode;
17  
18 /**
19 * Returns a resource representing the stream.
20 *
21 * @param StreamInterface $stream The stream to get a resource for
22 *
23 * @return resource
24 * @throws \InvalidArgumentException if stream is not readable or writable
25 */
26 public static function getResource(StreamInterface $stream)
27 {
28 self::register();
29  
30 if ($stream->isReadable()) {
31 $mode = $stream->isWritable() ? 'r+' : 'r';
32 } elseif ($stream->isWritable()) {
33 $mode = 'w';
34 } else {
35 throw new \InvalidArgumentException('The stream must be readable, '
36 . 'writable, or both.');
37 }
38  
39 return fopen('guzzle://stream', $mode, null, stream_context_create([
40 'guzzle' => ['stream' => $stream]
41 ]));
42 }
43  
44 /**
45 * Registers the stream wrapper if needed
46 */
47 public static function register()
48 {
49 if (!in_array('guzzle', stream_get_wrappers())) {
50 stream_wrapper_register('guzzle', __CLASS__);
51 }
52 }
53  
54 public function stream_open($path, $mode, $options, &$opened_path)
55 {
56 $options = stream_context_get_options($this->context);
57  
58 if (!isset($options['guzzle']['stream'])) {
59 return false;
60 }
61  
62 $this->mode = $mode;
63 $this->stream = $options['guzzle']['stream'];
64  
65 return true;
66 }
67  
68 public function stream_read($count)
69 {
70 return $this->stream->read($count);
71 }
72  
73 public function stream_write($data)
74 {
75 return (int) $this->stream->write($data);
76 }
77  
78 public function stream_tell()
79 {
80 return $this->stream->tell();
81 }
82  
83 public function stream_eof()
84 {
85 return $this->stream->eof();
86 }
87  
88 public function stream_flush()
89 {
90 return $this->stream->flush();
91 }
92  
93 public function stream_seek($offset, $whence)
94 {
95 return $this->stream->seek($offset, $whence);
96 }
97  
98 public function stream_stat()
99 {
100 static $modeMap = [
101 'r' => 33060,
102 'r+' => 33206,
103 'w' => 33188
104 ];
105  
106 return [
107 'dev' => 0,
108 'ino' => 0,
109 'mode' => $modeMap[$this->mode],
110 'nlink' => 0,
111 'uid' => 0,
112 'gid' => 0,
113 'rdev' => 0,
114 'size' => $this->stream->getSize() ?: 0,
115 'atime' => 0,
116 'mtime' => 0,
117 'ctime' => 0,
118 'blksize' => 0,
119 'blocks' => 0
120 ];
121 }
122 }