How to handle configuration in PHP
Configuration management hasn’t changed much in PHP for, oh, the last 20 years or so. Basically, all of its life. Configuration file format has changed in that you can use XML, YAML, JSON, or PHP include files. But that is not configuration management. In other industries there are entire companies devoted to configuration management, but in PHP we are still largely relying on configuration files to provide our applications with variable configuration values.
I use the word “variable” loosely because oftentimes, out of the box, we need to do deployment changes to make configuration changes.
But what should we be doing?
In my humble opinion, the only configuration that should be deployed to a production environment is the configuration needed to get the rest of your configuration.
With that in mind I am announcing the pre-release availability of the Magium Configuration Manager, or MCM. While I have a ways to go, this is part of an idea I have had for at least 7 years for how I thought PHP development and deployment should be done, and it goes way beyond configuration management.
There are three primary purposes behind this library:
- Provide a standardized configuration panel/CLI so developers don’t need to do a code deployment to change a configuration value.
- Provide an inheritable interface where values can “bubble up” through child contexts
- Provide a unified (read merged) configuration interface for third parties that work in your application like magical unicorn dust.
The current version of the MCM is 0.8.22. Prior to a 1.x release I will be adding a web interface for humans to use (0.9) and a REST interface (0.10) for other applications to use (for example, a Node application could retrieve your PHP application’s SQS URL).
If you are using composer all you need to do to install the MCM is run
composer require magium/configuration-management
You will need several configuration files for your site. These are documented on the GitHub page. But for your specific application you will need a configuration file that describes your configuration needs. It will look something like this:
<configuration xmlns="http://www.magiumlib.com/Configuration"> <section identifier="web"> <group identifier="head"> <element identifier="title" /> </group> </section> </configuration>
You will need to bootstrap the MCM for your site:
$factory = new \Magium\Configuration\MagiumConfigurationFactory(); $config = $factory->getManager()->getConfiguration(getenv('ENVIRONMENT'));
And get your configuration values
$siteTitle = $config->getPath('web/head/title');
I will have more to talk about as we go on, but for the time being you can use the following links for more information.
- Documentation at the Magium Configuration Manager Github page
- Instructional Youtube Video
- Some example code
- Redis Configuration Factory
- Magium Selenium/Configuration Bridge Use the MCM to make changes, such as the theme baseUrl, for multiple contexts.