Switon's runtime state manager for ContextAware components, including HTTP request isolation and coroutine sharing.
- 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.
composer require switon/contextuse 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
MIT.