root / framework / tags / fusebox5RC1 / fuseboxPlugin.cfc

Revision 202, 9.2 kB (checked in by scorfield, 3 years ago)

Fixes #82 by moving all 'special' request variables into request.fusebox and moving the tracing machinery into myFusebox (and therefore deleting the fuseboxTrace component).

Line 
1<!---
2Fusebox Software License
3Version 1.0
4
5Copyright (c) 2003, 2004, 2005, 2006 The Fusebox Corporation. All rights reserved.
6
7Redistribution and use in source and binary forms, with or without modification, are permitted
8provided that the following conditions are met:
9
101. Redistributions of source code must retain the above copyright notice, this list of conditions
11   and the following disclaimer.
12
132. Redistributions in binary form or otherwise encrypted form must reproduce the above copyright
14   notice, this list of conditions and the following disclaimer in the documentation and/or other
15   materials provided with the distribution.
16
173. The end-user documentation included with the redistribution, if any, must include the following
18   acknowledgment:
19
20   "This product includes software developed by the Fusebox Corporation (http://www.fusebox.org/)."
21
22   Alternately, this acknowledgment may appear in the software itself, if and wherever such
23   third-party acknowledgments normally appear.
24
254. The names "Fusebox" and "Fusebox Corporation" must not be used to endorse or promote products
26   derived from this software without prior written (non-electronic) permission. For written
27   permission, please contact fusebox@fusebox.org.
28
295. Products derived from this software may not be called "Fusebox", nor may "Fusebox" appear in
30   their name, without prior written (non-electronic) permission of the Fusebox Corporation. For
31   written permission, please contact fusebox@fusebox.org.
32
33If one or more of the above conditions are violated, then this license is immediately revoked and
34can be re-instated only upon prior written authorization of the Fusebox Corporation.
35
36THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38DISCLAIMED. IN NO EVENT SHALL THE FUSEBOX CORPORATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY
39DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45-------------------------------------------------------------------------------
46
47This software consists of voluntary contributions made by many individuals on behalf of the
48Fusebox Corporation. For more information on Fusebox, please see <http://www.fusebox.org/>.
49
50--->
51<cfcomponent output="false" hint="I represent a plugin declaration.">
52
53        <cffunction name="init" returntype="fuseboxPlugin" access="public" output="false"
54                                hint="I am the constructor.">
55                <cfargument name="phase" type="string" required="true"
56                                        hint="I am the phase with which this plugin is associated." />
57                <cfargument name="pluginXML" type="any" required="true"
58                                        hint="I am the XML representation of this plugin's declaration." />
59                <cfargument name="fbApp" type="fuseboxApplication" required="true"
60                                        hint="I am the fusebox application object." />
61       
62                <cfset var i = 0 />
63                <cfset var n = arrayLen(arguments.pluginXML.xmlChildren) />
64                <cfset var attr = 0 />
65                <cfset var nAttrs = 2 />
66                <cfset var verbChildren = arrayNew(1) />
67                <cfset var factory = arguments.fbApp.getFuseactionFactory() />
68                <cfset var ext = "." & arguments.fbApp.scriptFileDelimiter />
69               
70                <cfif not structKeyExists(arguments.pluginXML.xmlAttributes,"name")>
71                        <cfthrow type="fusebox.badGrammar.requiredAttributeMissing"
72                                        message="Required attribute is missing"
73                                        detail="The attribute 'name' is required, for a '#arguments.phase#' plugin declaration in fusebox.xml." />
74                </cfif>
75               
76                <cfset variables.name = arguments.pluginXML.xmlAttributes.name />
77                <cfset variables.fuseboxApplication = arguments.fbApp />
78
79                <cfif not structKeyExists(arguments.pluginXML.xmlAttributes,"template")>
80                        <cfthrow type="fusebox.badGrammar.requiredAttributeMissing"
81                                        message="Required attribute is missing"
82                                        detail="The attribute 'template' is required, for the '#getName()#' plugin declaration in fusebox.xml." />
83                </cfif>
84
85                <cfset variables.phase = arguments.phase />
86                <cfif arguments.pluginXML.xmlName is "plugin">
87                        <cfset this.path = arguments.fbApp.getPluginsPath() />
88                        <cfif structKeyExists(arguments.pluginXML.xmlAttributes,"path")>
89                                <cfset this.path = this.path & replace(arguments.pluginXML.xmlAttributes.path,"\","/","all") />
90                                <cfset nAttrs = 3 />
91                        </cfif>
92                        <cfif right(this.path,1) is not "/">
93                                <cfset this.path = this.path & "/" />
94                        </cfif>
95                        <cfset variables.template = arguments.pluginXML.xmlAttributes.template />
96                        <cfif len(variables.template) lt 4 or right(variables.template,4) is not ext>
97                                <cfset variables.template = variables.template & ext />
98                        </cfif>
99                        <cfset this.rootpath =
100                                        arguments.fbApp.relativePath(arguments.fbApp.getApplicationRoot() &
101                                                                                                        this.path,arguments.fbApp.getApplicationRoot()) />
102                        <!--- remove pairs of directory/../ to form canonical path: --->
103                        <cfloop condition="find('/../',this.rootpath) gt 0">
104                                <cfset this.rootpath = REreplace(this.rootpath,"[^\.:/]*/\.\./","") />
105                        </cfloop>
106                        <cfif arguments.fbApp.strictMode and structCount(arguments.pluginXML.xmlAttributes) neq nAttrs>
107                                <cfthrow type="fusebox.badGrammar.unexpectedAttributes"
108                                                message="Unexpected attributes"
109                                                detail="Unexpected attributes were found in the '#getName()#' plugin declaration in fusebox.xml." />
110                        </cfif>
111                        <cfset variables.parameters = arguments.pluginXML.xmlChildren />
112                        <cfset variables.paramVerbs = structNew() />
113                        <cfloop from="1" to="#n#" index="i">
114                                <cfif not structKeyExists(variables.parameters[i].xmlAttributes,"name")>
115                                        <cfthrow type="fusebox.badGrammar.requiredAttributeMissing"
116                                                        message="Required attribute is missing"
117                                                        detail="The attribute 'name' is required, for a 'parameter' to the '#getName()#' plugin declaration in fusebox.xml." />
118                                </cfif>
119                                <cfif not structKeyExists(variables.parameters[i].xmlAttributes,"value")>
120                                        <cfthrow type="fusebox.badGrammar.requiredAttributeMissing"
121                                                        message="Required attribute is missing"
122                                                        detail="The attribute 'value' is required, for a 'parameter' to the '#getName()#' plugin declaration in fusebox.xml." />
123                                </cfif>
124                                <cfif arguments.fbApp.strictMode and structCount(variables.parameters[i].xmlAttributes) neq 2>
125                                        <cfthrow type="fusebox.badGrammar.unexpectedAttributes"
126                                                        message="Unexpected attributes"
127                                                        detail="Unexpected attributes were found in the '#variables.parameters[i].xmlAttributes.name#' parameter of the '#getName()#' plugin declaration in fusebox.xml." />
128                                </cfif>
129                                <cfset attr = structNew() />
130                                <cfset attr.name = "myFusebox.plugins.#getName()#.parameters." & variables.parameters[i].xmlAttributes.name />
131                                <cfset attr.value = variables.parameters[i].xmlAttributes.value />
132                                <cfset variables.paramVerbs[i] = factory.create("set",this,attr,verbChildren) />
133                        </cfloop>
134                <cfelse>
135                        <cfthrow type="fusebox.badGrammar.illegalDeclaration"
136                                        message="Illegal declaration"
137                                        detail="The XML entity '#arguments.pluginXML.xmlName#' was found where a plugin declaration was expected in fusebox.xml." />
138                </cfif>
139       
140                <cfreturn this />
141               
142        </cffunction>
143       
144        <cffunction name="compile" returntype="void" access="public" output="false"
145                                hint="I compile this plugin object.">
146                <cfargument name="writer" type="any" required="false"
147                                        hint="I am the parsed file writer object. I am required but it's faster to specify that I am not required." />
148               
149                <cfset var i = 0 />
150                <cfset var n = structCount(variables.paramVerbs) />
151                <cfset var file = "" />
152                <cfset var p = "" />
153               
154                <cfif request.__fusebox.SuppressPlugins>
155                        <cfreturn />
156                </cfif>
157                <cfswitch expression="#variables.phase#">
158                <cfcase value="processError,fuseactionException">
159                        <cffile action="read" file="#variables.fuseboxApplication.getApplicationRoot()##this.path##variables.template#"
160                                        variable="file"
161                                        charset="#variables.fuseboxApplication.characterEncoding#" />
162                        <cfset arguments.writer.rawPrintln(file) />
163                </cfcase>
164                <cfdefaultcase>
165                        <cfloop from="1" to="#n#" index="i">
166                                <cfset variables.paramVerbs[i].compile(arguments.writer) />
167                        </cfloop>
168                        <cfset p = arguments.writer.setPhase(variables.phase) />
169                        <cfset arguments.writer.println('<cfset myFusebox.thisPlugin = "#getName()#"/>') />
170                        <cfset arguments.writer.print('<' & 'cfoutput><' & 'cfinclude template=') />
171                        <cfset arguments.writer.print('"#variables.fuseboxApplication.parseRootPath##this.path##variables.template#"') />
172                        <cfset arguments.writer.println('/><' & '/cfoutput>') />
173                        <cfset arguments.writer.setPhase(p) />
174                </cfdefaultcase>
175                </cfswitch>
176
177        </cffunction>
178       
179        <cffunction name="getName" returntype="string" access="public" output="false"
180                                hint="I return the name of the plugin.">
181               
182                <cfreturn variables.name />
183               
184        </cffunction>
185
186        <cffunction name="getCircuit" returntype="any" access="public" output="false"
187                                hint="I return the enclosing application object. This is an edge case to allow code that works with fuseactions to work with plugins too.">
188       
189                <cfreturn variables.fuseboxApplication />
190       
191        </cffunction>
192       
193</cfcomponent>
Note: See TracBrowser for help on using the browser.