Skip to content

Instantly share code, notes, and snippets.

@stevereich
Last active December 10, 2023 04:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevereich/7ac94120e5b9b292b5f92a7df8ffd4fe to your computer and use it in GitHub Desktop.
Save stevereich/7ac94120e5b9b292b5f92a7df8ffd4fe to your computer and use it in GitHub Desktop.
Commented Custom Application.cfc Template Boilerplate - Coldfusion
component output="false" {
/* **************************** APPLICATION VARIABLES **************************** */
// The application name. If you do not set this variable, or set it to the empty string,
// your CFC applies to the unnamed application scope, which is the ColdFusion J2EE servlet
// context.
THIS.name = "foo";
// Life span, as a real number of days, of the application, including all Application scope variables.
THIS.applicationTimeout = createTimeSpan(0, 1, 0, 0);
// Whether the application supports Client scope variables.
THIS.clientManagement = false;
// Where Client variables are stored; can be cookie, registry, or the name of a data source.
THIS.clientStorage = "registry"; //cookie||registry||datasource
// Contains ColdFusion custom tag paths.
THIS.customTagPaths = "";
// The Google Maps API key required to embed Google Maps in your web pages.
THIS.googleMapKey = "";
// Name of the data source from which the query retrieves data.
THIS.datasource = "";
// Whether to store login information in the Cookie scope or the Session scope.
THIS.loginStorage = "cookie"; //cookie||session
// A structure that contains ColdFusion mappings. Each element in the structure consists of a key
// and a value. The logical path is the key and the absolute path is the value.
THIS.mappings = {};
// Whether to enable validation on cfform fields when the form is submitted.
THIS.serverSideFormValidation = true;
// Whether the application supports Session scope variables.
THIS.sessionManagement = true;
// Life span, as a real number of days, of the user session, including all Session variables.
THIS.sessionTimeout = createTimeSpan(0, 0, 30, 0);
// Whether to send CFID and CFTOKEN cookies to the client browser.
THIS.setClientCookies = true;
// Whether to set CFID and CFTOKEN cookies for a domain (not just a host).
THIS.setDomainCookies = false;
// Whether to protect variables from cross-site scripting attacks.
THIS.scriptProtect = false;
// A Boolean value that specifies whether to add a security prefix in front of the value that a
// ColdFusion function returns in JSON-format in response to a remote call.
THIS.secureJSON = false;
// The security prefix to put in front of the value that a ColdFusion function returns in
// JSON-format in response to a remote call if the secureJSON setting is true.
THIS.secureJSONPrefix = "";
// A comma-delimited list of names of files. Tells ColdFusion not to call the onMissingTemplate
// method if the files are not found.
THIS.welcomeFileList = "";
// A struct that contains the following values: server, username, and password.If no value
// is specified, takes the value in the administrator.
THIS.smtpServersettings = {};
// Request timeout. Overrides the default administrator settings.
THIS.timeout = 30; // seconds
// A list of ip addresses that need debugging.
THIS.debugipaddress = "";
// Overrides the default administrator settings. It does not report compile-time exceptions.
THIS.enablerobustexception = false;
/* ORM variables */
// Specifies whether ORM should be used for the ColdFusion application.Set the value to true
// to use ORM. The default is false.
THIS.ormenabled = false;
// The struct that defines all the ORM settings. Documentation:
// http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSED380324-6CBE-47cb-9E5E-26B66ACA9E81.html
THIS.ormsettings = {};
/* **************************** APPLICATION METHODS **************************** */
/**
@hint "Runs when an application times out or the server is shutting down."
@ApplicationScope "The application scope."
*/
public void function onApplicationEnd(struct ApplicationScope=structNew()) {
return;
}
/********************************************************************************/
/**
@hint "Runs when ColdFusion receives the first request for a page in the application."
*/
public boolean function onApplicationStart() {
initApp(true);
return true;
}
/********************************************************************************/
/**
@hint "Intercepts any HTTP or AMF calls to an application based on CFC request."
@cfcname "Fully qualified dotted path to the CFC."
@method "The name of the method invoked."
@args "The arguments (struct) with which the method is invoked."
*/
public void function onCFCRequest(required string cfcname, required string method, required string args) {
return;
}
/********************************************************************************/
/**
@hint "Runs when an uncaught exception occurs in the application."
@Exception "The ColdFusion Exception object. For information on the structure of this object, see the
description of the cfcatch variable in the cfcatch description."
@EventName "The name of the event handler that generated the exception. If the error occurs during request
processing and you do not implement an onRequest method, EventName is the empty string."
note: This method is commented out because it should only be used in special cases
public void function onError(required any Exception, required string EventName) {
return;
}
*/
/********************************************************************************/
/**
@hint "Runs when a request specifies a non-existent CFML page."
@TargetPage "The path from the web root to the requested CFML page."
note: This method is commented out because it should only be used in special cases
public boolean function onMissingTemplate(required string TargetPage) {
return true;
}
*/
/********************************************************************************/
/**
@hint "Runs when a request starts, after the onRequestStart event handler. If you implement
this method, it must explicitly call the requested page to process it."
@TargetPage "Path from the web root to the requested page."
note: This method is commented out because it should only be used in special cases
*/
public void function onRequest(required string TargetPage) {
var doReinit = false;
if(structkeyexists(url,'reinit')){
doReinit = true;
}
initApp(doReinit);
include TargetPage;
// show variables if url param is passed
if(structkeyexists(url,'vars')) {
var a = (len(url.vars)) ? url.vars : "";
dumpVars(a);
}
}
/********************************************************************************/
/**
@hint "Runs when a request starts."
@TargetPage "Path from the web root to the requested page."
*/
public boolean function onRequestStart(required string TargetPage) {
return true;
}
/********************************************************************************/
/**
@hint "Runs at the end of a request, after all other CFML code."
*/
public void function onRequestEnd() {
return;
}
/********************************************************************************/
/**
@hint "Runs when a session ends."
@SessionScope "The Session scope"
@ApplicationScope "The Application scope"
*/
public void function onSessionEnd(required struct SessionScope, struct ApplicationScope=structNew()) {
return;
}
/********************************************************************************/
/**
@hint "Runs when a session starts."
*/
public void function onSessionStart() {
return;
}
/********************************************************************************/
/**
@vars "Variables to dump"
*/
private void function dumpVars(string vars="application,session,cookie,client,request,form,url,cgi,server") output=true {
var dumpedVars = (len(arguments.vars)) ? arguments.vars : "application,session,cookie,client,request,form,url,cgi,server";
var scopeArray = listtoarray(dumpedVars, ",", false);
var errorVar = {};
var output = "";
savecontent variable="output"{
writeoutput("<div style=""overflow:auto;background-color:##FFFFEE;border-top:2px ##CCC solid;padding:0 30px 30px 30px"">");
writeoutput("<h2 style=""margin-bottom:30px"">CF Variable Dump</h2>");
for (var i = 1; i <= arraylen(scopeArray); i++) {
try {
writedump(var = evaluate(scopeArray[i]), expand = false, label = "#ucase(scopeArray[i])#");
}
catch(any e){
errorVar['errorMessage'] = e.message;
writedump(var = errorVar, expand = true, label = "#ucase(scopeArray[i])#");
}
finally {
errorVar = {};
}
}
writeoutput("</div>");
}
writeoutput(output);
}
private struct function initApp(boolean force=false){
if(!structkeyexists(application,'startTime') || arguments.force){
lock timeout="30" scope="application" type="exclusive" {
application['startTime'] = now();
application['offline'] = false;
application['dsn'] = "";
application['settings'] = {};
application['cfc'] = {};
application['dsn'] = {};
application['cfc'] = {};
}
}
return application;
}
/********************************************************************************/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment