switon / context
Default context manager for request- and coroutine-scoped service-owned state for Switon Framework
v1.0.0
2026-06-06 13:43 UTC
Requires
- php: >=8.3
- psr/event-dispatcher: ^1.0
- switon/core: ^1.0
- switon/event: ^1.0
Requires (Dev)
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.0
- switon/testing: ^1.0
README
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:
ContextManagerkeeps 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.