Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a ColdFusion cfc method determine its own name?

Tags:

coldfusion

cfc

I am creating an API, and within each method I make a call to a logging method for auditing and troubleshooting. Something like:

<cffunction name="isUsernameAvailable">
    <cfset logAccess(request.userid,"isUsernameAvailable")>
    ......
</cffunction>

I'd like to avoid manually repeating the method name. Is there a way to programatically determine it?

I've looked at GetMetaData() but it only returns info about the component (including all the methods) but nothing about which method is currently being called.

like image 738
Ryan Stille Avatar asked Feb 19 '09 21:02

Ryan Stille


People also ask

How do you call a CFC in ColdFusion?

To invoke a component method of a CFC instance, use the cfinvoke tag and specify the following: The CFC instance name, enclosed in number signs (#), in the component attribute. The method name, in the method attribute.

What is a ColdFusion component?

A ColdFusion component (CFC) is a file saved with the extension . cfc. A CFC can contain data and functions. Within a CFC, data is referred to as properties. Although you use the cffunction tag to define functions within a CFC, they are typically referred to as methods instead of functions.


1 Answers

So now 3 ways.

If you are using ColdFusion 9.0 or higher there is now a function named GetFunctionCalledName(). It will return what you are looking for. http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WS7cc222be8a31a47d-6e8b7083122cebfc8f2-8000.html

OR

Use ColdSpring and Aspect Oriented Programming (http://www.coldspringframework.org/coldspring/examples/quickstart/index.cfm?page=aop) to handle this for you.

OR

Use a cfthrow to generate a stack trace that has the information for you:

<cffunction name="determineFunction" output="FALSE" access="public"  returntype="string" hint="" >
<cfset var functionName ="" />
<cfset var i = 0 />
<cfset var stackTraceArray = "" />
<cftry>
<cfthrow />
<cfcatch type="any">
    <cfset stacktraceArray = ListToArray(Replace(cfcatch.stacktrace, "at ", " | ", "All"), "|") />

    <!---Rip the right rows out of the stacktrace --->
    <cfloop index ="i" to="1" from="#ArrayLen(stackTraceArray)#" step="-1">
        <cfif not findNoCase("runFunction", stackTraceArray[i]) or FindNoCase("determineFunction", stackTraceArray[i])>
            <cfset arrayDeleteAt(stackTraceArray, i) />
        </cfif>
    </cfloop>

    <!---Whittle down the string to the func name --->
    <cfset functionName =GetToken(stacktraceArray[1], 1, ".") />
    <cfset functionName =GetToken(functionName, 2, "$")/>
    <cfset functionName =ReplaceNoCase(functionName, "func", "", "once")/>

    <cfreturn functionName />
</cfcatch>
</cftry></cffunction>

My recommendation would be use getFunctionCalledName, or if not on CF 9 ColdSpring, as it will probably buy you some other things.

like image 99
Terry Ryan Avatar answered Oct 02 '22 03:10

Terry Ryan