Core\Env::Mode
Which deployment your program is running in. There are two modes and there is no third.
Core\Env::Mode is declared in the class Core\Env. The method mode returns it.
Production is the careful one: an error shows a short message rather than its details, the log is written as JSON at info level, and requests are not logged one by one. Development is the other end, and you ask for it in nvs.toml. A program that has configured nothing is in Production, so a server where nobody wrote the line is still the careful one.
A mode is only a shorthand for the starting value of those five settings, and each of them stays settable on its own. A host that wants production with one thing changed writes that one line instead of reaching for the other mode.
Good to know: the mode is never taken from an environment variable, and nothing guesses it from a hostname or from how the program was built. Staging is not a third mode — a staging server runs in Production with its own settings.
Examples
Which mode am i in
<?nvs// This program checks which mode it runs in and does something different in each.// Nothing is configured here, so `Core\Env::mode()` returns `Production`.// A program is in `Production` until somebody sets the other mode.
Core\Env::Mode $mode = Core\Env::mode();
echo match ($mode) { Core\Env::Mode::Production => "production: keep what a visitor sees short\n", Core\Env::Mode::Development => "development: show me everything\n",};
// There are only two modes. An `if` is enough when only one of them matters.if ($mode == Core\Env::Mode::Development) { echo "loading the sample customers\n";} else { echo "no sample customers on a real deployment\n";}production: keep what a visitor sees shortno sample customers on a real deploymentWhat each mode means for a setting
<?nvs// This program passes each mode to a function and prints what it returns.// You can see the result for a real deployment while you work on your own machine.
class Pages{ // How long a finished page may be kept before it is built again. public static function cacheSeconds(Core\Env::Mode $mode): int { return match ($mode) { Core\Env::Mode::Production => 3600, Core\Env::Mode::Development => 0, }; }
public static function name(Core\Env::Mode $mode): string { return match ($mode) { Core\Env::Mode::Production => 'production', Core\Env::Mode::Development => 'development', }; }}
array<Core\Env::Mode> $both = [Core\Env::Mode::Production, Core\Env::Mode::Development];
foreach ($both as Core\Env::Mode $mode) { echo Pages::name($mode), ': keep a page for ', Pages::cacheSeconds($mode), " seconds\n";}production: keep a page for 3600 secondsdevelopment: keep a page for 0 secondsHow much of a failure a visitor sees
<?nvs// The everyday job: an order did not save, and somebody has to be told. On a// real deployment the visitor gets a short line and a reference to quote.// While you are working on the program you want the whole message instead.
class Failure{ public static function forVisitor(Core\Env::Mode $mode, string $detail, string $reference): string { return match ($mode) { Core\Env::Mode::Production => 'Sorry, we could not save your order. Please quote ' . $reference . '.', Core\Env::Mode::Development => 'Could not save the order: ' . $detail, }; }}
string $detail = 'the payments service did not answer in time';string $reference = 'ORD-1042';
Core\Env::Mode $mode = Core\Env::mode();
echo 'the visitor reads: ', Failure::forVisitor($mode, $detail, $reference), "\n";
// The mode decides who is told what went wrong. It does not decide whether the// server keeps the detail, so this line is the same in both modes.echo 'the server keeps: ', $reference, ' ', $detail, "\n";the visitor reads: Sorry, we could not save your order. Please quote ORD-1042.the server keeps: ORD-1042 the payments service did not answer in time