FlagDecorator
in package
Imperative attachment seam for flags. Mirrors the runtime half of `aiogram.dispatcher.flags.FlagDecorator.__call__` — the branch that mutates the callback by setting `value.aiogram_flag = {...}`.
Python can stamp attributes directly onto a function object. PHP cannot —
\Closure instances reject ad-hoc property assignment. We instead key a
process-wide WeakMap<\Closure|object, list<Flag>> by the target. When the
target is garbage-collected, its entry evicts automatically — no manual
cleanup needed even when the dispatcher rebuilds its handler graph.
Note that WeakMap only accepts objects; that's why the signature is
\Closure|object rather than callable. Callers with a string-callable or
array-callable should lift it via \Closure::fromCallable($cb) first.
The class is process-static by design — there's only one logical map of
"flag attachments" per process. The reset() seam exists exclusively for
test isolation; production code never calls it.
Table of Contents
Properties
- $storage : null|WeakMap<object, array<int, Flag>>
- Lazily-initialised on first access. Static null-coalesce assignment in `storage()` keeps the bootstrap branch small and avoids a constructor.
Methods
- attach() : object
- Attach a flag to a closure or object. The target is returned unchanged so registration sites can chain: `$cb = FlagDecorator::attach($cb, new Flag(...));`.
- attached() : array<int, Flag>
- Flags attached to `$target` via `attach()`, in attachment order. Does NOT include attribute-driven flags — for the combined view use `Flags::extractFlags()`.
- storage() : WeakMap<object, array<int, Flag>>
- Lazy accessor for the shared WeakMap. The `??=` operator initialises on first read and short-circuits afterwards, matching the singleton pattern upstream uses for module-level dicts.
Properties
$storage
Lazily-initialised on first access. Static null-coalesce assignment in `storage()` keeps the bootstrap branch small and avoids a constructor.
private
static null|WeakMap<object, array<int, Flag>>
$storage
= null
Methods
attach()
Attach a flag to a closure or object. The target is returned unchanged so registration sites can chain: `$cb = FlagDecorator::attach($cb, new Flag(...));`.
public
static attach(object $target, Flag $flag) : object
Multiple attaches accumulate; storage is keyed by object identity, so two
different closures (even with identical source) get independent flag
lists. The object parameter type accepts \Closure since closures are
objects — a redundant \Closure|object union is rejected by PHP 8.5.
Parameters
- $target : object
- $flag : Flag
Return values
objectattached()
Flags attached to `$target` via `attach()`, in attachment order. Does NOT include attribute-driven flags — for the combined view use `Flags::extractFlags()`.
public
static attached(object $target) : array<int, Flag>
Parameters
- $target : object
Return values
array<int, Flag>storage()
Lazy accessor for the shared WeakMap. The `??=` operator initialises on first read and short-circuits afterwards, matching the singleton pattern upstream uses for module-level dicts.
private
static storage() : WeakMap<object, array<int, Flag>>