Router
in package
Chooses the handler of a connection from the path of its handshake.
The path is read once, when the client connects: a client that sends a
handshake on /php stays on the internal channel for the whole connection,
it cannot ask for the API on the next frame. One connection, one contract.
No handler means a refusal, never a fallback to something permissive: an unknown path is answered with a close 1008 by the caller.
Tags
Table of Contents
Methods
- add() : void
- Registers the handler of one path.
- handler() : callable|null
- Returns the handler of a handshake target, or null when the path is unknown.
- path() : string
- Normalises a request target to a path: '/?page=2' and '/?x' are '/', '/php?x=1' is '/php', and a trailing slash never creates a second route.
- set_fallback() : void
- Registers the handler used when no path matched (the API bridge, normally).
Methods
add()
Registers the handler of one path.
public
add(string $path, callable $handler) : void
Parameters
- $path : string
-
'/php', '/admin'…
- $handler : callable
-
function (Connection $connection, string $payload): void
handler()
Returns the handler of a handshake target, or null when the path is unknown.
public
handler(string $target) : callable|null
Parameters
- $target : string
-
Request target of the handshake, query string included.
Return values
callable|nullpath()
Normalises a request target to a path: '/?page=2' and '/?x' are '/', '/php?x=1' is '/php', and a trailing slash never creates a second route.
public
static path(string $target) : string
Parameters
- $target : string
Return values
stringset_fallback()
Registers the handler used when no path matched (the API bridge, normally).
public
set_fallback(callable $handler) : void
Parameters
- $handler : callable