Site Tree

Regular Expression for Finding Unqualified Component Names

Using fully-qualified component names is always a good idea. It reduces the risk of incorrectly using a wrong component in case there are multiple components with the same name on a ColdFusion server. Also, when using ShadoCMS, it makes the code more robust as the Component Caching mechanism can correctly override the components if required.

However you develop sites with ShadoCMS, unqualified component references might creep into your code. Here is a quick way to locate such references.

The Bad

This is an example of an unqualified component name:

<cfset oPages = application.shado_obj_factory.get("shado_obj_pages")>

In the above example, we are retrieving an instance of shado_obj_pages.cfc using the Component Cache mechanism (also known as shado_obj_factory). The code will work, but component cache has to guess where does "shado_obj_pages" live and then instantiate it from there. If there are multiple shado_obj_pages.cfc components in your webroot, then there are chances of the incorrect component being instantiated, cached and returned.

The Good

Here is an example of a fully qualified component name -- the way your code should always reference components:

<cfset oPages = application.shado_obj_factory.get("shadomx.core.pages.shado_obj_pages")>

Notice that the component name is fully qualified with the path.

The Beautiful

Yesterday, while reviewing code for a site, I had the need to go through a very large site to find out all references to unqualified component names. One option was to do a simple search for the word "application.shado_obj_factory" in the site folder. However, this would have meant eye-balling all results (and there were lots of them) for unqualified component names.

The other, more elegant and more efficient, option was to use a regular expression that searches for the patttern application.shado_obj_factory(""), and finds all instances of its usage where a period (.) was not used inside the quotes.

Here is that regular expression.

(application.shado_obj_factory.get\(['|"])([^'|"])([a-zA-z]+[^.])(['|"]\))

To use this regular expression, you can either use Eclipse or any other tool (like BBEdit on Mac OSX) that allows regular expression search across files. You should also be able to use 'grep' or 'egrep' at command line. If using Eclipse or BBEdit or similar tool, make sure that you check for regular expression (and not plain string).

You can similarly write your own regular expression to find all createObject() or <cfobject> calls. If you do, please feel free to post it as a comment.


Comments

Add a comment

Sure, it works (once you change "A-z" to "A-Z"), but it could have been simplified right down:

(application.shado_obj_factory.get\(['"][a-zA-Z\.]+['"]\))

# Posted by: Joel Cass| 11 Dec 2008 | 08:08 PM


or even better:

application.shado_obj_factory.get\(['|"][^'|"|\.]+['|"]\)

no point having all those brackets since you can't use the backrefs/grouping.

# Posted by: Nick Shearer | 01 Apr 2009 | 12:47 PM


and for createObject functions you can use this regexp:

createObject\(\s*['|"]component['|"]\s*,\s*['|"][^'|"|\.]+['|"]\s*\)

# Posted by: Nick Shearer | 01 Apr 2009 | 12:54 PM

Add Comment