Aerys\Body class Body extends Amp\PromiseStream implements Amp\Promise

An API allowing responders to buffer or stream request entity bodies

Applications are invoked as soon as headers are received and before entity body data is parsed. The $request->body instance allows applications to await receipt of the full body (buffer) or stream it in chunks as it arrives.

Buffering Example:

$responder = function(Request $request, Response $response) {
     $bufferedBody = yield $request->getBody();
     $response->send("Echoing back the request body: {$bufferedBody}");
};

Streaming Example:

$responder = function(Request $request, Response $response) {
     $payload = "";
     $body = $request->getBody()
     while (yield $body->valid()) {
         $payload .= $body->consume();
     }
     $response->send("Echoing back the request body: {$payload}");
};

Summary

Properties

Methods

Properties

Methods

__construct — public function __construct(Amp\Promise $promise)
No documentation.
Type Parameter Description Default Value
Amp\Promise $promise No documentation. none
when — public function when(callable $func, mixed $data = null)

Notify the $func callback when the promise resolves (whether successful or not)

Implementations MUST invoke the $func callback in error-first style, e.g.:

<?php
$promise->when(function(\Exception $error = null, $result = null) {
    if ($error) {
        // failed
    } else {
        // succeeded
    }
});

Implementations MUST return the current object instance.

Type Parameter Description Default Value
callable $func An error-first callback to invoke upon promise resolution none
mixed $data Optional data to pass as a third parameter to $func null
watch — public function watch(callable $func, mixed $data = null)

Notify the $func callback when resolution progress events are emitted

Implementations MUST invoke $func callback with a single update parameter, e.g.:

<?php
$promise->watch(function($update) { ... });

Implementations MUST return the current object instance.

Type Parameter Description Default Value
callable $func A callback to invoke when data updates are available none
mixed $data Optional data to pass as an additional parameter to $func null
valid — public function valid(): Amp\Promise

Will more data arrive on the stream?

Stream consumers should await the result of the valid() promise before consuming stream output.

consume — public function consume(): mixed

Consume buffered stream data