pcelta/virtual-stream

A Virtual Stream Wrapper

dev-master 2017-08-20 08:50 UTC

This package is not auto-updated.

Last update: 2024-11-10 04:43:08 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads License

virtual-stream

The Virtual Stream will help you to avoid saving files into filesystem. This is very useful when we're transferring files and testing.

Instalation

{
    "require": {
        "pcelta/virtual-stream": "dev-master"
    }
}

How to use it

This library implements StreamWrapper class definition exactly as shown here: http://php.net/manual/en/class.streamwrapper.php.

Basically, this class implements all methods needed to avoid calls to filesystem. For example, in this scenario below you can see many function handling a file resource.

$filename = __DIR__ . '/dummy.txt';
$resource = fopen($filename, 'w+');

fwrite($resource, 'first');
fwrite($resource, 'second,');
$result = fread($resource, 20);

fclose($resource);

To make sure this example will work you need to give permission on current directory for writing and reading because PHP is going to write it.

That could be avoided using Virtual Stream!

Let's have a look at same example above but right now it is using Virtual Stream.

Stream::register(); // register a new StreamWrapper

$filename = sprintf('%s://dummy.txt', Stream::DEFAULT_PROTOCOL);
$resource = fopen($filename, 'w+');

fwrite($resource, 'first');
fwrite($resource, 'second,');
$result = fread($resource, 20);

fclose($resource);

Stream::unregister(); // remove a StreamWrapper registered.

In this example, PHP will not use the filesystem to perform these f* functions.