Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFC extends sibling folder

I've seen all kinds of solutions for extending cfcs in parent folders with access to parent files or the CF administration, but I have not seen a workable solution to extend a cfc in a "shared"/sibling folder without access to parent folders.

This solution requires access to parent folders? (Not sure what these mappings are, but I have no access to Application.cfc anyway)

This solution requires being able to create an application.cfc which doesn't work for me (creating one in myApp does nothing because the environment I'm in includes the index page in myApp and builds the from there... the client never directly calls it to fire recognition of Application.cfc)

For instance:

  • wwwroot/some/path/myApp/Shared/Base.cfc
  • wwwroot/some/path/myApp/Function/Function.cfc

I'm looking to be able to call functionality in the Base.cfc (which contains common methods used in the application) from the Function.cfc via super and extension.

I have access to all files within myApp, but not "wwwroot", "some", or "path."

In order to extend Base.cfc within Function.cfc I have to extend the full path of "some.path.myApp.Shared.Base" This would cause problems if someone renamed the myApp folder since I would have to hand edit every Function.cfc that extends that Base.cfc

What I'm looking to do it create an application specific "barrier" so if the app folder name changes it will not cause mass havoc that requires I edit all the CFC files that use functionality from the Base.cfc.

Since I can't use relative paths to the Base ("..Shared.Base") I'm wondering if there's a way to create a CFC in the myApp folder that I can extend from and alleviate the renaming headache if it were to occur or a way to give it a generic name like "myApp" and extend from there. (myApp.Shared.Base)

I do not have access to Application.cfm, nor the Coldfusion administration.

like image 420
Andir Avatar asked Oct 22 '10 20:10

Andir


2 Answers

Personally I would go simpler way: incapsulate the Base into the Function.

Looks like you want to use the set of core components for some common functionality. If this is true -- incapsulation even more applicable.

Paths to objects can be built dynamically, for example (step-by-step process for easier reading):

<cfscript>

    path1 = GetDirectoryFromPath(cgi.SCRIPT_NAME);
    path2 = ListToArray(path1, "/");
    path3 = path2;
    path3[ArrayLen(path3)] = "shared";
    path4 = ArrayToList(path3, ".");
    path5 = ArrayToList(path2, ".");

    myBase = CreateObject("component", "#path4#.Base");

    myFunction = CreateObject("component", "#path5#.Function").init(myBase);

</cfscript>

In the Function create function init:

<cffunction name="init">
    <cfargument name="base">
    <cfset variables.Base = arguments.base />
    <cfreturn this />
</cffunction>

Of course, you may have strong reasons for extending, but at least this approach is not sensitive to the parent directories renaming.

like image 131
Sergey Galashyn Avatar answered Oct 06 '22 01:10

Sergey Galashyn


If Base.cfc doesn't extend another cfc then you may be able to include the Base.cfc file into another cfc file in your function folder.

For example create a cfc file in the function folder with content:

<cfinclude template="../shared/base.cfc" />

Then extend the new file instead of the cfc in the shared folder.

like image 45
Dan Roberts Avatar answered Oct 05 '22 23:10

Dan Roberts