bentools/rewindable-generator
Composer 安装命令:
composer require bentools/rewindable-generator
包简介
Now your generators become rewindable.
README 文档
README
Rewindable generator
$generator = (function () { yield 'foo'; yield 'bar'; })(); var_dump(iterator_to_array($generator)); // ['foo', 'bar'] var_dump(iterator_to_array($generator)); // Boom
PHP Fatal error: Uncaught Exception: Cannot traverse an already closed generator
Yes, I know. That's annoying. But here's a tiny class which will leverage a CachingIterator to make your generator rewindable.
Simple as:
use BenTools\RewindableGenerator; $generator = (function () { yield 'foo'; yield 'bar'; })(); $iterator = new RewindableGenerator($generator); var_dump(iterator_to_array($iterator)); // ['foo', 'bar'] var_dump(iterator_to_array($iterator)); // ['foo', 'bar']
Warning: An exception will be thrown if you intend to rewind a generator which has not reached the end (i.e you breakthe loop), since the CachingIterator won't have all items in cache.
Installation
composer require bentools/rewindable-generator
Tests
./vendor/bin/phpunit
统计信息
- 总下载量: 329
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-04-26