Mercurial > hg > cmmr2012-drupal-site
comparison vendor/zendframework/zend-diactoros/src/CallbackStream.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | 5311817fb629 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 /** | |
3 * Zend Framework (http://framework.zend.com/) | |
4 * | |
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository | |
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com) | |
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License | |
8 */ | |
9 | |
10 namespace Zend\Diactoros; | |
11 | |
12 use InvalidArgumentException; | |
13 use RuntimeException; | |
14 use Psr\Http\Message\StreamInterface; | |
15 | |
16 /** | |
17 * Implementation of PSR HTTP streams | |
18 */ | |
19 class CallbackStream implements StreamInterface | |
20 { | |
21 /** | |
22 * @var callable|null | |
23 */ | |
24 protected $callback; | |
25 | |
26 /** | |
27 * @param callable $callback | |
28 * @throws InvalidArgumentException | |
29 */ | |
30 public function __construct(callable $callback) | |
31 { | |
32 $this->attach($callback); | |
33 } | |
34 | |
35 /** | |
36 * {@inheritdoc} | |
37 */ | |
38 public function __toString() | |
39 { | |
40 return $this->getContents(); | |
41 } | |
42 | |
43 /** | |
44 * {@inheritdoc} | |
45 */ | |
46 public function close() | |
47 { | |
48 $this->callback = null; | |
49 } | |
50 | |
51 /** | |
52 * {@inheritdoc} | |
53 */ | |
54 public function detach() | |
55 { | |
56 $callback = $this->callback; | |
57 $this->callback = null; | |
58 return $callback; | |
59 } | |
60 | |
61 /** | |
62 * Attach a new callback to the instance. | |
63 * | |
64 * @param callable $callback | |
65 * @throws InvalidArgumentException for callable callback | |
66 */ | |
67 public function attach(callable $callback) | |
68 { | |
69 $this->callback = $callback; | |
70 } | |
71 | |
72 /** | |
73 * {@inheritdoc} | |
74 */ | |
75 public function getSize() | |
76 { | |
77 } | |
78 | |
79 /** | |
80 * {@inheritdoc} | |
81 */ | |
82 public function tell() | |
83 { | |
84 throw new RuntimeException('Callback streams cannot tell position'); | |
85 } | |
86 | |
87 /** | |
88 * {@inheritdoc} | |
89 */ | |
90 public function eof() | |
91 { | |
92 return empty($this->callback); | |
93 } | |
94 | |
95 /** | |
96 * {@inheritdoc} | |
97 */ | |
98 public function isSeekable() | |
99 { | |
100 return false; | |
101 } | |
102 | |
103 /** | |
104 * {@inheritdoc} | |
105 */ | |
106 public function seek($offset, $whence = SEEK_SET) | |
107 { | |
108 throw new RuntimeException('Callback streams cannot seek position'); | |
109 } | |
110 | |
111 /** | |
112 * {@inheritdoc} | |
113 */ | |
114 public function rewind() | |
115 { | |
116 throw new RuntimeException('Callback streams cannot rewind position'); | |
117 } | |
118 | |
119 /** | |
120 * {@inheritdoc} | |
121 */ | |
122 public function isWritable() | |
123 { | |
124 return false; | |
125 } | |
126 | |
127 /** | |
128 * {@inheritdoc} | |
129 */ | |
130 public function write($string) | |
131 { | |
132 throw new RuntimeException('Callback streams cannot write'); | |
133 } | |
134 | |
135 /** | |
136 * {@inheritdoc} | |
137 */ | |
138 public function isReadable() | |
139 { | |
140 return false; | |
141 } | |
142 | |
143 /** | |
144 * {@inheritdoc} | |
145 */ | |
146 public function read($length) | |
147 { | |
148 throw new RuntimeException('Callback streams cannot read'); | |
149 } | |
150 | |
151 /** | |
152 * {@inheritdoc} | |
153 */ | |
154 public function getContents() | |
155 { | |
156 $callback = $this->detach(); | |
157 return $callback ? $callback() : ''; | |
158 } | |
159 | |
160 /** | |
161 * {@inheritdoc} | |
162 */ | |
163 public function getMetadata($key = null) | |
164 { | |
165 $metadata = [ | |
166 'eof' => $this->eof(), | |
167 'stream_type' => 'callback', | |
168 'seekable' => false | |
169 ]; | |
170 | |
171 if (null === $key) { | |
172 return $metadata; | |
173 } | |
174 | |
175 if (! array_key_exists($key, $metadata)) { | |
176 return null; | |
177 } | |
178 | |
179 return $metadata[$key]; | |
180 } | |
181 } |