Changeset 277

Show
Ignore:
Timestamp:
12/19/06 16:36:34 (2 years ago)
Author:
scorfield
Message:

Fixes #73 by adding custom attribute support to plugins.
Added an example plugin that shows how to access plugin parameters and custom attributes.
Updated skeleton fusebox.xml to show how example_plugin could be used.

Location:
framework/trunk
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • framework/trunk/fuseboxApplication.cfc

    r276 r277  
    9393                                        hint="I am the myFusebox data structure." /> 
    9494                 
    95                 <!--- update to ticket 139 ---> 
     95                <!--- fixed ticket 73 ---> 
    9696                <cfset var myVersion = "5.0.1.#REReplace('$LastChangedRevision$','[^0-9]','','all')#" /> 
    9797 
     
    660660                        </cfif> 
    661661                        <cfif structKeyExists(children[i].xmlAttributes,"relative")> 
    662                                 <cfif listFind("true,false,yes,no",children[1].xmlAttributes.relative) eq 0> 
     662                                <cfif listFind("true,false,yes,no",children[i].xmlAttributes.relative) eq 0> 
    663663                                        <cfthrow type="fusebox.badGrammar.invalidAttributeValue" 
    664664                                                        message="Attribute has invalid value"  
     
    882882                        <cfset nn = arrayLen(children[i].xmlChildren) /> 
    883883                        <cfloop from="1" to="#nn#" index="j"> 
    884                                 <cfset plugin = createObject("component","fuseboxPlugin").init(phase,children[i].xmlChildren[j],this) /> 
     884                                <cfset plugin = createObject("component","fuseboxPlugin").init(phase,children[i].xmlChildren[j],this,variables.lexicons) /> 
    885885                                <cfset this.plugins[plugin.getName()][phase] = plugin /> 
    886886                                <cfif not structKeyExists(this.pluginphases,phase)> 
  • framework/trunk/fuseboxPlugin.cfc

    r262 r277  
    5959                <cfargument name="fbApp" type="fuseboxApplication" required="true"  
    6060                                        hint="I am the fusebox application object." /> 
     61                <cfargument name="lexicons" type="struct" required="true"  
     62                                        hint="I am the lexicons declared in the fusebox.xml file that are available as custom attributes." /> 
    6163         
    6264                <cfset var i = 0 /> 
    6365                <cfset var n = arrayLen(arguments.pluginXML.xmlChildren) /> 
    6466                <cfset var attr = 0 /> 
    65                 <cfset var nAttrs = 2 /> 
     67                <cfset var ns = "" /> 
    6668                <cfset var verbChildren = arrayNew(1) /> 
    6769                <cfset var factory = arguments.fbApp.getFuseactionFactory() /> 
     
    7880                        <cfset variables.name = arguments.pluginXML.xmlAttributes.name /> 
    7981                        <cfset variables.fuseboxApplication = arguments.fbApp /> 
     82                        <cfset variables.customAttribs = structNew() /> 
    8083         
    8184                        <cfif not structKeyExists(arguments.pluginXML.xmlAttributes,"template")> 
     
    9194                        <cfif structKeyExists(arguments.pluginXML.xmlAttributes,"path")> 
    9295                                <cfset this.path = this.path & replace(arguments.pluginXML.xmlAttributes.path,"\","/","all") /> 
    93                                 <cfset nAttrs = 3 /> 
    9496                        </cfif> 
    9597                        <cfif right(this.path,1) is not "/"> 
    9698                                <cfset this.path = this.path & "/" /> 
    9799                        </cfif> 
     100                         
     101                        <!--- look for any valid custom attributes ---> 
     102                        <cfloop collection="#arguments.pluginXML.xmlAttributes#" item="attr"> 
     103                                <cfswitch expression="#attr#"> 
     104 
     105                                <cfcase value="name,template,path"> 
     106                                        <!--- already processed ---> 
     107                                </cfcase> 
     108 
     109                                <cfdefaultcase> 
     110 
     111                                        <cfif listLen(attr,":") eq 2> 
     112                                                <!--- looks like a custom attribute: ---> 
     113                                                <cfset ns = listFirst(attr,":") /> 
     114                                                <cfif structKeyExists(arguments.lexicons,ns)> 
     115                                                        <cfset customAttribs[ns][listLast(attr,":")] = arguments.pluginXML.xmlAttributes[attr] /> 
     116                                                <cfelse> 
     117                                                        <cfthrow type="fusebox.badGrammar.undeclaredNamespace"  
     118                                                                        message="Undeclared lexicon namespace"  
     119                                                                        detail="The lexicon prefix '#ns#' was found on a custom attribute in the '#getName()#' plugin declaration in fusebox.xml but no such lexicon namespace has been declared." /> 
     120                                                </cfif> 
     121         
     122                                        <cfelseif arguments.fbApp.strictMode> 
     123                                                <cfthrow type="fusebox.badGrammar.unexpectedAttributes" 
     124                                                                message="Unexpected attributes" 
     125                                                                detail="Unexpected attribute '#attr#' found in the '#getName()#' plugin declaration in fusebox.xml." /> 
     126                                        </cfif> 
     127                                         
     128                                </cfdefaultcase> 
     129 
     130                                </cfswitch> 
     131                        </cfloop> 
     132                         
    98133                        <cfset variables.template = arguments.pluginXML.xmlAttributes.template /> 
    99134                        <cfif len(variables.template) lt 4 or right(variables.template,4) is not ext> 
     
    107142                                <cfset this.rootpath = REreplace(this.rootpath,"[^\.:/]*/\.\./","") /> 
    108143                        </cfloop> 
    109                         <cfif arguments.fbApp.strictMode and structCount(arguments.pluginXML.xmlAttributes) neq nAttrs> 
    110                                 <cfthrow type="fusebox.badGrammar.unexpectedAttributes" 
    111                                                 message="Unexpected attributes" 
    112                                                 detail="Unexpected attributes were found in the '#getName()#' plugin declaration in fusebox.xml." /> 
    113                         </cfif> 
     144                         
    114145                        <cfset variables.parameters = arguments.pluginXML.xmlChildren /> 
    115146                        <cfset variables.paramVerbs = structNew() /> 
    116147                        <cfloop from="1" to="#n#" index="i"> 
     148                                 
    117149                                <cfif not structKeyExists(variables.parameters[i].xmlAttributes,"name")> 
    118150                                        <cfthrow type="fusebox.badGrammar.requiredAttributeMissing" 
     
    120152                                                        detail="The attribute 'name' is required, for a 'parameter' to the '#getName()#' plugin declaration in fusebox.xml." /> 
    121153                                </cfif> 
     154 
    122155                                <cfif not structKeyExists(variables.parameters[i].xmlAttributes,"value")> 
    123156                                        <cfthrow type="fusebox.badGrammar.requiredAttributeMissing" 
     
    125158                                                        detail="The attribute 'value' is required, for a 'parameter' to the '#getName()#' plugin declaration in fusebox.xml." /> 
    126159                                </cfif> 
     160 
    127161                                <cfif arguments.fbApp.strictMode and structCount(variables.parameters[i].xmlAttributes) neq 2> 
    128162                                        <cfthrow type="fusebox.badGrammar.unexpectedAttributes" 
     
    130164                                                        detail="Unexpected attributes were found in the '#variables.parameters[i].xmlAttributes.name#' parameter of the '#getName()#' plugin declaration in fusebox.xml." /> 
    131165                                </cfif> 
     166 
    132167                                <cfset attr = structNew() /> 
    133168                                <cfset attr.name = "myFusebox.plugins.#getName()#.parameters." & variables.parameters[i].xmlAttributes.name /> 
    134169                                <cfset attr.value = variables.parameters[i].xmlAttributes.value /> 
    135170                                <cfset variables.paramVerbs[i] = factory.create("set",this,attr,verbChildren) /> 
     171 
    136172                        </cfloop> 
    137173                <cfelse> 
     
    194230        </cffunction> 
    195231         
     232        <cffunction name="getCustomAttributes" returntype="struct" access="public" output="false"  
     233                                hint="I return the custom (namespace-qualified) attributes for this plugin tag."> 
     234                <cfargument name="ns" type="string" required="true"  
     235                                        hint="I am the namespace prefix whose attributes should be returned." /> 
     236                 
     237                <cfif structKeyExists(variables.customAttribs,arguments.ns)> 
     238                        <!--- we structCopy() this so folks can't poke values back into the metadata! ---> 
     239                        <cfreturn structCopy(variables.customAttribs[arguments.ns]) /> 
     240                <cfelse> 
     241                        <cfreturn structNew() /> 
     242                </cfif> 
     243                 
     244        </cffunction> 
     245         
    196246</cfcomponent> 
  • framework/trunk/myFusebox.cfc

    r276 r277  
    5151<cfcomponent hint="I provide the per-request myFusebox data structure and some convenience methods."> 
    5252        <cfscript> 
    53         // update to ticket 139 
     53        // fixed ticket 73 
    5454        this.version.runtime     = "5.0.1.#REReplace('$LastChangedRevision$','[^0-9]','','all')#"; 
    5555           
  • framework/trunk/skeleton/fusebox.xml.cfm

    r266 r277  
    44        Example fusebox.xml control file. Shows how to define circuits, classes, 
    55        parameters and global fuseactions. 
     6 
     7        This is just a test namespace for the plugin custom attribute example 
    68--> 
    7 <fusebox> 
     9<fusebox xmlns:test="test"> 
    810        <circuits> 
    911                <!-- illustrates defaults for parent ("") and relative ("true") --> 
     
    5355                <phase name="preProcess"> 
    5456                        <!-- 
    55                         <plugin name="prePP" template="preProcess"> 
    56                                 <parameter name="abc" value="123" /> 
     57                        <plugin name="prePP" template="example_plugin" test:abc="123"> 
     58                                <parameter name="def" value="456" /> 
    5759                        </plugin> 
    5860                        -->