Skip to content

Instantly share code, notes, and snippets.

@mhayes
Created November 12, 2010 22:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhayes/674779 to your computer and use it in GitHub Desktop.
Save mhayes/674779 to your computer and use it in GitHub Desktop.
ColdFusion Environment Configuration Loader
component {
function init(required string configFile) {
variables.configFile = arguments.configFile;
variables.loadSections = ["common", CGI.SERVER_NAME];
return getConfig();
}
private function getConfig() {
var config = StructNew();
var sections = getProfileSections(variables.configFile);
// Load settings applicable to this environment
for(var i=1; i LTE ArrayLen(variables.loadSections); i++) {
var currentSection = variables.loadSections[i];
// Verify section exists
if (StructKeyExists(sections, currentSection)) {
var keyList = sections[currentSection];
for(var j=1; j LTE ListLen(keyList); j++) {
var currentKey = ListGetAt(keyList, j);
var currentKeyValue = GetProfileString(variables.configFile, currentSection, currentKey);
// Append setting (overwrite common settings if a server-specific value exists)
StructInsert(config, currentKey, currentKeyValue, True);
}
}
}
return config;
}
}
[common]
APPLICATION_NAME = DemoApp
DEBUG = False
[sandbox-server]
DEBUG = True
<h1>Config Demo</h1>
<!--- assumes config.cfc and config.ini are in the same directory --->
<cfset config = new config(ExpandPath('config.ini')) />
<cfdump var="#config#" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment