API Context Context - evansims/openfga-php GitHub Wiki

Ambient Context Manager. Provides Python-style context management for PHP, allowing functions to access shared context without explicit parameter passing. This enables a more ergonomic API where client, store, and model can be set once and used implicitly by helper functions. Contexts support inheritance - child contexts automatically inherit values from their parent context unless explicitly overridden. This allows for flexible nesting where you can override just the pieces you need.

Table of Contents


Namespace

OpenFGA\Context


Source

View source code


Implements


Related Classes


Methods

current

Implements Context\ContextInterface

public function current(): self

Get the current ambient context.

View source


Returns

self


depth

Implements Context\ContextInterface

public function depth(): int

Get the current nesting depth of contexts.

View source


Returns

int


getClient

Implements Context\ContextInterface

public function getClient(): ?OpenFGA\ClientInterface

Get the current client.

View source


Returns

ClientInterface | null


getModel

Implements Context\ContextInterface

public function getModel(): ?OpenFGA\Models\AuthorizationModelInterface|string|null

Get the current authorization model.

View source


Returns

AuthorizationModelInterface | null | string | null


getPrevious

Implements Context\ContextInterface

public function getPrevious(): ?self

Get the previous context in the stack.

View source


Returns

self | null


getStore

Implements Context\ContextInterface

public function getStore(): ?OpenFGA\Models\StoreInterface|string|null

Get the current store.

View source


Returns

StoreInterface | null | string | null


hasContext

Implements Context\ContextInterface

public function hasContext(): bool

Check if an ambient context is currently active.

View source


Returns

bool


with

Implements Context\ContextInterface

public function with(
    callable $fn,
    ?OpenFGA\ClientInterface $client = NULL,
    StoreInterface|string|null $store = NULL,
    AuthorizationModelInterface|string|null $model = NULL,
): T

Execute a callable within a new ambient context.

View source


Parameters

Name Type Description
$fn callable
$client ClientInterface | null Optional client for the context
$store StoreInterface | string | null Optional store for the context
$model AuthorizationModelInterface | string | null Optional model for the context

Returns

T — The result of the callable execution

⚠️ **GitHub.com Fallback** ⚠️