PHPackages                             leocavalcante/coroutine-context-api - 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. leocavalcante/coroutine-context-api

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

leocavalcante/coroutine-context-api
===================================

Using Coroutines Contexts as Hierarchic Service Locators and Dependency Injection Containers.

v0.1.0(4y ago)4879MITPHPPHP &gt;=7.4 || &gt;=8.0

Since Oct 11Pushed 4y ago1 watchersCompare

[ Source](https://github.com/leocavalcante/coroutine-context-api)[ Packagist](https://packagist.org/packages/leocavalcante/coroutine-context-api)[ RSS](/packages/leocavalcante-coroutine-context-api/feed)WikiDiscussions main Synced 4d ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Coroutine Context API [![CI](https://github.com/leocavalcante/coroutine-context-api/actions/workflows/main.yml/badge.svg)](https://github.com/leocavalcante/coroutine-context-api/actions/workflows/main.yml)
=============================================================================================================================================================================================================

[](#coroutine-context-api-)

Using Coroutines Contexts as Hierarchic Service Locators and Dependency Injection Containers.

> Inspired by

Install
-------

[](#install)

```
composer require leocavalcante/coroutine-context-api
```

Usage
-----

[](#usage)

### Provide

[](#provide)

```
\Swoole\Coroutine\Context\provide(string $key, mixed $value): void
```

Sets a value to be consumed from children Coroutines based on a string.

```
use function Swoole\Coroutine\{run, Context\provide};

run(static function(): void {
    provide('message', 'Hello, World!');
});
```

### Consume

[](#consume)

```
\Swoole\Coroutine\Context\consume(string $key, [mixed $default]): mixed
```

Consumes the value from the given key.

```
use function Swoole\Coroutine\{run, Context\provide, Context\consume};

run(static function(): void {
    provide('message', 'Hello, World!');
    go(static fn() => print(consume('message') . PHP_EOL));
});
```

### But, but...

[](#but-but)

#### Why is this different from passing parameters to function arguments?

[](#why-is-this-different-from-passing-parameters-to-function-arguments)

The `consume` function can lookup through the nearest provided key in the Coroutine tree.

```
use function Swoole\Coroutine\{run, Context\provide, Context\consume};

run(static function(): void {
    provide('message', 'Hello, World!');
    go(static fn() =>
        go(static fn() =>
            go(static fn() =>
                print(consume('message') . PHP_EOL)
            )
        )
    );
});
```

#### Why is this different from globals?

[](#why-is-this-different-from-globals)

It is not about global space being polluted, it is based on parent-child "Coroutine tree".

```
run(static function(): void {
    provide('message', 'Hello, World!');
    go(static fn() => print(consume('message') . PHP_EOL));
});

run(static function(): void {
    provide('message', 'Olá, Mundo!');
    go(static fn() => print(consume('message') . PHP_EOL));
});

run(static function(): void {
    go(static fn() => print(consume('message', $default = '你好, 世界!') . PHP_EOL));
});
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

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

1678d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/940717934113da7abe00589e87cfde6f34496f039bcd3fc6ce4a33f5f415d4ac?d=identicon)[leocavalcante](/maintainers/leocavalcante)

---

Top Contributors

[![leocavalcante](https://avatars.githubusercontent.com/u/183722?v=4)](https://github.com/leocavalcante "leocavalcante (16 commits)")

---

Tags

contextcoroutinesdependency-injectionhacktoberfestservice-locatorswoole

###  Code Quality

TestsPest

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/leocavalcante-coroutine-context-api/health.svg)

```
[![Health](https://phpackages.com/badges/leocavalcante-coroutine-context-api/health.svg)](https://phpackages.com/packages/leocavalcante-coroutine-context-api)
```

PHPackages © 2026

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