Skip to content

Core\Out::capture

Runs a function and gives you everything it printed, as a value.

The function uses echo as usual, but nothing it prints reaches the output. capture collects it and returns it as a value. In a command-line program the value is a Core\Cli\Text. In a web request it is a Core\Html\Markup. The text in it was already escaped once when it was printed. You can print the value later with echo, and it is not escaped a second time. as string converts it to a string. The function's own return value is not used.

You can call capture inside another capture. Each call collects only what its own function printed. If the function throws an error, the collecting stops and the error continues as usual.

The through option takes a second function. It gets the collected value and returns a changed copy of the same class, and capture returns that copy.

Good to know: output from Core\Debug::dump is not collected. It still appears as usual.

The examples below collect some output and print it later, change it with through, and check what a function prints.

Parameters

  • $fncallable(): mixedThe function to run. Its return value is not used.

Options

  • throughcallableoptionalA function that takes the collected output and returns new output of the same class. capture returns that new value. Without it, capture returns the output as it was printed.

Return value

Returns Core\Html\Markup|Core\Cli\Text. What $fn printed. In a web request it is a Core\Html\Markup. In every other program it is a Core\Cli\Text. It is empty when $fn printed nothing. Use echo to print it again without escaping it twice. Use is to check the class, then its text method to get a string. Output from Core\Debug::dump is not collected.

Errors

  • RuntimeErrorThe through function returned a value of a different class than it was given.

Examples

Print it later

<?nvs
// Shows how to collect what a function prints and print it later.
// `Core\Out::capture` runs the function. The two `echo` lines print nothing yet.
// The result is a `Core\Cli\Text`, or a `Core\Html\Markup` in a web request.
var $greeting = Core\Out::capture(fn(): void => {
echo 'Hello, ';
echo 'world';
});
// This prints `Before` first, then `Hello, world`.
echo "Before\n";
echo $greeting, "\n";
Output
Before
Hello, world

Change it with through

<?nvs
// Shows how to change the collected text before you get it.
// In a command-line program, `through` gets a `Core\Cli\Text`. `$t->text()` returns
// a `string`, and `Core\Cli\Text::plain` turns the new string back into text.
var $title = Core\Out::capture(
fn(): void => { echo 'monthly report'; },
{ through: fn(Core\Cli\Text $t): Core\Cli\Text => Core\Cli\Text::plain(Core\Str::upper($t->text())) },
);
// This prints `MONTHLY REPORT`.
echo $title, "\n";
Output
MONTHLY REPORT

Check what a function prints

<?nvs
// Shows how a test checks what a function prints.
final class Receipt
{
public static function print(array<string> $items): void
{
foreach ($items as string $item) {
echo '- ', $item, "\n";
}
echo 'Items: ', Core\Arr::count($items), "\n";
}
}
// `as string` converts the collected output to a `string`, so you can compare it.
string $printed = Core\Out::capture(fn(): void => {
Receipt::print(['Coffee', 'Bread']);
}) as string;
// The receipt is what the test expects. This prints `pass`.
string $expected = "- Coffee\n- Bread\nItems: 2\n";
echo $printed == $expected ? 'pass' : 'fail', "\n";
Output
pass