Skip to content

Generating config files

Greg Bowler edited this page Mar 16, 2026 · 1 revision

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:

  • dev
  • deploy
  • prod

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=MyApp
  • database.host=localhost
  • exampleapi.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 described earlier looks for config.production.ini.

That means if you want a generated file to be picked up automatically by ConfigFactory::createForProject(), you should either:

  • write config.production.ini yourself using FileWriter
  • rename the generated config.prod.ini as part of your deployment process

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.