jgswift/qio
Composer 安装命令:
composer require jgswift/qio
包简介
PHP 5.5+ I/O utility package
README 文档
README
PHP 5.5+ I/O utility package
Installation
Install via cli using composer:
php composer.phar require jgswift/qio:0.1.*
Install via composer.json using composer:
{
"require": {
"jgswift/qio": "0.1.*"
}
}
Description
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
Dependency
- php 5.5+
- jgswift/qtil - general utility library
- jgswift/kfiltr - filter, map, and hook implementation
- jgswift/observr - observer pattern implementation
- jgswift/kenum - enumerator implementation
Usage
File Writer
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();
File Reader
// 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 Reader
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();
Memory Writer
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();
Memory Reader
// 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
Object Writer
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();
Object Reader
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" }
Reader Piping
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();
统计信息
- 总下载量: 110
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-04-21