1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thruster\Component\HttpMessage; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\StreamInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class FnStream |
9
|
|
|
* |
10
|
|
|
* @package Thruster\Component\HttpMessage |
11
|
|
|
* @author Aurimas Niekis <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class FnStream implements StreamInterface |
14
|
|
|
{ |
15
|
|
|
const SLOTS = ['__toString', 'close', 'detach', 'rewind', |
16
|
|
|
'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', |
17
|
|
|
'isReadable', 'read', 'getContents', 'getMetadata']; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $methods; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $methods Hash of method name to a callable. |
26
|
|
|
*/ |
27
|
11 |
|
public function __construct(array $methods) |
28
|
|
|
{ |
29
|
11 |
|
$this->methods = $methods; |
30
|
|
|
|
31
|
|
|
// Create the functions on the class |
32
|
11 |
|
foreach ($methods as $name => $fn) { |
33
|
9 |
|
$this->{'_fn_' . $name} = $fn; |
34
|
|
|
} |
35
|
11 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Lazily determine which methods are not implemented. |
39
|
|
|
* @throws \BadMethodCallException |
40
|
|
|
*/ |
41
|
1 |
|
public function __get($name) |
42
|
|
|
{ |
43
|
1 |
|
throw new \BadMethodCallException(str_replace('_fn_', '', $name) |
44
|
1 |
|
. '() is not implemented in the FnStream'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The close method is called on the underlying stream only if possible. |
49
|
|
|
*/ |
50
|
11 |
|
public function __destruct() |
51
|
|
|
{ |
52
|
11 |
|
if (isset($this->_fn_close)) { |
53
|
7 |
|
call_user_func($this->_fn_close); |
|
|
|
|
54
|
|
|
} |
55
|
11 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Adds custom functionality to an underlying stream by intercepting |
59
|
|
|
* specific method calls. |
60
|
|
|
* |
61
|
|
|
* @param StreamInterface $stream Stream to decorate |
62
|
|
|
* @param array $methods Hash of method name to a closure |
63
|
|
|
* |
64
|
|
|
* @return FnStream |
65
|
|
|
*/ |
66
|
6 |
|
public static function decorate(StreamInterface $stream, array $methods) |
67
|
|
|
{ |
68
|
|
|
// If any of the required methods were not provided, then simply |
69
|
|
|
// proxy to the decorated stream. |
70
|
6 |
|
foreach (array_diff(static::SLOTS, array_keys($methods)) as $diff) { |
71
|
6 |
|
$methods[$diff] = [$stream, $diff]; |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
return new self($methods); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function __toString() |
78
|
|
|
{ |
79
|
1 |
|
return call_user_func($this->_fn___toString); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function close() |
83
|
|
|
{ |
84
|
1 |
|
return call_user_func($this->_fn_close); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public function detach() |
88
|
|
|
{ |
89
|
1 |
|
return call_user_func($this->_fn_detach); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
6 |
|
public function getSize() |
93
|
|
|
{ |
94
|
6 |
|
return call_user_func($this->_fn_getSize); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public function tell() |
98
|
|
|
{ |
99
|
2 |
|
return call_user_func($this->_fn_tell); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
5 |
|
public function eof() |
103
|
|
|
{ |
104
|
5 |
|
return call_user_func($this->_fn_eof); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
public function isSeekable() |
108
|
|
|
{ |
109
|
4 |
|
return call_user_func($this->_fn_isSeekable); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
public function rewind() |
113
|
|
|
{ |
114
|
3 |
|
call_user_func($this->_fn_rewind); |
|
|
|
|
115
|
3 |
|
} |
116
|
|
|
|
117
|
2 |
|
public function seek($offset, $whence = SEEK_SET) |
118
|
|
|
{ |
119
|
2 |
|
call_user_func($this->_fn_seek, $offset, $whence); |
|
|
|
|
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
public function isWritable() |
123
|
|
|
{ |
124
|
1 |
|
return call_user_func($this->_fn_isWritable); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
public function write($string) |
128
|
|
|
{ |
129
|
2 |
|
return call_user_func($this->_fn_write, $string); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
4 |
|
public function isReadable() |
133
|
|
|
{ |
134
|
4 |
|
return call_user_func($this->_fn_isReadable); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
7 |
|
public function read($length) |
138
|
|
|
{ |
139
|
7 |
|
return call_user_func($this->_fn_read, $length); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
public function getContents() |
143
|
|
|
{ |
144
|
1 |
|
return call_user_func($this->_fn_getContents); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
4 |
|
public function getMetadata($key = null) |
148
|
|
|
{ |
149
|
4 |
|
return call_user_func($this->_fn_getMetadata, $key); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.