PHPackages                             bentools/rewindable-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bentools/rewindable-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bentools/rewindable-generator
=============================

Now your generators become rewindable.

1.2(2y ago)4329↓85.7%1MITPHPPHP &gt;=8.1

Since Apr 26Pushed 2y ago1 watchersCompare

[ Source](https://github.com/bpolaszek/rewindable-generator)[ Packagist](https://packagist.org/packages/bentools/rewindable-generator)[ RSS](/packages/bentools-rewindable-generator/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (6)Versions (5)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/2a63f76e40defe7ea2ba49257052923f0be83df96262a7cd978235509d93798e/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f726577696e6461626c652d67656e657261746f722f762f737461626c65)](https://packagist.org/packages/bentools/rewindable-generator)[![License](https://camo.githubusercontent.com/5403b74116e5a3e435c28c43c62761b994500da60213cfc208ab22a06c4cee71/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f726577696e6461626c652d67656e657261746f722f6c6963656e7365)](https://packagist.org/packages/bentools/rewindable-generator)[![CI Workflow](https://github.com/bpolaszek/rewindable-generator/actions/workflows/ci.yml/badge.svg)](https://github.com/bpolaszek/rewindable-generator/actions/workflows/ci.yml)[![Coverage](https://camo.githubusercontent.com/e829111600c46a9870fcf5dd9a7ad33e8d83ee1814bc7aa52d691a45d4258a4a/68747470733a2f2f636f6465636f762e696f2f67682f62706f6c61737a656b2f726577696e6461626c652d67656e657261746f722f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d4c35756c5461796d6274)](https://codecov.io/gh/bpolaszek/rewindable-generator)[![Total Downloads](https://camo.githubusercontent.com/61b8676659d983c6b455ed5e39bd8ce086d4a8e5885a1d56d7eb881a0aac0117/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f726577696e6461626c652d67656e657261746f722f646f776e6c6f616473)](https://packagist.org/packages/bentools/rewindable-generator)

Rewindable generator
====================

[](#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 `break`the loop), since the `CachingIterator` won't have all items in cache.

Installation
------------

[](#installation)

> composer require bentools/rewindable-generator

Tests
-----

[](#tests)

> ./vendor/bin/phpunit

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 50% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~689 days

Total

4

Last Release

920d ago

PHP version history (2 changes)1.0PHP &gt;=5.5

1.1PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/acdd1a8ee0e657ddd06cf11f98a32100ef7121afb8aa270a5b295f5c29c038b3?d=identicon)[bpolaszek](/maintainers/bpolaszek)

---

Top Contributors

[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (3 commits)")[![simPod](https://avatars.githubusercontent.com/u/327717?v=4)](https://github.com/simPod "simPod (3 commits)")

---

Tags

generatoriteratorgeneratorsrewindable

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bentools-rewindable-generator/health.svg)

```
[![Health](https://phpackages.com/badges/bentools-rewindable-generator/health.svg)](https://phpackages.com/packages/bentools-rewindable-generator)
```

###  Alternatives

[symfony/maker-bundle

Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

3.4k116.2M677](/packages/symfony-maker-bundle)[nikic/iter

Iteration primitives using generators

1.1k6.2M51](/packages/nikic-iter)[simplesoftwareio/simple-qrcode

Simple QrCode is a QR code generator made for Laravel.

2.9k29.5M106](/packages/simplesoftwareio-simple-qrcode)[loophp/collection

A (memory) friendly, easy, lazy and modular collection class.

743730.3k15](/packages/loophp-collection)[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4531.2M5](/packages/athari-yalinqo)[ihor/nspl

Non-standard PHP library (NSPL) - functional primitives toolbox and more

375369.1k](/packages/ihor-nspl)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
