phpbotgram

MemoryStorage extends BaseStorage
in package

FinalYes

In-memory FSM storage backend.

Mirrors aiogram.fsm.storage.memory.MemoryStorage (aiogram/fsm/storage/memory.py:25-72). All state and data are held in a plain PHP array keyed by a derived string; the array is reset on close().

WARNING: All data is lost when the PHP process exits. Do not use in production if state persistence across restarts is required.

Key derivation: identical to SimpleEventIsolation::buildKey()<botId>:<chatId>:<threadId|"">:<userId>:<businessConnectionId|"">:<destiny>.

Python defaultdict equivalent: record() auto-vivifies a fresh MemoryStorageRecord on first access for any key.

Table of Contents

Properties

$storage  : array<string, MemoryStorageRecord>
In-memory record store. Keyed by the derived string returned from `keyOf`.

Methods

close()  : void
Release all storage records.
getData()  : array<string, mixed>
Retrieve a COPY of the FSM data payload for the given key.
getState()  : null|string
Retrieve the FSM state for the given key.
getValue()  : mixed
Read a single key from the data map stored at `$storageKey`.
setData()  : void
Persist the FSM data payload for the given key (replaces the record entirely).
setState()  : void
Persist the FSM state for the given key.
updateData()  : array<string, mixed>
Merge `$data` into the existing record at `$key` and persist the result.
keyOf()  : string
Derive a stable string key from all `StorageKey` fields.
record()  : MemoryStorageRecord
Auto-vivify and return the `MemoryStorageRecord` for `$key`.

Properties

Methods

close()

Release all storage records.

public close() : void

Mirrors MemoryStorage.close (memory.py:41-42). The upstream implementation is a no-op coroutine; here we reset the internal array so that memory is freed promptly.

getData()

Retrieve a COPY of the FSM data payload for the given key.

public getData(StorageKey $key) : array<string, mixed>

PHP arrays are value-typed, so returning $record->data already yields an independent copy — mutations to the returned array do not bleed back into storage. This matches upstream memory.py:57:

return self.storage[key].data.copy()

Mirrors MemoryStorage.get_data (memory.py:56-57).

Parameters
$key : StorageKey

Storage address.

Return values
array<string, mixed>

Current data map (may be empty).

Mirrors BaseStorage.get_data (base.py:143-146).

getState()

Retrieve the FSM state for the given key.

public getState(StorageKey $key) : null|string

Mirrors MemoryStorage.get_state (memory.py:47-48).

Parameters
$key : StorageKey

Storage address.

Return values
null|string

Serialised state name, or null if no state is stored.

Mirrors BaseStorage.get_state (base.py:132-135).

getValue()

Read a single key from the data map stored at `$storageKey`.

public getValue(StorageKey $storageKey, string $dictKey[, mixed $default = null ]) : mixed

Returns $default when the dict key is absent. PHP collapses the upstream @overload pattern into one method with a mixed $default = null parameter.

Parameters
$storageKey : StorageKey

Storage address.

$dictKey : string

Key within the data map.

$default : mixed = null

Value to return when $dictKey is not present.

Return values
mixed

The stored value, or $default.

Mirrors BaseStorage.get_value (base.py:151-168).

setData()

Persist the FSM data payload for the given key (replaces the record entirely).

public setData(StorageKey $key, array<string|int, mixed> $data) : void

PHP arrays are value-typed, so $data is implicitly copied on assignment — no explicit $data->copy() is needed, matching upstream memory.py:54:

self.storage[key].data = data.copy()

Mirrors MemoryStorage.set_data (memory.py:50-54).

Parameters
$key : StorageKey

Storage address.

$data : array<string|int, mixed>

Data map to store (replaces the current record entirely).

Mirrors BaseStorage.set_data (base.py:137-141). Python Mapping[str, Any] → PHP array<string, mixed>.

setState()

Persist the FSM state for the given key.

public setState(StorageKey $key[, State|string|null $state = null ]) : void

When $state is a State instance, $state->state() is called to obtain the fully-qualified state string. When it is a plain string or null, the value is stored as-is.

Mirrors MemoryStorage.set_state (memory.py:44-45):

self.storage[key].state = state.state if isinstance(state, State) else state
Parameters
$key : StorageKey

Storage address.

$state : State|string|null = null

New state value. string — a raw state name already serialised. State — a State instance; implementations call $state->state() to obtain the qualified name. null — clears the state.

Mirrors BaseStorage.set_state (base.py:127-130).

updateData()

Merge `$data` into the existing record at `$key` and persist the result.

public updateData(StorageKey $key, array<string, mixed> $data) : array<string, mixed>

Loads the current data map, merges the supplied patch on top with array_merge (later keys win on collision), persists the merged map, and returns a copy so that mutations to the returned array cannot bleed back into storage.

Parameters
$key : StorageKey

Storage address.

$data : array<string, mixed>

Partial data map to merge in.

Return values
array<string, mixed>

A copy of the merged data map as it was persisted.

Mirrors BaseStorage.update_data (base.py:170-181). Upstream returns current_data.copy() after in-place update.

keyOf()

Derive a stable string key from all `StorageKey` fields.

private keyOf(StorageKey $key) : string

Format: <botId>:<chatId>:<threadId|"">:<userId>:<businessConnectionId|"">:<destiny>

Mirrors the key derivation used by SimpleEventIsolation::buildKey().

Parameters
$key : StorageKey
Return values
string
On this page

Search results