Stream wrapper for strings

I've added a StringStream class to the horde/Support package that lets you access a string as a stream, using fgets, fread, fseek, ftell, etc. - all of the standard stream functionality. 

The neat thing about this implementation is that it doesn't duplicate the string, and it also doesn't use global scope to pass the string to the stream wrapper. The first is accomplished through references; not such a huge trick. The second is harder, since there isn't an obvious way to pass arbitrary data to a stream wrapper implementation. You can pass an object to fopen() as the $path, and if it has a __toString() method it will be used - an interesting trick that I'm still looking for a good use for. Unfortunately the string-ified value is what gets passed to stream_open() inside the stream wrapper implementation.

But! You can use the stream context to pass arbitrary parameters. So the $context parameter becomes the vehicle to pass the Horde_Support_StringStream object to the stream methods, which are implemented in Horde_Stream_Wrapper_String.

Here's an example:

$str = 'large data passed as a string';
$stream = new Horde_Support_StringStream($str);

$fp = $stream->fopen();
while (!feof($fp)) {
    $chunk = fread($fp, 1024);
    // Do something chunked