PHP 5.5+ I/O utility package
Install via cli using composer:
php composer.phar require jgswift/qio:0.1.*
Install via composer.json using composer:
{
"require": {
"jgswift/qio": "0.1.*"
}
}
qio is a group of utilities meant to abstract stream applications in php. php already provides a large and robust implementation for handling streams and in many cases qio mainly serves as an OOP abstraction around native stream handling. However, qio also provides supplemental implementations for bitwise streaming, directory caching, file uploading, general asset management, and piping
- php 5.5+
- jgswift/qtil - general utility library
- jgswift/kfiltr - filter, map, and hook implementation
- jgswift/observr - observer pattern implementation
- jgswift/kenum - enumerator implementation
The following is a minimal example of file stream handling
// WRITING DATA
$file = new qio\File('myfile.txt');
$stream = new qio\File\Stream($file,qio\Stream\Mode::ReadWriteTruncate);
$writer = new qio\File\Writer($stream);
$stream->open();
$writer->write('foobar');
$stream->close();
// READING DATA
$file = new qio\File('myfile.txt');
$stream = new qio\File\Stream($file,qio\Stream\Mode::Read);
$reader = new qio\File\Reader($stream);
$stream->open();
$value = $reader->readAll();
$stream->close();
var_dump($value); // prints "foobar"
Directory reading is conceptual similar to the above file operations
$dir = new qio\Directory(__DIR__);
$stream = new qio\Directory\Stream($dir);
$reader = new qio\Directory\Reader($stream);
$stream->open();
while($info = $reader->read()) {
echo $info->getPath()."\n"; // PRINTS PATH
}
$stream->close();
Here is a memory stream that handles bytes reading/writing
// WRITING BYTES
$file = new qio\File('myfile.txt');
$stream = new qio\File\Stream($file,qio\Stream\Mode::ReadWriteTruncate);
$writer = new qio\Memory\Writer($stream);
$stream->open();
$writer->writeString('test');
$writer->writeInteger(4);
$writer->writeBoolean(true);
$stream->close();
// READING BYTES
$file = new qio\File('myfile.txt');
$stream = new qio\File\Stream($file,qio\Stream\Mode::Read);
$reader = new qio\Memory\Reader($stream);
$stream->open();
$string = $reader->readString();
$int = $reader->readInteger();
$bool = $reader->readBoolean();
$stream->close();
var_dump($string,$int,$bool); // PRINTS 'test', 4, true
Serialize data on the fly by wrapping the file writer inside of a serial writer
class User {
public $name;
}
$user = new User;
$user->name = 'test';
$file = new qio\File('myfile.txt');
$stream = new qio\File\Stream($file,qio\Stream\Mode::ReadWriteTruncate);
$writer = new qio\Object\Serial\Writer(
new qio\File\Writer($stream)
);
$stream->open();
$writer->write($user); // write user to stream
$stream->close();
Unserialize serial data by wrapping a file reader with a serial reader
$file = new qio\File('myfile.txt');
$stream = new qio\File\Stream($file,\qio\Stream\Mode::Read);
$reader = new qio\Object\Serial\Reader(
new qio\File\Reader($stream)
);
$stream->open();
$user = $reader->read(); // read user from stream
$stream->close();
var_dump($user); // User#object { "name" => "test" }
Pipe reads input data from a source stream and writes it to an output stream automatically
$myfile = new qio\File('myfile.txt');
$otherfile = new qio\File('otherfile.txt');
$source = new qio\File\Stream($file,qio\Stream\Mode::Read);
$target = new qio\File\Stream($otherfile,qio\Stream\Mode::ReadWriteTruncate);
$reader = new qio\File\Reader($source);
$writer = new qio\File\Writer($target);
$source->open();
$target->open();
$reader->pipe($writer);
$source->close();
$target->close();