Index

StreamResponse

MMO\core\server · 1 symbol · generated 30 Sep 2026

class

StreamResponse

StreamResponse.php:17

Psr Response that reads a stream Used to retrieve files The application sends it as it reads it, a chunk at a time (render's output option): a download is not limited by php's memory.

class MMO\core\server\StreamResponse extends MMO\core\classes\aResponse
Namespace
MMO\core\server
Extends
aResponse
return StreamResponse::File($filename, [], 'Été 2025.pdf');

Index

CHUNK_SIZEthe size of what is read, then sent, at a time (render's output option)
stream
ContentDisposition($name, $inline) staticA Content-Disposition header for a file named $name: an ascii name for old clients, and the real one as RFC 6266 has it (filename*), so that a quote, an accent or a line break in a name cannot …
File($filename, $headers, $clientFilename) staticCreate a Stream response using a local file
getBody()Gets the body of the message.
getContentType($single)Prefers an explicitly-set 'Content-Type' header (tWithHeaders::getContentType); falls back to the stream's own detected type (Stream::getContentType) when no header was set and the stream…
getParsedBody()Get response body decoded.
JSON($filename, $clientFilename) staticCreate a Stream response for a local JSON file (attachment, must-revalidate caching).
lock()Close the underlying stream, releasing its resource.
logEvent($event)
output()Send the stream as it is read, a chunk at a time: from its start when it can seek, as render reads it.
render($opts)
withBody($body)Return an instance with the specified message body.
withParsedBody($data)
Zip($filename, $headers, $clientFilename) staticCreate a Stream response for a local file, forced to the 'zip' content type (File for the rest).
__construct($stream, $code, $reason, $headers)

Constants1

CHUNK_SIZE

the size of what is read, then sent, at a time (render's output option)

const CHUNK_SIZE = 1048576

Properties1

stream

protected Psr\Http\Message\StreamInterface $stream
Type
StreamInterface

Methods14

ContentDisposition($name, $inline)

staticStreamResponse.php:110

A Content-Disposition header for a file named $name: an ascii name for old clients, and the real one as RFC 6266 has it (filename*), so that a quote, an accent or a line break in a name cannot break the header.

static function ContentDisposition(string $name, bool $inline = false): string
$namestring
$inline = falseboolif true, the browser shows the file rather than saving it
returnsstring
StreamResponse::ContentDisposition('Été "2025".pdf'); // attachment; filename="_t_ _2025_.pdf"; filename*=UTF-8''%C3%89t%C3%A9%20%222025%22.pdf

File($filename, $headers, $clientFilename)

staticStreamResponse.php:52

Create a Stream response using a local file

static function File(string $filename, array $headers = [], ?string $clientFilename = null): self
$filenamestringthe local filename
$headers = []arrayheaders of the response
$clientFilename = nullnull | stringthe filename displayed for the user (by default it is the basename of filename)
returnsself

getBody()

StreamResponse.php:202

Gets the body of the message.

function getBody(): Psr\Http\Message\StreamInterface
returnsStreamInterfaceReturns the body as a stream.

getContentType($single)

StreamResponse.php:165

Prefers an explicitly-set 'Content-Type' header (tWithHeaders::getContentType); falls back to the stream's own detected type (Stream::getContentType) when no header was set and the stream is our

function getContentType(bool $single = true): array|string
See
Stream class. NOTE: like Stream::getContentType, this can fall through to return false (when no header is set AND the stream isn't a Stream instance, e.g. a bare third-party StreamInterface) even though the return type is declared string|array -- harmless in practice (PHP's weak typing coerces that false into '', and getParsedBody's only caller passes the result through extension(), which handles '' the same as any unrecognized type). UNLIKE Stream::getContentType, the type is NOT widened here: this overrides tWithHeaders::getContentType (via aMessage), and PHP requires an override's return type to be covariant (a subtype) of the parent's -- adding |false here is not a subtype of the parent's string|array and fails at class-load time (Fatal error: Declaration ... must be compatible with ...), confirmed by testing. So this one keeps the technically-inaccurate string|array declaration; this note documents the actual (occasionally-false) behavior instead.
$single = trueboolif true, return only the media type; if false, every parameter too.
returnsarray | string

getParsedBody()

StreamResponse.php:176

Get response body decoded.

function getParsedBody(): ArrayAccess|array|nullDecodes per the response's content type: JSON/CSV are parsed to arrays, HTML to [`XmlParser::Nodes`](https://mmoframework.fr/documentation/mmo.core/php/XmlParser.md#xmlparser-nodes), anything else is returned as a single-element array wrapping the raw content.
returnsArrayAccess | array | null
throws

JSON($filename, $clientFilename)

staticStreamResponse.php:88

Create a Stream response for a local JSON file (attachment, must-revalidate caching).

static function JSON(string $filename, ?string $clientFilename = null): self
$filenamestringthe local filename
$clientFilename = nullnull | stringthe filename displayed for the user (by default it is the basename of filename)
returnsself

logEvent($event)

StreamResponse.php:37
function logEvent(object $event): bool
$eventobject
returnsboolalways false -- a StreamResponse never triggers event logging.

output()

StreamResponse.php:136

Send the stream as it is read, a chunk at a time: from its start when it can seek, as render reads it.

protected function output(): void
returnsvoid

render($opts)

StreamResponse.php:127
function render(array $opts = []): string
See
iRenderable::render
$opts = []arrayoutput => true: the stream is sent as it is read, a chunk at a time (CHUNK_SIZE), rather than returned -- how Application::sendResponse sends it, so that a download is not limited by php's memory. Otherwise forwarded to the stream's own render() when it's our Stream class.
returnsstringthe stream's full content, or '' when it was sent (output).
$response->render(); // the whole content, read into a string$response->render(['output' => true]); // '': the content was echoed, a megabyte at a time

withBody($body)

StreamResponse.php:219

Return an instance with the specified message body.

function withBody(Psr\Http\Message\StreamInterface $body): selfThe body MUST be a StreamInterface object. This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return a new instance that has the new body stream.
$bodyStreamInterfaceBody.
returnsself
throwsWhen the body is not valid.

withParsedBody($data)

StreamResponse.php:190
function withParsedBody(ArrayAccess|array|null $data): MMO\core\server\Response
$dataArrayAccess | array | null
returnsResponsea new (non-stream) Response wrapping $data, keeping this response's status/headers.

Zip($filename, $headers, $clientFilename)

staticStreamResponse.php:78

Create a Stream response for a local file, forced to the 'zip' content type (File for the rest).

static function Zip(string $filename, array $headers = [], ?string $clientFilename = null): self
$filenamestringthe local filename
$headers = []arrayheaders of the response
$clientFilename = nullnull | stringthe filename displayed for the user (by default it is the basename of filename)
returnsself

__construct($stream, $code, $reason, $headers)

StreamResponse.php:31
function __construct( Psr\Http\Message\StreamInterface $stream, int $code = 200, string $reason = '', array $headers = [] )
$streamStreamInterfacethe response body.
$code = 200intResponse code
$reason = ''stringStatus text
$headers = []arrayheaders
Inherited members (62)
PropertiesDeclared byDescription
headersaMessage
ikeysaMessage
ListenersaMessage
protocolaMessage
statusCodeaResponse
statusTextaResponse
stoppedaResponse
MethodsDeclared byDescription
addHeader($name, $value)aMessageAppend a value to a header, keeping any value(s) already set (as opposed to setHeader, which replaces).
AddListener($handler, $order)aMessageRegister a listener directly (as opposed to ConnectListeners's attribute-based discovery).
ConnectListeners($obj)aMessageScan $obj's methods for #[Listen(EventClass::class)] attributes and register each one as a listener on the matching event class.
debugName()aResponse
DisconnectListeners($obj)aMessageUndo a previous ConnectListeners: removes every listener that connect had registered for $obj on this event class.
dispatch()aMessageInvoke every GetListeners listener with $this.
getCharset()aMessage
getContentExtension()aMessage
getExitCode()aResponse
getHeader($name)aMessageRetrieves a message header value by the given case-insensitive name.
getHeaderLine($name)aMessageRetrieves a comma-separated string of the values for a single header.
getHeaders()aMessageRetrieves all message header values.
GetListeners()aMessageCollect 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 …
getOrigin()aMessageGet origin of the request by looking at 'Origin', then 'Referer', then 'Sender' in the request's headers.
getProtocolVersion()aMessageRetrieves the HTTP protocol version as a string.
getReasonPhrase()aResponseGets the response reason phrase associated with the status code.
getRedirect()aResponse
getStatusCode()aResponseGets the response status code.
getUserAgent()aMessage
hasHeader($name)aMessageChecks if a header exists by the given case-insensitive name.
HasListener($handler)aMessage
isHTML()aMessage
isIe()aMessage
isIphone()aMessage
isJSON()aMessage
isMac()aMessage
isMobile()aMessage
isOk()aResponse
isPropagationStopped()aResponse
isWindows()aMessage
RemoveListener($handler)aMessageRemove the last added handler from the listeners.
Send($args)aMessageConstruct a new instance of this class with $args, then dispatch it to every listener.
setCacheControl($cache, $pragma)aMessageSet the Cache-Control header (and, optionally, the legacy Pragma header alongside it).
setContentType($type, $charset)aMessageSet the Content-Type header, appending a charset parameter (unless $charset is null/empty).
setExpires($time)aMessageSet the Expires header from a unix timestamp, formatted as an RFC 7231 HTTP-date.
setHeader($name, $value)aMessageSet header, replacing any previous value(s) for the same (case-insensitively matched) header name.
SetListenerOrder($handler, $order)aMessageChange the order of an already-registered listener, whether added directly via AddListener or indirectly via ConnectListeners (which also stores its [$obj, $methodName] callables here).
setRedirect($url, $status)aResponseSet a redirection call send() to actually redirect
setRequireAuth($realm, $type)aResponseSet redirection to a login/password inputbox call send() to actually redirect
setStatus($code, $reason)aResponse
stopPropagation()aResponsePrevent any remaining listener in the current dispatch chain from being called.
unsetHeader($name)aMessageRemove a header entirely.
withAddedHeader($name, $value)aMessageReturn an instance with the specified header appended with the given value.
withCacheControl($cache, $pragma)aMessageSet cache control of a clone.
withContentType($type, $charset)aMessageSet content type of a clone.
withExpires($time)aMessageSet cache expire date of a clone.
withHeader($name, $value)aMessageReturn an instance with the provided value replacing the specified header.
withHeaders($headers, $reset)aMessageReturn a clone with every header in $headers set (through tWithHeaders::setHeader, so each value replaces any existing one for the same header name).
withoutHeader($name)aMessageReturn an instance without the specified header.
withProtocolVersion($version)aMessageReturn an instance with the specified HTTP protocol version.
withRedirect($url, $status)aResponseSet a redirection of a clone call send() to actually redirect
withRequireAuth($realm, $type)aResponseSet redirection to a login/password inputbox of a clone call send() to actually redirect
withStatus($code, $reasonPhrase)aResponseReturn an instance with the specified status code and, optionally, reason phrase.
__clone()aResponseA cloned event starts fresh: propagation is never considered stopped on the clone, even if it was on the original instance.
__toString()aResponse