gyselroth/stream-iterator
Composer 安装命令:
composer require gyselroth/stream-iterator
包简介
Provides \StreamIterator which allows traversing through an iterator and stringify each element
README 文档
README
\StreamIterator\StreamIterator provides a fully PSR-7 compatible stream wrapper for an interator. You may also pass a callback which handles each yielded iterator entry. \StreamIterator is also nicely useable on blocking iterators and/or to create realtime stream responses.
Requirements
- The minimum supported PHP version is 5.6
- The library depends on the following external PHP libraries:
- psr/http-message (^1.0)
Installation
The package is available at packagist an can be installed via composer:
composer require gyselroth/stream-iterator
Documentation
The examples use a simple ArrayIterator, of course you may use any kind of traversable object.
Read the whole iterator
$my_iterator = new \ArrayIterator([0,1,2,3,4,5]); $stream = new \StreamIterator\StreamIterator($my_iterator); $contents = $stream->getContents(); echo $contents; //Prints 012345
Using a callback
Using a callback enables us to operate on each of the yielded iterator elements:
$my_iterator = new \ArrayIterator([0,1,2,3,4,5]); $stream = new \StreamIterator\StreamIterator($my_iterator, function($item) { return '-'.$item; }) $contents = $stream->getContents(); echo $contents; //Prints -0-1-2-3-4-5
JSON stream example
In this example we create a json output from the example iterator:
$my_iterator = new \ArrayIterator([['foo' => 'bar'], ['foo' => 'bar']]); $stream = new \StreamIterator\StreamIterator($my_iterator, function($item) { if($this->tell() === 0) { $string = '['; } else { $string = ','; } $string .= json_encode($item); if($this->eof()) { $string .= ']'; } return $string; }) $contents = $stream->getContents(); echo $contents; //Prints [{"foo":"bar"},{"foo":"bar"}]
(JSON) stream without buffer
This enables a realtime json stream of an iterator. This also allows to operate on blocking iterators where \Iterator::next() blocks until a new entry gets yielded. Each iterator item gets printed as soon as it arrives.
Note Some web server have output buffers or gzip enabled, this will not work with a realtime stream. Be sure that all buffers are completely disabled (For endpoints where a realtime stream is used). For example if you are using Nginx and PHP-FPM you will most likely need to send a header
header('X-Accel-Buffering', 'no')to disable the fastcgi nginx buffer. Otherwise nginx will buffer your output.
$my_iterator = new \ArrayIterator([['foo' => 'bar'], ['foo' => 'bar']]); $stream = new \StreamIterator\StreamIterator($my_iterator, function($item) { if($this->tell() === 0) { $string = '['; } else { $string = ','; } $string .= json_encode($item); if($this->eof()) { $string .= ']'; } echo $string; flush(); return ''; }) $contents = $stream->getContents(); //Prints [{"foo":"bar"},{"foo":"bar"}] echo $contents; //Prints "" (Empty string)
Changelog
A changelog is available here.
Contribute
We are glad that you would like to contribute to this project. Please follow the given terms.
Thanks
This projects use ideas provided by Matthew Weier O'Phinney phly/psr7examples.
gyselroth/stream-iterator 适用场景与选型建议
gyselroth/stream-iterator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.98k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 10 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「iterator」 「stream」 「psr-7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gyselroth/stream-iterator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gyselroth/stream-iterator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gyselroth/stream-iterator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
collection php library
Model of a web-based resource
Object-Oriented API for PHP streams
PHP PDO Mysql select statement iterator implemented as multiple queries using LIMIT clauses
Iteration tools for PHP
Sequential way to run Symfony Processes in parallel
统计信息
- 总下载量: 2.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-24