|
| 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 Oscar Otero (http://oscarotero.com) / 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 | + |
| 158 | + return $callback ? $callback() : ''; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * {@inheritdoc} |
| 163 | + */ |
| 164 | + public function getMetadata($key = null) |
| 165 | + { |
| 166 | + $metadata = [ |
| 167 | + 'eof' => $this->eof(), |
| 168 | + 'stream_type' => 'callback', |
| 169 | + 'seekable' => false |
| 170 | + ]; |
| 171 | + |
| 172 | + if (null === $key) { |
| 173 | + return $metadata; |
| 174 | + } |
| 175 | + |
| 176 | + if (! array_key_exists($key, $metadata)) { |
| 177 | + return null; |
| 178 | + } |
| 179 | + |
| 180 | + return $metadata[$key]; |
| 181 | + } |
| 182 | +} |
0 commit comments