|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Phalcon Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Phalcon Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Phalcon\Http\Message\Stream; |
|
15
|
|
|
|
|
16
|
|
|
use Phalcon\Http\Message\Stream; |
|
17
|
|
|
use RuntimeException; |
|
18
|
|
|
|
|
19
|
|
|
use function stream_get_contents; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Describes a data stream from "php://input" |
|
23
|
|
|
* |
|
24
|
|
|
* Typically, an instance will wrap a PHP stream; this interface provides |
|
25
|
|
|
* a wrapper around the most common operations, including serialization of |
|
26
|
|
|
* the entire stream to a string. |
|
27
|
|
|
* |
|
28
|
|
|
* @property string $data |
|
29
|
|
|
* @property bool $eof |
|
30
|
|
|
*/ |
|
31
|
|
|
class Input extends Stream |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $data = ""; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var bool |
|
40
|
|
|
*/ |
|
41
|
|
|
private $eof = false; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Input constructor. |
|
45
|
|
|
*/ |
|
46
|
67 |
|
public function __construct() |
|
47
|
|
|
{ |
|
48
|
67 |
|
parent::__construct("php://input", "rb"); |
|
49
|
67 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Reads all data from the stream into a string, from the beginning to end. |
|
53
|
|
|
* |
|
54
|
|
|
* This method MUST attempt to seek to the beginning of the stream before |
|
55
|
|
|
* reading data and read the stream until the end is reached. |
|
56
|
|
|
* |
|
57
|
|
|
* Warning: This could attempt to load a large amount of data into memory. |
|
58
|
|
|
* |
|
59
|
|
|
* This method MUST NOT raise an exception in order to conform with PHP's |
|
60
|
|
|
* string casting operations. |
|
61
|
|
|
* |
|
62
|
|
|
* @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __toString(): string |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->eof) { |
|
67
|
|
|
return $this->data; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->getContents(); |
|
71
|
|
|
|
|
72
|
|
|
return $this->data; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns the remaining contents in a string |
|
77
|
|
|
* |
|
78
|
|
|
* @param int $length |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
* @throws RuntimeException if unable to read. |
|
82
|
|
|
* @throws RuntimeException if error occurs while reading. |
|
83
|
|
|
* |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getContents(int $length = -1): string |
|
86
|
|
|
{ |
|
87
|
|
|
if ($this->eof) { |
|
88
|
|
|
return $this->data; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$data = stream_get_contents($this->handle, $length); |
|
92
|
|
|
$this->data = $data; |
|
93
|
|
|
|
|
94
|
|
|
if (-1 === $length || $this->eof()) { |
|
95
|
|
|
$this->eof = true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this->data; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns whether or not the stream is writeable. |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function isWritable(): bool |
|
105
|
|
|
{ |
|
106
|
1 |
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Read data from the stream. |
|
111
|
|
|
* |
|
112
|
|
|
* @param int $length |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function read($length): string |
|
117
|
|
|
{ |
|
118
|
|
|
$data = parent::read($length); |
|
119
|
|
|
|
|
120
|
|
|
if (true !== $this->eof) { |
|
121
|
|
|
$this->data = $data; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($this->eof()) { |
|
125
|
|
|
$this->eof = true; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $data; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|