, (*1)
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., (*2)
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., (*3)
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., (*4)
$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., (*5)
That could be avoided using Virtual Stream!, (*6)
Let's have a look at same example above but right now it is using Virtual Stream., (*7)
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., (*8)