Index

OutputCapture

MMO\core\helpers · 1 symbol · generated 5 Oct 2026

class

OutputCapture

OutputCapture.php:16

Capture everything echoed/printed (via PHP's own output buffering, ob_start()) so it can be inspected, filtered, or discarded instead of being sent to the real output. Captures can nest: starting a new one suspends the previous one (its output is unaffected until the new one is closed).

class MMO\core\helpers\OutputCapture implements MMO\core\interfaces\iRenderable
Namespace
MMO\core\helpers
Implements
iRenderable
$cap = OutputCapture::Start(); echo "hello world"; $cap->close(); // 'hello world' -- captured instead of printed

Index

CHUNK_SIZE
active
chunks
filters
INSTS static
stream
streamStart
addFilter($filter)Add a filter to apply to the text
clear($end)Clear output buffer
close($clear)Close the output buffer
flush($end)Flush output buffer and triggers all the filters for the resulted data
getStream()
getText()
isActive()
IsCapturing() static
isCurrent()
makeCurrent($clean)Ensure this capture is isCurrent, flushing/discarding (per $clean) any output buffer that was pushed on top of it in the meantime (logging a warning each time, since that's usually a sign of u…
onWrite($buffer, $phase)
render($opts)
Start($stream, $chunkSize) staticStart capturing output.
StartBig($chunkSize) staticLike Start, but always backed by a php://temp stream (spills to disk once large enough) instead of an in-memory array -- for output that may be too big to comfortably hold in memory.
Stop() staticClose and return the most recently started (still active) capture.
StopAll() staticClose every currently active capture, innermost first.
__construct($stream, $chunkSize)protected constructor so you cannot do new OutputCapture() -- use Start / StartBig
__toString()

Constants1

CHUNK_SIZE

const CHUNK_SIZE = 16384

Properties6

active

protected bool $active = true
Type
bool

chunks

protected array $chunks = []
Type
array

filters

protected array $filters = []
Type
array

INSTS

static
protected static array $INSTS = []
Type
array

stream

protected $stream = null

streamStart

protected int $streamStart = 0
Type
int

Methods18

addFilter($filter)

OutputCapture.php:112

Add a filter to apply to the text

function addFilter(callable $filter): void
$filtercallablefunction(string $buffer, int $phase, self $output): string|false. Returning false discards that chunk entirely.
returnsvoid
$cap->addFilter(fn($buf, $phase, $o) => strtoupper($buf));

clear($end)

OutputCapture.php:186

Clear output buffer

function clear(bool $end = false): void
$end = falseboolif true, close the buffer too.
returnsvoid
$cap = OutputCapture::Start(); echo "discard me"; $cap->clear(); // everything captured so far is thrown away echo "kept"; $cap->close(); // 'kept'

close($clear)

OutputCapture.php:198

Close the output buffer

function close(bool $clear = false): string
$clear = falseboolif true, ignore content in the buffer
returnsstringthe captured text (empty if $clear was true)
$cap->close();

flush($end)

OutputCapture.php:169

Flush output buffer and triggers all the filters for the resulted data

function flush(bool $end = false): void
$end = falseboolif true, close the buffer too.
returnsvoid
$cap->flush(); // runs the filters over what's captured so far, without closing

getText()

OutputCapture.php:125
function getText(): string
returnsstringthe text captured so far -- still readable after close (the final text is snapshotted then)
$cap->getText();

IsCapturing()

staticOutputCapture.php:79
static function IsCapturing(): bool
returnsbooltrue if at least one capture is currently active

isCurrent()

OutputCapture.php:138
function isCurrent(): bool
returnsbooltrue if this capture is active AND is the innermost (most recently started, not yet closed) one -- i.e. it is the one PHP's output buffer stack is actually writing into right now

makeCurrent($clean)

OutputCapture.php:150

Ensure this capture is isCurrent, flushing/discarding (per $clean) any output buffer that was pushed on top of it in the meantime (logging a warning each time, since that's usually a sign of unrelated code calling ob_start() without cleaning up).

protected function makeCurrent(bool $clean = false): void
$clean = falsebool
returnsvoid
throwsif this capture is not isActive

render($opts)

OutputCapture.php:95
function render(array $opts = []): string
$opts = []array
returnsstringsame as getText, flushing first if still active (so any buffered-but-unwritten text is included)

Start($stream, $chunkSize)

staticOutputCapture.php:39

Start capturing output.

static function Start($stream = null, int $chunkSize = self::CHUNK_SIZE): self
See
StartBig) for potentially large output
$stream = nullif given, captured text is written to this stream (or a stream opened from this filename) instead of being buffered in memory -- use this (or
$chunkSize = self::CHUNK_SIZEintpassed to ob_start()'s own chunk size
returnsselfthe new, active capture
OutputCapture::Start();

StartBig($chunkSize)

staticOutputCapture.php:50

Like Start, but always backed by a php://temp stream (spills to disk once large enough) instead of an in-memory array -- for output that may be too big to comfortably hold in memory.

static function StartBig(int $chunkSize = self::CHUNK_SIZE): self
$chunkSize = self::CHUNK_SIZEint
returnsself

Stop()

staticOutputCapture.php:60

Close and return the most recently started (still active) capture.

static function Stop(): self
returnsselfthe closed capture (its final text is available via getText)
throwsif no capture is currently active
OutputCapture::Stop()->getText();

StopAll()

staticOutputCapture.php:72

Close every currently active capture, innermost first.

static function StopAll(): void
returnsvoid

__construct($stream, $chunkSize)

OutputCapture.php:88

protected constructor so you cannot do new OutputCapture() -- use Start / StartBig

protected function __construct($stream = null, int $chunkSize = self::CHUNK_SIZE)
$stream = null
$chunkSize = self::CHUNK_SIZEint