ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
FnStream.php
Go to the documentation of this file.
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
5 
12 class FnStream implements StreamInterface
13 {
15  private $methods;
16 
18  private static $slots = ['__toString', 'close', 'detach', 'rewind',
19  'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write',
20  'isReadable', 'read', 'getContents', 'getMetadata'];
21 
25  public function __construct(array $methods)
26  {
27  $this->methods = $methods;
28 
29  // Create the functions on the class
30  foreach ($methods as $name => $fn) {
31  $this->{'_fn_' . $name} = $fn;
32  }
33  }
34 
39  public function __get($name)
40  {
41  throw new \BadMethodCallException(str_replace('_fn_', '', $name)
42  . '() is not implemented in the FnStream');
43  }
44 
48  public function __destruct()
49  {
50  if (isset($this->_fn_close)) {
51  call_user_func($this->_fn_close);
52  }
53  }
54 
64  public static function decorate(StreamInterface $stream, array $methods)
65  {
66  // If any of the required methods were not provided, then simply
67  // proxy to the decorated stream.
68  foreach (array_diff(self::$slots, array_keys($methods)) as $diff) {
69  $methods[$diff] = [$stream, $diff];
70  }
71 
72  return new self($methods);
73  }
74 
75  public function __toString()
76  {
77  return call_user_func($this->_fn___toString);
78  }
79 
80  public function close()
81  {
82  return call_user_func($this->_fn_close);
83  }
84 
85  public function detach()
86  {
87  return call_user_func($this->_fn_detach);
88  }
89 
90  public function getSize()
91  {
92  return call_user_func($this->_fn_getSize);
93  }
94 
95  public function tell()
96  {
97  return call_user_func($this->_fn_tell);
98  }
99 
100  public function eof()
101  {
102  return call_user_func($this->_fn_eof);
103  }
104 
105  public function isSeekable()
106  {
107  return call_user_func($this->_fn_isSeekable);
108  }
109 
110  public function rewind()
111  {
112  call_user_func($this->_fn_rewind);
113  }
114 
115  public function seek($offset, $whence = SEEK_SET)
116  {
117  call_user_func($this->_fn_seek, $offset, $whence);
118  }
119 
120  public function isWritable()
121  {
122  return call_user_func($this->_fn_isWritable);
123  }
124 
125  public function write($string)
126  {
127  return call_user_func($this->_fn_write, $string);
128  }
129 
130  public function isReadable()
131  {
132  return call_user_func($this->_fn_isReadable);
133  }
134 
135  public function read($length)
136  {
137  return call_user_func($this->_fn_read, $length);
138  }
139 
140  public function getContents()
141  {
142  return call_user_func($this->_fn_getContents);
143  }
144 
145  public function getMetadata($key = null)
146  {
147  return call_user_func($this->_fn_getMetadata, $key);
148  }
149 }
eof()
Returns true if the stream is at the end of the stream.
Definition: FnStream.php:100
tell()
Returns the current position of the file read/write pointer.
Definition: FnStream.php:95
rewind()
Seek to the beginning of the stream.
Definition: FnStream.php:110
isReadable()
Returns whether or not the stream is readable.
Definition: FnStream.php:130
isSeekable()
Returns whether or not the stream is seekable.
Definition: FnStream.php:105
static decorate(StreamInterface $stream, array $methods)
Adds custom functionality to an underlying stream by intercepting specific method calls...
Definition: FnStream.php:64
detach()
Separates any underlying resources from the stream.
Definition: FnStream.php:85
read($length)
Read data from the stream.
Definition: FnStream.php:135
isWritable()
Returns whether or not the stream is writable.
Definition: FnStream.php:120
__destruct()
The close method is called on the underlying stream only if possible.
Definition: FnStream.php:48
$stream
PHP stream implementation.
__get($name)
Lazily determine which methods are not implemented.
Definition: FnStream.php:39
__construct(array $methods)
Definition: FnStream.php:25
getSize()
Get the size of the stream if known.
Definition: FnStream.php:90
getContents()
Returns the remaining contents in a string.
Definition: FnStream.php:140
close()
Closes the stream and any underlying resources.
Definition: FnStream.php:80
Compose stream implementations based on a hash of functions.
Definition: FnStream.php:12
getMetadata($key=null)
Get stream metadata as an associative array or retrieve a specific key.
Definition: FnStream.php:145
seek($offset, $whence=SEEK_SET)
Seek to a position in the stream.
Definition: FnStream.php:115
__toString()
Reads all data from the stream into a string, from the beginning to end.
Definition: FnStream.php:75
$key
Definition: croninfo.php:18
write($string)
Write data to the stream.
Definition: FnStream.php:125
Describes a data stream.