switon/context

Default context manager for request- and coroutine-scoped service-owned state for Switon Framework

Maintainers

Package info

github.com/switon-php/context

Documentation

pkg:composer/switon/context

Statistics

Installs: 2

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-06 13:43 UTC

This package is auto-updated.

Last update: 2026-06-07 03:09:36 UTC


README

CI PHP 8.3+

Switon's runtime state manager for ContextAware components, including HTTP request isolation and coroutine sharing.

Highlights

  • Request isolation: mutable state stays out of long-lived services.
  • Automatic context binding: getContext() resolves the matching context for each component.
  • Coroutine sharing: child coroutines can inherit or reset context as needed.
  • Lifecycle events: child-context creation and propagation are observable.
  • Scoped runtime state: ContextManager keeps the active context available for the current request or coroutine.

Installation

composer require switon/context

Quick Start

use Switon\Core\Attribute\Autowired;
use Switon\Core\ContextAware;
use Switon\Core\ContextManagerInterface;

final class RequestStateContext
{
    public array $vars = [];
}

final class RequestState implements ContextAware
{
    #[Autowired] protected ContextManagerInterface $contextManager;

    public function getContext(): RequestStateContext
    {
        return $this->contextManager->getContext($this);
    }

    public function remember(string $key, mixed $value): void
    {
        $this->getContext()->vars[$key] = $value;
    }
}

Docs: https://docs.switon.dev/latest/context

License

MIT.