phpbotgram

EventObserver
in package

FinalYes

Simple events observer used for managing events that are **not** related to Telegram updates — primarily startup/shutdown lifecycle hooks (mirrors upstream `aiogram.dispatcher.event.event.EventObserver`). Update routing is handled separately by `TelegramEventObserver` (filters + middlewares + handler results), which is intentionally a different class.

Handlers are wrapped in CallableObject at registration time, so reflection runs once per handler instead of per trigger() call. The dispatcher passes a kwargs bag (bot, event_context, state, …) and each handler declares only the keys it cares about — CallableObject::call() filters the map down to the declared parameter names before forwarding.

Decorator-style usage (aiogram's @observer() form) is not mirrored: PHP lacks Python-style decorators, so callers use the imperative register($callback) form. The method returns the callback unchanged to preserve the assignment ergonomics:

$callback = $observer->register($callback);

Table of Contents

Properties

$handlers  : array<int, CallableObject>

Methods

clear()  : void
Drop all registered handlers. After `clear()`, `trigger()` is a no-op until new handlers are registered.
handlers()  : array<int, CallableObject>
Expose the registered handlers (wrapped as `CallableObject` instances) for inspection by tests and introspection code. Production dispatch always goes through `trigger()`.
register()  : Closure
Register a handler. Returns the **original** callback unchanged so the registration call site can keep a reference to the same closure (parity with upstream `register()` which appends to `self.handlers` and returns the callback in the decorator path). Internally the closure is wrapped in a `CallableObject` for cached reflection-based kwarg binding.
trigger()  : void
Synchronous fan-out. Each handler is invoked in registration order with `$args` forwarded positionally and `$kwargs` filtered down to the parameter names that handler actually declares. No return values are consumed: lifecycle observers are pub/sub. If a handler throws, the exception propagates and later handlers do **not** run (mirrors upstream's lack of try/except in `EventObserver.trigger`).

Properties

Methods

clear()

Drop all registered handlers. After `clear()`, `trigger()` is a no-op until new handlers are registered.

public clear() : void

handlers()

Expose the registered handlers (wrapped as `CallableObject` instances) for inspection by tests and introspection code. Production dispatch always goes through `trigger()`.

public handlers() : array<int, CallableObject>
Return values
array<int, CallableObject>

register()

Register a handler. Returns the **original** callback unchanged so the registration call site can keep a reference to the same closure (parity with upstream `register()` which appends to `self.handlers` and returns the callback in the decorator path). Internally the closure is wrapped in a `CallableObject` for cached reflection-based kwarg binding.

public register(Closure $callback) : Closure
Parameters
$callback : Closure
Return values
Closure

trigger()

Synchronous fan-out. Each handler is invoked in registration order with `$args` forwarded positionally and `$kwargs` filtered down to the parameter names that handler actually declares. No return values are consumed: lifecycle observers are pub/sub. If a handler throws, the exception propagates and later handlers do **not** run (mirrors upstream's lack of try/except in `EventObserver.trigger`).

public trigger([array<int, mixed> $args = [] ][, array<string, mixed> $kwargs = [] ]) : void
Parameters
$args : array<int, mixed> = []
$kwargs : array<string, mixed> = []
On this page

Search results