| File: Stream.php Lines 1 to 23 | 
 | 
1 | <?php | 
 | 
2 | namespace Puyo\Psr7; | 
 | 
3 |  | 
 | 
4 | /** | 
 | 
5 |  * ストリームを抽象化したもの | 
 | 
6 |  * | 
 | 
7 |  * PSR-7 の StreamInterface の実装。 | 
 | 
8 |  * HTTPに限らず利用できるが、現状はバイナリにしか対応していない。 | 
 | 
9 |  */ | 
 |  | class Stream implements \Psr\Http\Message\StreamInterface | 
 | 
11 | { | 
 | 
12 |     /** @var resource */ | 
 |  |     protected $fp; | 
 | 
14 |  | 
 | 
15 |     /** | 
 | 
16 |      * ファイルのパスから生成した場合に内部で開くためのStream | 
 | 
17 |      * @var resource | 
 | 
18 |      */ | 
 | 
19 |     protected $tmpFp; | 
 | 
20 |  | 
 | 
21 |     /** | 
 | 
22 |      * ファイルのフルパスもしくはresourceから生成する | 
 | 
23 |      * @param resource|string $fileOrStream | 
|   | 
| File: Stream.php Lines 53 to 80 | 
 | 
53 |  | 
 | 
54 |     /** | 
 | 
55 |      * {@inheritdoc} | 
 | 
56 |      */ | 
 | 
57 |     public function close() { | 
 | 
58 |         if(!$this->fp) { | 
 | 
59 |             return; | 
 | 
60 |         } | 
 | 
61 |  | 
 |  |         $fp = $this->detach(); | 
 | 
63 |         fclose($fp); | 
 | 
64 |     } | 
 | 
65 |  | 
 | 
66 |     /** | 
 | 
67 |      * @return resource | 
 | 
68 |      */ | 
 | 
69 |     public function detach() { | 
 |  |         $fp = $this->fp; | 
 | 
71 |         $this->fp = null; | 
 | 
72 |         return $fp; | 
 | 
73 |     } | 
 | 
74 |  | 
 | 
75 |     /** | 
 | 
76 |      * streamもしくはファイルのフルパスからstreamを割り当てる | 
 | 
77 |      * @param string|resource $fileOrStream | 
 | 
78 |      * @param string $mode | 
 | 
79 |      */ | 
 | 
80 |     public function attach($fileOrStream, $mode='rb') { | 
|   | 
| File: Stream.php Lines 276 to 298 | 
 | 
276 |         if (!array_key_exists($key, $metadata)) { | 
 | 
277 |             return null; | 
 | 
278 |         } | 
 | 
279 |  | 
 | 
280 |         return $metadata[$key]; | 
 | 
281 |     } | 
 | 
282 |  | 
 | 
283 |     private function setStream($fileOrStream, $mode='rb') { | 
 | 
284 |         $error = null; | 
 |  |         $fp = $tmpFp = null; | 
 | 
286 |  | 
 | 
287 |         if(is_string($fileOrStream)) { | 
 |  |             set_error_handler(function ($e) use (&$error) { | 
 | 
289 |                 $error = $e; | 
 | 
290 |             }, E_WARNING); | 
 | 
291 |             $tmpFp = fopen($fileOrStream, $mode); | 
 | 
292 |             restore_error_handler(); | 
 | 
293 |  | 
 | 
294 |             if($error !== null) { | 
 | 
295 |                 throw new \InvalidArgumentException('Invalid stream reference provided'); | 
 | 
296 |             } | 
 | 
297 |         } elseif($this->isAvailableStream($fileOrStream)) { | 
 | 
298 |             $fp = $fileOrStream; | 
|   | 
| File: Stream.php Lines 302 to 321 | 
 | 
302 |         if($tmpFp !== null) { | 
 | 
303 |             $this->fp = $this->tmpFp = $tmpFp; | 
 | 
304 |         } | 
 | 
305 |     } | 
 | 
306 |  | 
 | 
307 |     /** | 
 | 
308 |      * @param resource $fp | 
 | 
309 |      * @return bool | 
 | 
310 |      */ | 
 |  |     private function isAvailableStream($fp) { | 
 | 
312 |         if(is_resource($fp) && 'stream' === get_resource_type($fp)) { | 
 | 
313 |             return true; | 
 | 
314 |         } | 
 | 
315 |         return false; | 
 | 
316 |     } | 
 | 
317 |  | 
 | 
318 |     private function closeTmpStream() { | 
 | 
319 |         if($this->isAvailableStream($this->tmpFp)) { | 
 | 
320 |             fclose($this->tmpFp); | 
 | 
321 |         } | 
|   |