Generating config files
This library can also write configuration back to disk. There are two ways to do this:
- from the command line with
config-generate - from PHP using
FileWriter
Generating files from the CLI
The package exposes a binary named config-generate.
vendor/bin/config-generate deploy \
"database.host=staging-db.internal" \
"database.schema=shop_branch_42" \
"stripe.secret_key=secret-for-ci"
This creates a new INI file in the current working directory. The first argument controls the suffix, and the remaining arguments are key-value pairs written using dot notation.
The allowed suffixes are:
devdeployprod
So the above example writes config.deploy.ini.
CLI argument format
Each config value must be written as:
section.key=value
For example:
app.namespace=MyAppdatabase.host=localhostexampleapi.key=abc123
If the arguments are invalid, the command writes an error message to standard error.
Within PHP, the Generator class throws GT\Config\InvalidArgumentException when the suffix or key-value arguments are invalid.
Writing a config file in PHP
If you already have a Config object, use FileWriter to write it as an INI file.
use GT\Config\Config;
use GT\Config\ConfigSection;
use GT\Config\FileWriter;
$config = new Config(
new ConfigSection("app", [
"namespace" => "MyApp",
"debug" => "true",
]),
new ConfigSection("database", [
"host" => "localhost",
"port" => "3306",
])
);
$writer = new FileWriter($config);
$writer->writeIni(__DIR__ . "/config.generated.ini");
Any missing directories in the pathname are created automatically.
If the file cannot be written, FileWriter throws GT\Config\ConfigException.
A note on production file naming
The CLI generator’s production suffix is prod, which writes config.prod.ini. The automatic project loader recognises both config.prod.ini and config.production.ini as production configuration. Keep only one of these files: using both is a conflicting environment selection.
The generator also supports dev and deploy. Use FileWriter or create the INI file directly for other supported environment filenames, including staging.
Typical deployment usage
Here we can see how this is useful in continuous integration. A deployment pipeline can create a small override file containing secrets or environment-specific hosts without editing the committed config.ini.
vendor/bin/config-generate deploy \
"database.host=$DB_HOST" \
"database.password=$DB_PASSWORD" \
"shopapi.key=$SHOP_API_KEY"
This keeps the committed config readable while still allowing dynamic values at deploy time.
If you are using this package inside the wider PHP.GT framework, continue to Config within WebEngine applications.