phpbotgram

FsmContextMiddleware extends BaseMiddleware
in package

FinalYes

Dispatcher-side middleware that resolves the FSM context for each incoming update and injects `state`, `raw_state`, and `fsm_storage` into the kwarg bag so that handlers and filters can access FSM state via reflection binding.

Mirrors aiogram.fsm.middleware.FSMContextMiddleware (aiogram/fsm/middleware.py).

Upstream bugfix (https://github.com/aiogram/aiogram/issues/1317): the raw state is loaded after the event-isolation lock is acquired, not before. This prevents two concurrent fibers from reading a stale state value before the other fiber has finished updating it.

Key conventions (snake_case matching upstream wire keys):

  • STATE_KEY = 'state'
  • RAW_STATE_KEY = 'raw_state'
  • FSM_STORAGE_KEY = 'fsm_storage'

Table of Contents

Constants

FSM_STORAGE_KEY  : string = 'fsm_storage'
RAW_STATE_KEY  : string = 'raw_state'
STATE_KEY  : string = 'state'

Properties

$storage  : BaseStorage
$eventsIsolation  : BaseEventIsolation
$strategy  : FsmStrategy

Methods

__construct()  : mixed
__invoke()  : mixed
close()  : void
Release all resources held by both the storage and the event-isolation strategy.
getContext()  : FsmContext
Construct a fresh `FsmContext` bound to an explicit `StorageKey`.
resolveContext()  : FsmContext|null
Build an `FsmContext` from raw ids, applying the configured strategy.
resolveEventContext()  : FsmContext|null
Derive the FsmContext for the current update by reading the EventContext that `UserContextMiddleware` has already placed in `$data`.

Constants

Properties

Methods

__invoke()

public __invoke(callable(object, array<string, mixed>): mixed $handler, object $event, array<string, mixed> $data) : mixed
Parameters
$handler : callable(object, array<string, mixed>): mixed
$event : object
$data : array<string, mixed>

close()

Release all resources held by both the storage and the event-isolation strategy.

public close() : void

Mirrors FSMContextMiddleware.close (middleware.py:115-117).

getContext()

Construct a fresh `FsmContext` bound to an explicit `StorageKey`.

public getContext(Bot $bot, int $chatId, int $userId[, int|null $threadId = null ][, string|null $businessConnectionId = null ][, string $destiny = StorageKey::DEFAULT_DESTINY ]) : FsmContext

Mirrors FSMContextMiddleware.get_context (middleware.py:94-113).

Parameters
$bot : Bot
$chatId : int
$userId : int
$threadId : int|null = null
$businessConnectionId : string|null = null
$destiny : string = StorageKey::DEFAULT_DESTINY

Destiny tag; defaults to StorageKey::DEFAULT_DESTINY.

Return values
FsmContext

resolveContext()

Build an `FsmContext` from raw ids, applying the configured strategy.

public resolveContext(Bot $bot, int|null $chatId, int|null $userId[, int|null $threadId = null ][, string|null $businessConnectionId = null ][, string $destiny = StorageKey::DEFAULT_DESTINY ]) : FsmContext|null

Mirrors FSMContextMiddleware.resolve_context (middleware.py:62-92).

Fallback rules (upstream parity):

  1. If $chatId is null, use $userId as a substitute.
  2. If $userId is null and the strategy is Chat or ChatTopic, fall back to $chatId for the user slot.
  3. If both are null after fallbacks, return null.
Parameters
$bot : Bot
$chatId : int|null
$userId : int|null
$threadId : int|null = null
$businessConnectionId : string|null = null
$destiny : string = StorageKey::DEFAULT_DESTINY

Destiny tag; defaults to StorageKey::DEFAULT_DESTINY.

Return values
FsmContext|null

resolveEventContext()

Derive the FsmContext for the current update by reading the EventContext that `UserContextMiddleware` has already placed in `$data`.

public resolveEventContext(Bot $bot, array<string, mixed> $data[, string $destiny = StorageKey::DEFAULT_DESTINY ]) : FsmContext|null

Falls through to resolveContext with the appropriate ids.

Parameters
$bot : Bot
$data : array<string, mixed>
$destiny : string = StorageKey::DEFAULT_DESTINY
Return values
FsmContext|null
On this page

Search results