58bd1e4729284aca1244644fad1c3722f5b5e228
[yaffs-website] / vendor / zendframework / zend-diactoros / src / PhpInputStream.php
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 /**
13  * Caching version of php://input
14  */
15 class PhpInputStream extends Stream
16 {
17     /**
18      * @var string
19      */
20     private $cache = '';
21
22     /**
23      * @var bool
24      */
25     private $reachedEof = false;
26
27     /**
28      * @param  string|resource $stream
29      */
30     public function __construct($stream = 'php://input')
31     {
32         parent::__construct($stream, 'r');
33     }
34
35     /**
36      * {@inheritdoc}
37      */
38     public function __toString()
39     {
40         if ($this->reachedEof) {
41             return $this->cache;
42         }
43
44         $this->getContents();
45         return $this->cache;
46     }
47
48     /**
49      * {@inheritdoc}
50      */
51     public function isWritable()
52     {
53         return false;
54     }
55
56     /**
57      * {@inheritdoc}
58      */
59     public function read($length)
60     {
61         $content = parent::read($length);
62         if ($content && ! $this->reachedEof) {
63             $this->cache .= $content;
64         }
65
66         if ($this->eof()) {
67             $this->reachedEof = true;
68         }
69
70         return $content;
71     }
72
73     /**
74      * {@inheritdoc}
75      */
76     public function getContents($maxLength = -1)
77     {
78         if ($this->reachedEof) {
79             return $this->cache;
80         }
81
82         $contents     = stream_get_contents($this->resource, $maxLength);
83         $this->cache .= $contents;
84
85         if ($maxLength === -1 || $this->eof()) {
86             $this->reachedEof = true;
87         }
88
89         return $contents;
90     }
91 }