Methods43
Register a listener directly (as opposed to ConnectListeners's attribute-based discovery).
static function AddListener(callable $handler, int $order = 0): void| $handler | callable | called as $handler($eventInstance) when this event class is Send / dispatch-ed |
| $order = 0 | int | listeners run in ascending order (lower first); listeners with an equal order run in the order they were added |
| returns | void | |
UserCreated::AddListener(function (UserCreated $e) {...}, 10);
Scan $obj's methods for #[Listen(EventClass::class)] attributes and register each one as a listener on the matching event class. connect does the actual reflection/registration.
static function ConnectListeners(object|string $obj): void| $obj | object | string | an instance (or class name, for static-only listener methods) |
| returns | void | |
| throws | | |
class UserService {
#[Listen(UserCreated::class, order: 1)]
public function onUserCreated(UserCreated $e): void {...}
}
UserCreated::ConnectListeners(new UserService()); // onUserCreated() is now called on every UserCreated::Send()
Undo a previous ConnectListeners: removes every listener that connect had registered for $obj on this event class.
static function DisconnectListeners(object|string $obj): voidUserCreated::DisconnectListeners($service); // $service's #[Listen] methods stop firing
Invoke every GetListeners listener with $this. If the class also implements PSR-14's StoppableEventInterface, dispatch stops early as soon as isPropagationStopped() becomes true.
function dispatch(): static| returns | static | $this, for chaining |
(new UserCreated('Marc'))->dispatch(); // what Send() does internally
function getCharset(): ?string| returns | null | string | the charset parameter of the Content-Type header, or null if not present |
$msg->setContentType('text/html'); $msg->getCharset(); // 'utf-8' (the default charset)
function getContentExtension(): ?string| returns | null | string | the file extension conventionally associated with the Content-Type header (e.g. 'html', 'json') |
$msg->setContentType('text/html'); $msg->getContentExtension(); // 'html'
function getContentType(bool $single = true): array|string| $single = true | bool | if true, return only the media type (e.g. 'text/html'); if false, every parameter too |
| returns | array | string | |
$msg->setContentType('text/html'); $msg->getContentType(); // 'text/html'
Collect every listener for this class, including those registered on its parent classes (so a subclass event is also caught by listeners of its parent event), sorted by ascending $order (ties keep insertion order).
static function GetListeners(): array| returns | array | [[callable $handler, int $order], ...] |
UserCreated::GetListeners(); // [[$handlerB, 5], [$handlerA, 10]] -- lower order first
Get origin of the request by looking at 'Origin', then 'Referer', then 'Sender' in the request's headers.
function getOrigin(): string| returns | string | '' if none of those headers are set |
$msg->setHeader('Origin', 'https://example.com'); $msg->getOrigin(); // 'https://example.com'
abstract function getParsedBody(): ArrayAccess|array|nullRetrieves the HTTP protocol version as a string.
function getProtocolVersion(): stringThe string MUST contain only the HTTP version number (e.g., "1.1", "1.0").| returns | string | HTTP protocol version. |
$msg->getProtocolVersion(); // '1.0'
function getUserAgent(): ?stringstatic function HasListener(callable|object $handler): bool| $handler | callable | object | either the exact callable passed to AddListener, or (for a [$obj, $methodName] listener registered via ConnectListeners) just the object, to check whether any of its methods are currently listening |
| returns | bool | |
UserCreated::HasListener($handler); // true if $handler is currently registeredUserCreated::HasListener($service); // true if any of $service's methods were connected via #[Listen]
function isHTML(): boolfunction isIe(): bool| returns | bool | true if the User-Agent looks like Internet Explorer |
function isIphone(): bool| returns | bool | true if the User-Agent looks like mobile Safari on iPhone |
function isJSON(): boolfunction isMac(): bool| returns | bool | true if the User-Agent mentions macOS |
function isMobile(): bool| returns | bool | true if the User-Agent looks like a mobile device/browser |
function isWindows(): bool| returns | bool | true if the User-Agent mentions Windows |
Remove the last added handler from the listeners.
static function RemoveListener(callable $handler): boolNOTE: if you added the same handler multiple time, you need to call this function the same amount of times.Construct a new instance of this class with $args, then dispatch it to every listener.
static function Send(mixed ...$args): static| ...$args | mixed | |
| returns | static | the constructed (and dispatched) event instance |
UserCreated::Send('Marc'); // equivalent to (new UserCreated('Marc'))->dispatch()
Set the Cache-Control header (and, optionally, the legacy Pragma header alongside it).
protected function setCacheControl($cache, $pragma = null): self| $cache | | cache method 'public', 'private', 'no-cache', 'no-store' |
| $pragma = null | | if given, also sets the Pragma header (e.g. 'no-cache', for HTTP/1.0 clients) |
| returns | self | |
$msg->setCacheControl('no-cache', 'no-cache');
Set the Content-Type header, appending a charset parameter (unless $charset is null/empty).
protected function setContentType(string $type, ?string $charset = 'utf-8'): self| $type | string | the media type, e.g. 'text/html' or one of the framework's MIME_* constants |
| $charset = 'utf-8' | null | string | appended as ;charset=$charset; pass null/'' to omit it entirely |
| returns | self | |
$msg->setContentType('text/html'); $msg->getHeaderLine('Content-Type'); // 'text/html;charset=utf-8'$msg->setContentType('application/octet-stream', null); // no charset appended
Set the Expires header from a unix timestamp, formatted as an RFC 7231 HTTP-date.
protected function setExpires(?int $time): self| $time | null | int | a unix timestamp; 0 (or any past time) tells clients/caches the response is already stale |
| returns | self | |
$msg->setExpires(0); $msg->getHeaderLine('Expires'); // 'Thu, 01 Jan 1970 00:00:00 GMT'
Change the order of an already-registered listener, whether added directly via AddListener or indirectly via ConnectListeners (which also stores its [$obj, $methodName] callables here).
static function SetListenerOrder(callable $handler, int $order): bool| $handler | callable | |
| $order | int | |
| returns | bool | true if $handler was found and its order changed |
UserCreated::SetListenerOrder($handler, 20); // moves $handler later in the dispatch order
Set cache control of a clone.
function withCacheControl($cache, $pragma = null): self| $cache | | cache method 'public', 'private', 'no-cache', 'no-store' |
| $pragma = null | | |
| returns | self | |
$msg->withCacheControl('no-cache', 'no-cache');
Set content type of a clone.
function withContentType(string $type, ?string $charset = 'utf-8'): self$msg->withContentType('application/json');
Set cache expire date of a clone.
function withExpires($time): self| $time | | a unix timestamp |
| returns | self | |
$msg->withExpires(0); // clone's Expires header is set to the epoch (already stale)
abstract function withParsedBody(ArrayAccess|array|null $data): selfReturn an instance with the specified HTTP protocol version.
function withProtocolVersion($version): selfThe version string MUST contain only the HTTP version number (e.g.,
"1.1", "1.0").
This method MUST be implemented in such a way as to retain the
immutability of the message, and MUST return an instance that has the
new protocol version.| $version | | HTTP protocol version |
| returns | self | |
$msg->withProtocolVersion('1.1');
function __construct(array $headers = [])| $headers = [] | array | name => value, set directly on this (not-yet-shared) instance -- unlike withHeader, no cloning happens here since the instance doesn't exist yet |