MagratheaPHP2
Up to date Narrative last written —; no source changes since. Method signatures below are reflected live.

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:

<appRoot>/config/magrathea.conf

[general] holds cross-environment settings, including use_environment (which section is active by default when SetEnvironment() hasn't been called explicitly). Every other section is a flat, self-contained environment — nothing is inherited or merged between sections.

[general]
	use_environment = "default"
	time_zone = "America/Sao_Paulo"

[dev]
	db_host = "localhost"
	db_name = "my_db"
	db_user = "root"
	db_pass = "secret"
	db_port = "3306"
	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"
	jwt_key = "$=JWT_SECRET"

Environment variable interpolation

Any value starting with $= is resolved via getenv() at read time, by both GetConfig() and GetConfigFromDefault():

db_pass = "$=DB_PASSWORD"

Properties

PropertyTypeDefaultDescription
$pathstring"configs"Config directory path
$configFilestring"magrathea.conf"Config file name
$configsarray|nullnullParsed config data (loaded once, then cached)
$environmentstring|nullnullActive 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.

$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"],
]);

Integration with MagratheaPHP

MagratheaPHP::Load() calls Config::Instance()->SetPath(...)->LoadFile() internally. You don't normally need to call Config directly during bootstrap.


Notes

Class Reference — Config

Magrathea2\Config extends Singleton /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/Config.php

This class will provide you the quickest access possible to the magrathea.conf config file.

Get($config_name): string|int|null

Alias for GetConfigFromDefault

ParamTypeDefault
$config_name mixed required
GetAvailableEnvironments(): array

returns an array with available environments

GetConfig($config_name = ""): array|string

`$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.

ParamTypeDefault
$config_name mixed ""
GetConfigFromDefault($config_name, $throwable = false): string|int|null

This function will get the $config_name property from `magrathea.conf`. It will get from the section defined on `general/use_environment`.

ParamTypeDefault
$config_name mixed required
$throwable mixed false
GetConfigSection($section_name)

`$section_name` is the name of the section that will be returned as an array.

ParamTypeDefault
$section_name mixed required
GetEnvironment(): string

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.

GetFilePath(): string

Getst file path

GetPath(): string

Gets file path

SetConfig($c)

sets config

ParamTypeDefault
$c mixed required
SetConfigFile($f)

set name of config file

ParamTypeDefault
$f mixed required
SetEnvironment($e)

This function will set the environment for future operations

ParamTypeDefault
$e mixed required
SetPath($p)

set path for config file

ParamTypeDefault
$p mixed required

Class Reference — ConfigApp

Magrathea2\ConfigApp extends Singleton /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/ConfigApp.php

This class will provide you the quickest access possible to the magrathea.conf config file.

Get(string $key, $default = null): ?string

Gets the value by key

ParamTypeDefault
$key string required
$default mixed null
GetBool(string $key, bool $default = false): bool

Gets a boolean by key

ParamTypeDefault
$key string required
$default bool false
GetFloat(string $key, float $default = 0): float

Gets an integer by key

ParamTypeDefault
$key string required
$default float 0
GetInt(string $key, int $default = 0): int

Gets an integer by key

ParamTypeDefault
$key string required
$default int 0
Mock($data): Magrathea2\ConfigApp

Mocks data

ParamTypeDefault
$data mixed required
Save($key, $value, $overwrite = true): Magrathea2\Admin\Features\AppConfig\AppConfig

saves a config

ParamTypeDefault
$key mixed required
$value mixed required
$overwrite mixed true
UnMock(): Magrathea2\ConfigApp

unmocks this

Class Reference — ConfigFile

Magrathea2\ConfigFile /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/ConfigFile.php

Magrathea Config loads and saves information in config files.

CreateFileIfNotExists()

Creates the configuration file if it doesn't exists. And saves it.

GetConfig($config_name = "")

Gets configuration If an acceptable config name, returns its value @todo exception 704 on key does not exists

ParamTypeDefault
$config_name mixed ""
GetConfigSection($section_name)

Gets a full config section @todo exception 704 on key does not exists

ParamTypeDefault
$section_name mixed required
GetPath(): string

gets config file path

Save($save_sections = true)

Saves the config file Default: `true`

ParamTypeDefault
$save_sections mixed true
SetConfig($c)

Sets the config information

ParamTypeDefault
$c mixed required
SetFile($filePath)

Sets the name of the config file

ParamTypeDefault
$filePath mixed required
SetPath($p)

Set the path to the confi file

ParamTypeDefault
$p mixed required
__construct()

Examples