Last active
April 13, 2017 17:15
-
-
Save tommyready/dccb142f295921b465294ad753477b89 to your computer and use it in GitHub Desktop.
Getting Application Scope Settings from INI File in CFSCRIPT
This file contains hidden or 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 { | |
this.name = 'MyApp' & hash(getCurrentTemplatePath()); | |
this.applicationTimeout = createTimeSpan(1,0,0,0); | |
this.sessionTimeout = createTimeSpan(1,0,0,0); | |
this.sessionManagement = true; | |
this.setClientCookies = false; | |
public boolean function onApplicationStart() { | |
Application.Version = '1.0'; | |
var iniSettings = getSettings(); // Get Settings from Ini Files. | |
StructAppend(Application,iniSettings,true); // Append Settings from Ini Files to Application Scope | |
return true; | |
} | |
public void function onApplicationEnd(struct applicationScope={}) { | |
return; | |
} | |
public void function onSessionStart() { | |
return; | |
} | |
public void function onSessionEnd(required struct sessionScope, struct applicationScope={}) { | |
return; | |
} | |
public boolean function onRequestStart(required string targetPage) { | |
return true; | |
} | |
public void function onRequest(required string targetPage) { | |
if( isDefined("url.init") AND url.init eq 1 ) { | |
onApplicationStart(); | |
} | |
include arguments.targetPage; | |
return; | |
} | |
public void function onRequestEnd() { | |
return; | |
} | |
public void function onAbort(required string targetPage) { | |
return; | |
} | |
public void function onError(required any exception, required string eventName) { | |
writeDump( exception ); | |
return; | |
} | |
public boolean function onMissingTemplate(required string targetPage) { | |
return true; | |
} | |
private struct function getSettings() { | |
var iniSettings = {}; | |
var iniFiles = DirectoryList(GetDirectoryFromPath(GetBaseTemplatePath()) & 'settings\',true,'query','*.ini'); // Get All INI Files in the Settings Directory | |
for( var f in iniFiles ) { | |
StructAppend(iniSettings, parseIniFile(f.Directory & '\' & f.Name), true); | |
} | |
// Look for Application Level Settings | |
if(StructKeyExists(iniSettings, 'application')) { | |
var applicationScopedSettings = iniSettings.Application; | |
for(var key in applicationScopedSettings ) { | |
iniSettings[key] = applicationScopedSettings[key]; | |
} | |
// Remove Application Scope Setting Key | |
StructDelete(iniSettings,'application'); | |
} | |
return iniSettings; | |
} | |
private struct function parseIniFile(required string iniFile) { | |
var sectionsReturn = {}; | |
var sections = GetProfileSections(arguments.iniFile); | |
for (var section in sections) { | |
for (var i=1;i<=listLen(sections[section]);i++) { | |
sectionsReturn[section][listGetAt(sections[section],i)] = GetProfileString(arguments.iniFile, section, listGetAt(sections[section],i) ) ; | |
} | |
} | |
return sectionsReturn; | |
} | |
} |
This file contains hidden or 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
[application] | |
datasource=myDSN | |
[directories] | |
directory=C:\MyDirectory\ | |
fileDirectory=C:\MyDirectory\FILES\ | |
backupDirectory=C:\MyDirectory\BACKUPS\ | |
reportDirectory=C:\MyDirectory\REPORTS\ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I put the ini files in a directory named "settings" in the root directory of the application.