Config — Configuration Management
File: src/Config.php Namespace: Magrathea2 Extends: Singleton
Manages application configuration from an INI file. Sections are named by environment (not by topic) and each environment section repeats its own full flat set of keys — there is no section-inheritance or merging syntax.
Configuration File Format
Place your config at:
<AppPath's parent>/configs/magrathea.conf
MagratheaPHP::Load() points Config at MagratheaPHP::GetConfigRoot(): the configs folder (plural) one level above AppPath(). With AppPath("/var/www/my-app/app"), the file is /var/www/my-app/configs/magrathea.conf.
[general] holds cross-environment settings, including use_environment (which section is active by default when SetEnvironment() hasn't been called explicitly). It must name a section that exists, or every Get() throws MagratheaConfigException ("Could not find environment"). MagratheaPHP's Dev()/Prod() don't change it: they only set the Debugger mode. Every other section is a flat, self-contained environment — nothing is inherited or merged between sections.
[general]
use_environment = "dev"
[dev]
db_host = "localhost"
db_name = "my_db"
db_user = "root"
db_pass = "secret"
db_port = "3306"
timezone = "America/Sao_Paulo"
jwt_key = "a-very-long-random-string-here"
[production]
db_host = "db.prod.server"
db_name = "my_db"
db_user = "app_user"
db_pass = "$=DB_PASSWORD"
db_port = "3306"
timezone = "America/Sao_Paulo"
jwt_key = "$=JWT_SECRET"
Timezone
timezone goes in each environment section (a copy in [general] is never read). It's a PHP timezone identifier (America/Sao_Paulo, UTC...):
MagratheaPHP::Load()passes it todate_default_timezone_set(), sonow()anddate()use it.- With it,
MagratheaPHP::Connect()also syncs MySQL's sessiontime_zoneto PHP's on every
Without it, PHP uses php.ini's default (often UTC). An invalid name is ignored the same way: PHP stays on its default.
connection, so NOW() and CURRENT_TIMESTAMP defaults agree with now(). Turn that off with db_sync_timezone = false in the same section (see the Database page).
time_zone (with an underscore) is not a config key: older samples and docs used it, and it was always ignored. The admin's Server page and the Bootstrap wizard's config step run a timezone check (TimezoneCheck) that flags it, along with invalid names and PHP/MySQL clocks out of sync.
Environment variable interpolation
Any value starting with $= is resolved via getenv() at read time, by both GetConfig() and GetConfigFromDefault():
db_pass = "$=DB_PASSWORD"
Properties
| Property | Type | Default | Description |
|---|---|---|---|
$path | string | "configs" | Config directory path |
$configFile | string | "magrathea.conf" | Config file name |
$configs | array|null | null | Parsed config data (loaded once, then cached) |
$environment | string|null | null | Active environment name |
Methods
SetPath(string $p): Config
Override the default config directory.
Config::Instance()->SetPath("/etc/myapp/config");
GetPath(): string
Returns the current config path.
SetConfigFile(string $f): Config
Change the config filename (default: magrathea.conf).
Config::Instance()->SetConfigFile("app.conf");
SetEnvironment(string $e): Config
Sets the active environment for Get() / GetConfigFromDefault(). Does not itself read or validate anything — the environment section is only looked up (and can throw) the next time a default-scoped read happens.
Config::Instance()->SetEnvironment("production");
GetEnvironment(): string
Returns the active environment name. If none was set explicitly via SetEnvironment(), it's resolved from general/use_environment in the config file.
GetAvailableEnvironments(): array
Returns every top-level section name except general.
GetConfig(string $config_name = ""): array|string
Reads from the root of the parsed config — not environment-scoped.
- Empty string (default): returns the entire parsed config.
"section": returns that whole section as an array."section/key": returns a single key from that section.
$prodSection = Config::Instance()->GetConfig("production");
// ["db_host" => "db.prod.server", "db_name" => "my_db", ...]
$prodHost = Config::Instance()->GetConfig("production/db_host");
// "db.prod.server"
Get(string $config_name): string|int|null
Alias for GetConfigFromDefault($config_name). Reads a bare key from the active environment section (see GetEnvironment()).
$host = Config::Instance()->Get("db_host");
GetConfigFromDefault(string $config_name, bool $throwable = false): string|int|null
Reads a bare key from the active environment section. Throws MagratheaConfigException if the environment section itself is missing from the file. If the key is missing within that section, returns null unless $throwable is true, in which case it throws MagratheaConfigException (code 704).
$value = Config::Instance()->GetConfigFromDefault("jwt_key", true);
GetConfigSection(string $section_name): array
Returns a named section as an array (any section — not necessarily the active environment). Throws MagratheaConfigException if the section is empty/missing. There is no merging with any other section.
$db = Config::Instance()->GetConfigSection("production");
echo $db["db_host"];
SetConfig(array $c): Config
Manually overrides the parsed config array (useful for testing — bypasses the file entirely).
Config::Instance()->SetConfig([
"dev" => ["db_host" => "localhost", "db_name" => "test_db"],
]);
GetFilePath(): string
Returns the full resolved path to the config file ($path . "/" . $configFile). Throws MagratheaConfigException if the path is empty or the file doesn't exist.
Usage Examples
Read the active environment's config
use Magrathea2\Config;
$host = Config::Instance()->Get("db_host");
$user = Config::Instance()->Get("db_user");
Read a specific environment regardless of which is active
$prodHost = Config::Instance()->GetConfig("production/db_host");
// or the whole section:
$prod = Config::Instance()->GetConfigSection("production");
Switch environment programmatically
Config::Instance()->SetEnvironment("staging");
$host = Config::Instance()->Get("db_host"); // now reads [staging]
Test with mock config
Config::Instance()->SetConfig([
"dev" => ["db_host" => "localhost", "db_name" => "test_db"],
]);
ConfigFile — arbitrary INI files
File: src/ConfigFile.php Namespace: Magrathea2
A lower-level, non-singleton counterpart to Config: it reads/writes any INI file you point it at (SetPath() + SetFile()), not just the app's magrathea.conf. It backs Admin\ObjectManager's magrathea_objects.conf handling, among other internal uses.
$fileControl = new \Magrathea2\ConfigFile();
$fileControl->SetPath($dir)->SetFile("some.conf");
$data = $fileControl->GetConfig(); // whole file, or "section" / "section/key" like Config::GetConfig()
GetConfig() throws MagratheaConfigException both when the file is missing and when it exists but fails to parse as valid INI (parse_ini_file() returning false) — the same exception type either way, so callers can't distinguish the two failure modes from the exception alone. An empty-but-valid file is not an error: it parses to [] and GetConfig() returns that empty array. GetConfigSection($name) has its own, separate re-parse of the file and does not benefit from this check — a malformed file there is indistinguishable from an empty one: both silently return null (its own !$configSection throw branch is unreachable, since the preceding empty($configSection) check already returns null for both cases).
Integration with MagratheaPHP
MagratheaPHP::Load() calls Config::Instance()->SetPath(...)->LoadFile() internally, and sets PHP's timezone from timezone. You don't normally need to call Config directly during bootstrap.
Notes
- The config file is only parsed once per request (cached in
$configs). GetConfig()is not environment-scoped;Get()/GetConfigFromDefault()are.- There is no section-inheritance syntax (
[section:env]or similar) and no automatic - Never hardcode credentials. Use
$=ENV_VAR_NAMEfor secrets.
These are not interchangeable — pick based on whether you want the active environment or a specific named section.
merging between sections — each environment section repeats its full flat key set.
Class Reference — Config
This class will provide you the quickest access possible to the magrathea.conf config file.
Alias for GetConfigFromDefault
| Param | Type | Default |
|---|---|---|
$config_name |
mixed | required |
returns an array with available environments
`$config_name` can be called to get a parameter from inside a section of the config file. To achieve this, you should use a slash (/) to separate the section from the property. If the slash is not used, the function will return the property only if it's on the root. If `$config_name` is a section name, the function will return the full section as an Array. If `$config_name` is empty, the function will return the full config as an Array (**not recommended!**). If config variable name starts with $=, it will be interpreted as an environment variable and its value will be returned.
| Param | Type | Default |
|---|---|---|
$config_name |
mixed | "" |
This function will get the $config_name property from `magrathea.conf`. It will get from the section defined on `general/use_environment`.
| Param | Type | Default |
|---|---|---|
$config_name |
mixed | required |
$throwable |
mixed | false |
`$section_name` is the name of the section that will be returned as an array.
| Param | Type | Default |
|---|---|---|
$section_name |
mixed | required |
This function will return the environment being used in the project. The environment is defined in `general/use_environment` property and can be defined in the `magrathea.conf` file.
Getst file path
Gets file path
sets config
| Param | Type | Default |
|---|---|---|
$c |
mixed | required |
set name of config file
| Param | Type | Default |
|---|---|---|
$f |
mixed | required |
This function will set the environment for future operations
| Param | Type | Default |
|---|---|---|
$e |
mixed | required |
set path for config file
| Param | Type | Default |
|---|---|---|
$p |
mixed | required |
Class Reference — ConfigApp
This class will provide you the quickest access possible to the magrathea.conf config file.
Gets the value by key
| Param | Type | Default |
|---|---|---|
$key |
string | required |
$default |
mixed | null |
Gets a boolean by key
| Param | Type | Default |
|---|---|---|
$key |
string | required |
$default |
bool | false |
Gets an integer by key
| Param | Type | Default |
|---|---|---|
$key |
string | required |
$default |
float | 0 |
Gets an integer by key
| Param | Type | Default |
|---|---|---|
$key |
string | required |
$default |
int | 0 |
Mocks data
| Param | Type | Default |
|---|---|---|
$data |
mixed | required |
saves a config
| Param | Type | Default |
|---|---|---|
$key |
mixed | required |
$value |
mixed | required |
$overwrite |
mixed | true |
unmocks this
Class Reference — ConfigFile
Magrathea Config loads and saves information in config files.
Creates the configuration file if it doesn't exists. And saves it.
Gets configuration If an acceptable config name, returns its value @todo exception 704 on key does not exists
| Param | Type | Default |
|---|---|---|
$config_name |
mixed | "" |
Gets a full config section @todo exception 704 on key does not exists
| Param | Type | Default |
|---|---|---|
$section_name |
mixed | required |
gets config file path
Saves the config file Default: `true`
| Param | Type | Default |
|---|---|---|
$save_sections |
mixed | true |
Sets the config information
| Param | Type | Default |
|---|---|---|
$c |
mixed | required |
Sets the name of the config file
| Param | Type | Default |
|---|---|---|
$filePath |
mixed | required |
Set the path to the confi file
| Param | Type | Default |
|---|---|---|
$p |
mixed | required |