A schema defines the structure of your configuration, including allowed keys, value types, and validation rules. league/config uses the nette/schema package for this purpose. You define schemas using the Nette\Schema\Expect class and pass them into the League\Config\Configuration constructor.
With schemas, you can:
- Mark options as
required. - Provide
default values. - Validate types (int, string, bool, etc.), ranges (min/max), regular expressions, or enums.
- Mark options as
deprecated. - Use custom assertions via
assert(). - Cast values to specific types.
Note on nested structures: While nette/schema typically casts nested structures (Expect::structure) to stdClass objects, league/config automatically casts them to array for you.
use League\Config\Configuration;
use Nette\Schema\Expect;
$config = new Configuration([
'debug_mode' => Expect::bool(),
'database' => Expect::structure([
'driver' => Expect::anyOf('mysql', 'postgresql', 'sqlite')->required(),
'host' => Expect::string()->default('localhost'),
'port' => Expect::int()->min(1)->max(65535),
'ssl' => Expect::bool(),
'database' => Expect::string()->required(),
'username' => Expect::string()->required(),
'password' => Expect::string()->nullable(),
]),
'logging' => Expect::structure([
'enabled' => Expect::bool()->default($_ENV['DEBUG'] == true),
'file' => Expect::string()->deprecated("use logging.path instead"),
'path' => Expect::string()->assert(function ($path) { return \is_writeable($path); })->required(),
]),
]);