Skip to content

Commit 18340a3

Browse files
rrazorondrejmirtes
authored andcommitted
Address #4881 by implementing a more complete fake stream wrapper
1 parent c51c22f commit 18340a3

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/Reflection/BetterReflection/SourceLocator/FileReadTrapStreamWrapper.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ final class FileReadTrapStreamWrapper
3131

3232
public static ?string $autoloadLocatedFile = null;
3333

34+
private bool $readFromFile = false;
35+
36+
private int $seekPosition = 0;
37+
3438
/**
3539
* @param string[] $streamWrapperProtocols
3640
*
@@ -85,6 +89,8 @@ public static function withStreamWrapperOverride(
8589
public function stream_open($path, $mode, $options, &$openedPath): bool
8690
{
8791
self::$autoloadLocatedFile = $path;
92+
$this->readFromFile = false;
93+
$this->seekPosition = 0;
8894

8995
return true;
9096
}
@@ -99,6 +105,8 @@ public function stream_open($path, $mode, $options, &$openedPath): bool
99105
*/
100106
public function stream_read($count): string
101107
{
108+
$this->readFromFile = true;
109+
102110
// Dummy return value that is also valid PHP for require(). We'll read
103111
// and process the file elsewhere, so it's OK to provide dummy data for
104112
// this read.
@@ -173,4 +181,64 @@ public function url_stat($path, $flags)
173181
return $result;
174182
}
175183

184+
/**
185+
* Simulates behavior of reading from an empty file.
186+
*
187+
* @return bool
188+
*/
189+
public function stream_eof(): bool
190+
{
191+
return $this->readFromFile;
192+
}
193+
194+
public function stream_flush(): bool
195+
{
196+
return true;
197+
}
198+
199+
public function stream_tell(): int
200+
{
201+
return $this->seekPosition;
202+
}
203+
204+
/**
205+
* @param int $offset
206+
* @param int $whence
207+
* @return bool
208+
*/
209+
public function stream_seek($offset, $whence): bool
210+
{
211+
switch ($whence) {
212+
// Behavior is the same for a zero-length file
213+
case SEEK_SET:
214+
case SEEK_END:
215+
if ($offset < 0) {
216+
return false;
217+
}
218+
$this->seekPosition = $offset;
219+
return true;
220+
221+
case SEEK_CUR:
222+
if ($offset < 0) {
223+
return false;
224+
}
225+
$this->seekPosition += $offset;
226+
return true;
227+
228+
default:
229+
return false;
230+
}
231+
}
232+
233+
/**
234+
* @param int $option
235+
* @param int $arg1
236+
* @param int $arg2
237+
* @return bool
238+
*/
239+
public function stream_set_option($option, $arg1, $arg2): bool
240+
{
241+
return false;
242+
}
243+
176244
}

0 commit comments

Comments
 (0)