Created
November 12, 2010 22:05
-
-
Save mhayes/674779 to your computer and use it in GitHub Desktop.
ColdFusion Environment Configuration Loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[common] | |
APPLICATION_NAME = DemoApp | |
DEBUG = False | |
[sandbox-server] | |
DEBUG = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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