Configuration is power. I’m always looking on how to keep my settings as far away from my application as possible, so that switching them around is painless and straightforward. Most frameworks address this with some sort of in-house convention, but if you’re into Zend Framework, you’d know that ZF just gives you the tools, not the direction.
Goal
For the sake of relevancy I’m not going to include my entire bootstrap file. I also want to briefly note that I’m extremely new to Zend Framework, so any suggestions or revelations would be welcomed and praised.
Simply put, my goal was to autoload any given number of configuration files into global namespace without worrying about name conflicts. I also wanted to employ a different set of configuration files based on whatever environment I currently have my application running in (development, production, what have you.) Utilizing Zend_Registry for storage and Zend_Config_Ini for reading, the answer became very very simple.
Declaring the Environment
In order to get started, you’re going to need to have a pre-boot configuration section in your bootstrap. This is where I define all my paths, set my includes path, error reporting level, and so on. Here I will also declare a setting to determine what environment I’m in.
$environment = 'development';
/**
* Later on
*/
if(!defined('ENVIRONMENT')) define('ENVIRONMENT', $environment);
Creating Environment Settings
Now that we have defined our environment, we need to load in our configuration files. Not only that, but we need to only load the environment-specific settings. Zend_Config_Ini, and more specifically, PHP’s parse_ini_file(), make it ridiculously easy. We can format our ini files following this example of a database.ini I have:
[production] adapter = PDO_MySQL connect.host = localhost connect.username = Production_User connect.password = Production_Password connect.dbname = Production_Database_Name [development : production] connect.host = localhost connect.username = root connect.password = root connect.dbname = zend_test
Here I’ve specified the main section:
[production]
first, as that configuration matters most to me. However, I’m still in development phase.
I don’t need to change everything, so I’ll use the parse_ini_file() syntax of inheritance and using production as my parent:
[development : production]
Doing this will pass all configuration values from production to development, and allow development to override the necessary ones. If you have more than these two environments, simply declare each new section (empty or not) with the same style of inheritance syntax:
[sectionName : parentName]
Loading Environment Settings
To automatically load the new settings file(s) you’ve created, you need to scan the directory and load the appropriate files into Zend_Registry, using the filename for each file as the registry index.
$files = scandir('./applications/config/'); // configure your path here
foreach($files as $file) {
/**
* Skip directory files
*/
if($file[0] != '.') {
Zend_Registry::set(substr($file, 0, -4), new Zend_Config_Ini($file, ENVIRONMENT));
}
}
This will load the file into the registry and use the name of the file (minus the extension) as the identifier. It will only load the sections identified by ENVIRONMENT into the registry.
Accessing Settings
Simply call the Zend_Registry as you normally would. All avenues of normal functionality are still present, and even more useful, now that your configurations are more autonomous.
if(Zend_Registry::isRegistered('database')) {
$adapter = Zend_Registry::get('database')->adapter;
$connectionDetails = Zend_Registry::get('database')->connect->toArray();
}
You also don’t have to worry about database settings file overwriting anything in the registry, unless you manually set something else to do so, since it represents a file inside a directory. This is just a very simple example of what you can do with the framework itself. I’ll post more uses of the framework as I encounter them. I want to reiterate that while I’m experienced in frameworks generally, Zend is still new to me, and I may be violating Zend standards left and right (though their documentation on conventions is noteworthy, particularly the part about PHP tags in code.) It seems that the deeper I delve into Zend, the more worthy it appears to me and my personal style of development. More on that next time!
0 comments ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment