PHPackages                             jced-artem/leak-safe-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. jced-artem/leak-safe-generator

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

jced-artem/leak-safe-generator
==============================

Generator wrapper to prevent memory leaks

1.0.0(9y ago)016MITPHPPHP &gt;=5.5.0

Since Dec 13Pushed 9y ago1 watchersCompare

[ Source](https://github.com/jced-artem/leak-safe-generator)[ Packagist](https://packagist.org/packages/jced-artem/leak-safe-generator)[ Docs](https://github.com/jced-artem)[ RSS](/packages/jced-artem-leak-safe-generator/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Leak safe PHP generator wrapper
===============================

[](#leak-safe-php-generator-wrapper)

### Install

[](#install)

`composer require jced-artem/leak-safe-generator`

If you want to prevent memory leaks when you use generators with handlers and you bored to use `try..finally` constructions - this class could help.

### Example

[](#example)

```
function parsePages() {
    $ch = curl_init();
    // ... some options
    while ($condition) {
        yield curl_exec($ch);
    }
    curl_close($ch);
}

foreach (parsePages() as $page) {
    if ($someCondition) {
        break;
    }
    // ...
}

```

### Problem

[](#problem)

If you break loop before get all yields you will have not closed handler `$ch` and this can cause memory leaks.

### Solution

[](#solution)

```
function parsePages() {
    $ch = curl_init();
    // ... some options
    try {
        $finished = false;
        while ($condition) {
            yield curl_exec($ch);
        }
        $finished = true;
    } finally {
        // close anyway
        curl_close($ch);
        if ($finished) {
            // do something if reached last element
        } else {
            // do something on break
        }
    }
}

```

### New problem

[](#new-problem)

This code isn't usable if you make several generators

### New solution

[](#new-solution)

```
$lsg = new LeakSafeGenerator();
$lsg
    ->init(function() {
        $this->ch = curl_init();
        // ... some options
        while ($condition) {
            yield curl_exec($this->ch);
        }
    })
    ->onInterrupt(function() {
        // do something on break
    })
    ->onComplete(function() {
        // do something if reached last element
    })
    ->onFinish(function() {
        curl_close($this->ch);
    })
;
foreach ($lsg->getGenerator() as $page) {
    if ($someCondition) {
        break;
    }
    // ...
}

```

or shorter version

```
$lsg = new LeakSafeGenerator(
    function() {
        $this->ch = curl_init();
        // ... some options
        while ($condition) {
            yield curl_exec($this->ch);
        }
    },
    function() {
        curl_close($this->ch);
    }
);

```

And you don't need to create additional functions/methods and write `try..finally` constructions for each generator.

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

3486d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/694933bfd2d85dd295280dff0c339061f3652f3c65ad2a5427e429462b33e295?d=identicon)[jced\_artem](/maintainers/jced_artem)

---

Top Contributors

[![jced-artem](https://avatars.githubusercontent.com/u/1507915?v=4)](https://github.com/jced-artem "jced-artem (8 commits)")

---

Tags

phpgeneratormemorywrapperleak

### Embed Badge

![Health badge](/badges/jced-artem-leak-safe-generator/health.svg)

```
[![Health](https://phpackages.com/badges/jced-artem-leak-safe-generator/health.svg)](https://phpackages.com/packages/jced-artem-leak-safe-generator)
```

###  Alternatives

[tomloprod/radiance

A deterministic mesh gradient avatar generator for PHP.

1407.5k](/packages/tomloprod-radiance)[samrap/gestalt

Gestalt is a simple, elegant PHP package for managing your framework's configuration values.

163.6k3](/packages/samrap-gestalt)

PHPackages © 2026

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