A PHP framework-agnostic approach to make the current [config](http://12factor.net/config) available to PHP applications and shell-scripts.
A PHP framework-agnostic approach to make the current config available to PHP applications and shell-scripts.
For use by the application wherever it may be fit, for instance in the application's / PHP framework's configuration files.
- Install via composer:
composer require neam/php-app-config:dev-develop
- Copy the
example/configfolder into the root directory of your project.
cp -r vendor/neam/php-app-config/example/config config
- Make sure that all app entry scripts include the file called
include.phpright after including the composer autoloader (adjust the path as necessary in each entry script):
// Make app config available as PHP constants require(dirname(__FILE__) . '/../vendor/neam/php-app-config/include.php');
- Replace config vars in your app with the corresponding PHP constants. Example of usage within Yii configuration:
'db' => array( 'connectionString' => 'mysql:host=localhost;dbname=db', 'emulatePrepare' => true, 'username' => 'root', 'password' => 'supersecret', 'charset' => 'utf8', 'enableParamLogging' => true, // Log SQL parameters //'schemaCachingDuration'=>3600*24, ),
'db' => array( 'connectionString' => 'mysql:host=' . DATABASE_HOST . (defined('DATABASE_PORT') && DATABASE_PORT != '' ? ';port=' . DATABASE_PORT : '') . ';dbname=' . DATABASE_NAME, 'emulatePrepare' => true, 'username' => DATABASE_USER, 'password' => DATABASE_PASSWORD, 'charset' => 'utf8', 'enableParamLogging' => true, // Log SQL parameters //'schemaCachingDuration'=>3600*24, ),
Configuration directives are looked for in the following places:
- $_ENV (which can be populated during a bootstrapping phase as in the
localexample config file)
- php-fpm environment variables (set in php-fpm configuration files and available through getenv())
- apache sub-process environment variables (set in Apache 2.x vhost config and available through apache_getenv())
- php-cgi params (set in nginx location blocks using fastcgi_param and available through $_SERVER)
include.php file will load a file (defaulting to
project-root/config/local/include.php) that defines what application-specific configuration directives to expect.
To change the expected config include file, set the
CONFIG_INCLUDE environment variable to the path to the include relative to the project root. For instance, in order to use
project-root/config/remote/include.php, set the CONFIG_INCLUDE environment variable to
Within the config include file, you define which directives to expect using the Config::expect() method:
Config::expect("DATABASE_URL", $default = null, $required = true);
include.php file will then call
Config::defineConstants() which will define expected configuration directives as constants.
The included example configuration includes sample config profiles used to deploy to Heroku-style services and/or running the code locally.
- Deploy the app to Docker Cloud or a PaaS
- Set the CONFIG_INCLUDE config var to
$ heroku config:set CONFIG_INCLUDE=config/remote/include.php
- Set the expected config vars (DATABASE_URL, GA_TRACKING_ID etc) to their respective values
Your app should now run on Heroku using the expected config vars as PHP constants.
The example local config include will first expect the
remote, then load the
overrides.php file, followed by a non-versioned file called
secrets.php. To use the local configuration:
- Create ang gitignore your secret local configuration file
cp .env.dist .env echo ".env" >> .gitignore
- Add general defaults/overrides that all developers use locally in the
- Add secret defaults/overrides that should not be versioned to the
Your app should now run locally using the expected config vars as PHP constants.
To show your current config, you can run the following in a shell:
php -d variables_order="EGPCS" vendor/neam/php-app-config/export.php
To use the config in a shell-script:
source vendor/neam/php-app-config/shell-export.sh # now the config is available to the shell-script
To export the current configuration to a file which can be sourced by a shell script:
php -d variables_order="EGPCS" vendor/neam/php-app-config/export.php > .exported.php-app-config.inc.sh