I load in static setting via an ini file. Ini file have the benefits
- of NOT being server by most web servers by default.
- easier to edit than XML
- even simplier than JSON
Setting get loaded in Application.cfc
void function setupApplication() output="false" {
...
application.stAdminSetting = application.loadini("admin\standard.ini");
...
}
Application.cfc has this as a function...
<cffunction name="loadini" output="false" returnType="struct">
<cfargument name="configfile" required="true" type="string" hint="reads from currentTemplate">
<cfscript>
var stResult = {};
var iniFilePath = "#GetDirectoryFromPath(GetBaseTemplatePath())##arguments.configfile#";
var stSection = getProfileSections(iniFilePath);
for(var section in stSection) {
var CurrentSection = evaluate("stSection.#section#");
var stData = {};
for(var i = 1; i <= ListLen(CurrentSection); i++) {
var key = ListGetAt(CurrentSection, i);
stData[key] = getProfileString(iniFilePath, section, key);
}
setvariable("stResult.#section#", stData);
}
return stResult;
</cfscript>
</cffunction>
The ini file can have any number of setting. In particular my ini file has:
[ws]
comment=http://xxxxx.com/resource/comment.cfc?wsdl
ior=http://xxxxx.com/resource/ior.cfc?wsdl
node=http://xxxxx.com/resource/node.cfc?wsdl
pref=http://xxxxx.com/resource/pref.cfc?wsdl
traffic=http://xxxxx.com/resource/traffic.cfc?wsdl
What kinds of things can I do to improve this?
2 Answers 2
You don't need evaluate()
here:
var CurrentSection = evaluate("stSection.#section#");
Simply do this:
var CurrentSection = stSection[section]);
Similarly you don't need to use setVariable()
:
setvariable("stResult.#section#", stData);
Simply:
stResult[section] = stData;
What kind of things are you looking to improve? Your solution looks pretty straightforward. The only problem with an ini file is that ColdFusion can't instantly/natively parse it. Whereas XML/JSON can be parsed directly into a Coldfusion Struct.
<cfscript>
public struct function loadini( required string configFile) {
var stResult = {};
var iniFilePath = "#GetDirectoryFromPath(GetBaseTemplatePath())##arguments.configfile#";
var stSection = getProfileSections(iniFilePath);
// loop over the sections
for (section in stSection) {
// loop over the list of variables in the ini file
for (var i=0;i<=listLen(stSection[section]);i++) {
// result.section.variable = value
stResult[section][listGetAt(stSection[section],i)] = GetProfileString(iniFilePath, section, listGetAt(stSection[section],i) ) ;
}
}
return stResult;
}
</cfscript>
<cfdump var="#loadini('test.ini')#">