|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thruster\Component\HttpMessage; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\StreamInterface; |
|
6
|
|
|
use function Thruster\Component\HttpMessage\copy_to_string; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Trait StreamDecoratorTrait |
|
10
|
|
|
* |
|
11
|
|
|
* @package Thruster\Component\HttpMessage |
|
12
|
|
|
* @author Aurimas Niekis <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
trait StreamDecoratorTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param StreamInterface $stream Stream to decorate |
|
18
|
|
|
*/ |
|
19
|
18 |
|
public function __construct(StreamInterface $stream) |
|
20
|
|
|
{ |
|
21
|
18 |
|
$this->stream = $stream; |
|
|
|
|
|
|
22
|
18 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Magic method used to create a new stream if streams are not added in |
|
26
|
|
|
* the constructor of a decorator (e.g., LazyOpenStream). |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $name Name of the property (allows "stream" only). |
|
29
|
|
|
* |
|
30
|
|
|
* @return StreamInterface |
|
31
|
|
|
*/ |
|
32
|
9 |
|
public function __get($name) |
|
33
|
|
|
{ |
|
34
|
9 |
|
if ('stream' == $name) { |
|
35
|
8 |
|
$this->stream = $this->createStream(); |
|
36
|
|
|
|
|
37
|
7 |
|
return $this->stream; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
throw new \UnexpectedValueException("$name not found on class"); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
19 |
|
public function __toString() |
|
44
|
|
|
{ |
|
45
|
|
|
try { |
|
46
|
19 |
|
if ($this->isSeekable()) { |
|
47
|
15 |
|
$this->seek(0); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
19 |
|
return $this->getContents(); |
|
51
|
1 |
|
} catch (\Exception $e) { |
|
52
|
|
|
// Really, PHP? https://bugs.php.net/bug.php?id=53648 |
|
|
|
|
|
|
53
|
1 |
|
trigger_error('StreamDecorator::__toString exception: ' |
|
54
|
1 |
|
. (string) $e, E_USER_ERROR); |
|
55
|
1 |
|
return ''; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
23 |
|
public function getContents() |
|
60
|
|
|
{ |
|
61
|
23 |
|
return copy_to_string($this); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Allow decorators to implement custom methods |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $method Missing method name |
|
68
|
|
|
* @param array $args Method arguments |
|
69
|
|
|
* |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __call($method, array $args) |
|
73
|
|
|
{ |
|
74
|
|
|
$result = call_user_func_array([$this->stream, $method], $args); |
|
75
|
|
|
|
|
76
|
|
|
// Always return the wrapped object if the result is a return $this |
|
77
|
|
|
return $result === $this->stream ? $this : $result; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
public function close() |
|
81
|
|
|
{ |
|
82
|
3 |
|
$this->stream->close(); |
|
83
|
3 |
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
public function getMetadata($key = null) |
|
86
|
|
|
{ |
|
87
|
3 |
|
return $this->stream->getMetadata($key); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
public function detach() |
|
91
|
|
|
{ |
|
92
|
2 |
|
return $this->stream->detach(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
4 |
|
public function getSize() |
|
96
|
|
|
{ |
|
97
|
4 |
|
return $this->stream->getSize(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
15 |
|
public function eof() : bool |
|
101
|
|
|
{ |
|
102
|
15 |
|
return $this->stream->eof(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
8 |
|
public function tell() : int |
|
106
|
|
|
{ |
|
107
|
8 |
|
return $this->stream->tell(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
4 |
|
public function isReadable() : bool |
|
111
|
|
|
{ |
|
112
|
4 |
|
return $this->stream->isReadable(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
3 |
|
public function isWritable() : bool |
|
116
|
|
|
{ |
|
117
|
3 |
|
return $this->stream->isWritable(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
19 |
|
public function isSeekable() : bool |
|
121
|
|
|
{ |
|
122
|
19 |
|
return $this->stream->isSeekable(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function rewind() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->seek(0); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
12 |
|
public function seek($offset, $whence = SEEK_SET) |
|
131
|
|
|
{ |
|
132
|
12 |
|
$this->stream->seek($offset, $whence); |
|
133
|
12 |
|
} |
|
134
|
|
|
|
|
135
|
16 |
|
public function read($length) |
|
136
|
|
|
{ |
|
137
|
16 |
|
return $this->stream->read($length); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
6 |
|
public function write($string) |
|
141
|
|
|
{ |
|
142
|
6 |
|
return $this->stream->write($string); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Implement in subclasses to dynamically create streams when requested. |
|
147
|
|
|
* |
|
148
|
|
|
* @return StreamInterface |
|
149
|
|
|
* @throws \BadMethodCallException |
|
150
|
|
|
*/ |
|
151
|
1 |
|
protected function createStream() |
|
152
|
|
|
{ |
|
153
|
1 |
|
throw new \BadMethodCallException('Not implemented'); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: