-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
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.
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.
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.
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.iniyourself usingFileWriter - rename the generated
config.prod.inias part of your deployment process
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.
PHP.GT/Config is a separately maintained component of PHP.GT/WebEngine.