phpbotgram

Setup
in package

FinalYes

Wires phpbotgram into an **existing** `amphp/http-server` app.

Port of setup_application from aiogram/webhook/aiohttp_server.py:22-40.

def setup_application(app, dispatcher, /, **kwargs):
    workflow_data = {"app": app, "dispatcher": dispatcher,
                     **dispatcher.workflow_data, **kwargs}

    async def on_startup(*a, **kw):
        await dispatcher.emit_startup(**workflow_data)

    async def on_shutdown(*a, **kw):
        await dispatcher.emit_shutdown(**workflow_data)

    app.on_startup.append(on_startup)
    app.on_shutdown.append(on_shutdown)

Router deviation

amphp/http-server-router is not a project dependency, so register() accepts a callable(string, RequestHandler): void instead of a concrete Router type. The callable is forwarded to BaseRequestHandler::register(), which calls it with the path and the handler. Typical usage with amphp/http-server-router:

Setup::register(
    server: $server,
    registerRoute: fn(string $p, RequestHandler $h) => $router->addRoute('POST', $p, $h),
    dispatcher: $dispatcher,
    handler: $handler,
    path: '/webhook',
);

Caller owns the server lifecycle

register() does not call expose() or start(). The caller retains full control of the HttpServer lifecycle — this method only attaches the route and lifecycle hooks.

Table of Contents

Methods

register()  : void
Wire a `Dispatcher` + `BaseRequestHandler` into an existing `HttpServer`.

Methods

register()

Wire a `Dispatcher` + `BaseRequestHandler` into an existing `HttpServer`.

public static register(HttpServer $server, callable(string, RequestHandler): void $registerRoute, Dispatcher $dispatcher, BaseRequestHandler $handler[, string $path = '/webhook' ][, array<string, mixed> $workflowData = [] ]) : void
  • Registers the handler's POST route via $registerRoute.
  • Attaches $dispatcher->emitStartup() to $server->onStart().
  • Attaches $dispatcher->emitShutdown() + $handler->close() to $server->onStop().
Parameters
$server : HttpServer

The server to register hooks on.

$registerRoute : callable(string, RequestHandler): void

Callback that registers a POST route for the given path and handler.

$dispatcher : Dispatcher

Dispatcher whose lifecycle observers are wired to server start/stop.

$handler : BaseRequestHandler

Webhook handler to register.

$path : string = '/webhook'

URL path (e.g. '/webhook').

$workflowData : array<string, mixed> = []

Extra kwargs forwarded to the dispatcher startup/shutdown emitters.

On this page

Search results