-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cfc
executable file
·281 lines (220 loc) · 11.1 KB
/
Util.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<cfcomponent hint="The util component provides utility methods for the rest of the application">
<cfset variables.rootpath = "">
<cfset variables.packageroot = "">
<cfset variables.directorySeparator = "">
<cfset variables.nativetypes = "any,array,binary,boolean,date,guid,numeric,query,string,struct,uuid,variablename,void">
<cfset variables.paths = "">
<cffunction name="init" access="public" returntype="Util" output="false" hint="Initializes the component and sets up the variables that the other methods in the component need.">
<cfargument name="rootpath" type="string" required="yes">
<cfargument name="packageRoot" type="string" required="yes">
<cfargument name="directorySeparator" type="string" required="yes">
<cfargument name="paths" type="array" required="true">
<cfset variables.rootpath = arguments.rootpath>
<cfset variables.packageRoot = arguments.packageRoot>
<cfset variables.directorySeparator = arguments.directorySeparator>
<cfset variables.paths = arguments.paths>
<cfreturn this>
</cffunction>
<cffunction name="setRootPath" access="public" returntype="void">
<cfargument name="rootpath" required="true" type="string">
<cfset variables.rootpath = arguments.rootpath>
</cffunction>
<cffunction name="getDetailURL" access="public" returntype="string" output="false" hint="Figures out whether or not the passed string is a component in the current package. If so it generates the appropriate URL to view the documentation for that component. If not it returns a non-functioning URL.">
<cfargument name="str" type="string" required="true" hint="Any string to be turned into a detail link">
<cfargument name="currentpath" type="string" required="true" hint="The path to the directory containing the current component">
<cfset result = "" />
<cftry>
<cfset result = "content.cfm?file=#getFilePath(str, currentpath)#" />
<cfcatch type="UnknownComponentException">
<cfset result = "unknowncomponent.cfm" />
</cfcatch>
<cfcatch type="NativeTypeException">
<cfset result = "nativetypes.cfm##detail_#cfcatch.detail#" />
</cfcatch>
</cftry>
<cfreturn result />
</cffunction>
<cffunction name="getFilePath" access="public" returntype="string" output="false" hint="Figures out whether or not the passed string is a component in the current package. If so it generates the appropriate file path to that component. If not it throws an exception." throws="UnknownComponentException,NativeTypeException">
<cfargument name="str" type="string" required="true" hint="Any string to be turned into a detail link">
<cfargument name="currentpath" type="string" required="true" hint="The path to the directory containing the current component">
<cfset var path = "">
<cfset var filePath = "">
<cfset var matches = structNew()>
<cfset var prefix = "">
<cfset var newpath = "">
<cfset var subPath = "">
<cfif listFirst(str,'./') eq variables.packageRoot>
<cfset filePath = variables.rootpath & variables.directorySeparator & listChangeDelims(listRest(arguments.str,'./'),variables.directorySeparator,'.') & '.cfc'>
<cfelseif reFind("[/\.]",str)>
<cfset str = listChangeDelims(str,'.','./')>
<cfloop from="1" to="#arrayLen(variables.paths)#" index="i">
<cfif findNoCase(variables.paths[i].prefix,str)>
<cfset path = variables.paths[i].path>
<cfset prefix = variables.paths[i].prefix>
</cfif>
</cfloop>
<cfif path EQ variables.rootpath>
<cfset str = listRest(str, "/.") />
<cfset path = "" />
</cfif>
<cfif len(path) GT 0>
<cfset str = listChangeDelims(str,variables.directorySeparator,'/.')>
<cfset str = listRest(str, variables.directorySeparator) />
<cfloop list="#path#" index="i" delimiters="#variables.directorySeparator#">
<cfset newpath = listAppend(newPath,i,variables.directorySeparator)>
<cfset subPath = replaceNoCase(path,newPath & variables.directorySeparator,"")>
<cfif len(subPath) AND left(str,len(subPath)) EQ subPath>
<cfbreak>
</cfif>
</cfloop>
<cfset filePath = newpath>
<cfset filePath = filePath & variables.directorySeparator & str & '.cfc'>
<cfelse>
<cfset str = listChangeDelims(str, variables.directorySeparator, "/.") />
<cfset filePath = variables.rootpath & variables.directorySeparator & str & ".cfc" />
</cfif>
<cfelseif not listFindNoCase(variables.nativetypes,str)>
<cfset path = arguments.currentpath & arguments.str & '.cfc'>
<cflock type="READONLY" name="cfcdocfileaccess" timeout="5">
<cfif fileExists(path)>
<cfset filePath = path />
<cfelse>
<cfthrow type="UnknownComponentException" />
</cfif>
</cflock>
<cfelse>
<cfthrow type="NativeTypeException"
detail="#str#" />
</cfif>
<cfreturn filePath>
</cffunction>
<cffunction name="getPackageFromPath" access="public" returntype="string" output="false" hint="The getPackageFromPath method gets the package path for any given directory under the root path.">
<cfargument name="path">
<cfset var prefix = "">
<cfset var subPath = "">
<cfset var package = "">
<cfset var matches = structNew()>
<cfset var packagepath = "">
<cfloop from="1" to="#arrayLen(variables.paths)#" index="i">
<cfif findNoCase("#variables.paths[i].path#",path)>
<cfset packagepath = variables.paths[i].path>
<cfset prefix = variables.paths[i].prefix>
</cfif>
</cfloop>
<cfif len(packagepath)>
<cfset subPath = prefix & variables.directorySeparator & replaceNoCase(arguments.path,packagepath,'')>
<cfif listLast(subPath,'.') is 'cfc'>
<cfset subPath = listDeleteAt(subPath,listLen(subPath,'/\'),'/\')>
</cfif>
<cfset package = listChangeDelims(subPath,'.','/\')>
<cfelse>
<cfset package = "Unknown package">
</cfif>
<cfreturn package>
</cffunction>
<cffunction name="getPackageDisplayName" access="public" output="true" returntype="string"
hint="I take a full package name and return the proper display string to represent it">
<cfargument name="package" type="string" required="true" hint="The package name to translate for display" />
<cfset package = getPackagePathFromRoot(package) />
<cfif len(trim(package)) EQ 0>
<cfset package = "<em>default package</em>" />
</cfif>
<cfreturn package />
</cffunction>
<cffunction name="getPackageDisplayPrefix" access="public" output="false" returntype="string"
hint="I take a full package name and return the proper display string to represent it as a prefix for a fully qualified CFC name">
<cfargument name="package" type="string" required="true" hint="The package name to translate for display" />
<cfset package = getPackagePathFromRoot(package) />
<cfif len(trim(package)) GT 0>
<cfset package = package & "." />
</cfif>
<cfreturn package />
</cffunction>
<cffunction name="getPackagePathFromRoot" access="private" output="false" returntype="string"
hint="I take a full package name, and return the relative path from the root">
<cfargument name="package" type="string" required="true" hint="The package name to translate" />
<cfset var i = "" />
<cfloop from="1" to="#arrayLen(variables.paths)#" index="i">
<cfif left(package, len(variables.paths[i].prefix)) EQ variables.paths[i].prefix>
<cfset package = removeChars(package, 1, len(variables.paths[i].prefix)) />
<cfif len(trim(package)) NEQ 0>
<!--- kill the leading period --->
<cfset package = removeChars(package, 1, 1) />
</cfif>
<cfreturn package />
</cfif>
</cfloop>
<cfthrow type="UnknownPackageRootException"
message="The package root from the full package path was not recognized" />
</cffunction>
<cffunction name="getCFCInformation" access="public" output="false" returntype="struct"
hint="I create complete CFC information for the specified CFC, including information about all it's superclasses">
<cfargument name="file" type="string" required="true" hint="the file to generate information about" />
<cfset var fileContents = "" />
<cfset var parser = "" />
<cfset var stComponent = structNew() />
<cfset var filename = "" />
<cfset var filepath = "" />
<cfset var i = "" />
<cfif NOT REFind("(.cfc)$",arguments.file)>
<!--- agressively trimmed off the .cfc in createSetup() fix it --->
<cfset arguments.file = arguments.file & ".cfc" />
</cfif>
<cftry>
<cffile action="read" file="#arguments.file#" variable="fileContents">
<cfcatch type="any">
<!--- TODO: Errors here relate to reading framework type files or other external cfc groups (ignore for now) --->
</cfcatch>
</cftry>
<cfif len(trim(filecontents))>
<cfset parser = createObject('component','CFCParser')>
<cfset parser.init()>
<cfset stComponent = parser.parse(fileContents)>
<cfset filename = listLast(file,'/\')>
<cfset filepath = replace(file,filename,'')>
<cfset stComponent.name = listFirst(filename,'.')>
<cfset stComponent.path = filepath>
<cfset stComponent.package = getPackageFromPath(file)>
<cfset stComponent.pathindex = 1>
<cfloop from="1" to="#arrayLen(paths)#" index="i">
<cfif findNoCase(paths[i].path,file)>
<cfset stComponent.pathindex = i>
<cfbreak>
</cfif>
</cfloop>
<cfset stComponent.code = fileContents>
<cfset variables.objectCache[file] = stComponent />
<!--- duplicate to definitively sever from the cache --->
<cfset stComponent = duplicate(stComponent) />
<!--- If the component extends another and is not a third party framework parse that one as well --->
<cfif stComponent.attributes.extends NEQ "cfcomponent" AND NOT isFrameworkFile(stComponent.attributes.extends)>
<cfset stComponent.superComponent = getCFCInformation(getFilePath(stComponent.attributes.extends, stComponent.path)) />
</cfif>
</cfif>
<cfreturn stComponent />
</cffunction>
<cffunction name="isFrameworkFile" access="public" output="false" returntype="boolean">
<cfargument name="component" type="string" required="true" />
<cfset var frameworkList = "mxunit,machii,coldspring" />
<cfif ListFindNoCase(frameworkList,ListFirst(arguments.component,"."))>
<cfreturn true />
</cfif>
<cfreturn false />
</cffunction>
<cffunction name="getMachIIAppPath" access="public" output="false" returntype="string">
<cfargument name="appKey" type="string" required="true" />
<cfif StructKeyExists(application, "public") AND StructKeyExists(application.public, "appLoader")>
<cfreturn "public" />
<cfelse>
<cfif StructKeyExists(application, arguments.appKey)>
<cfreturn arguments.appKey />
<cfelse>
<cfthrow type="EXCEPTION_APPNAME_NOT_FOUND" message="Giving up can't find the mach-ii application name" />
</cfif>
</cfif>
</cffunction>
<cffunction name="unescapeCreatedCode" access="public" output="false" returntype="string">
<cfargument name="code" type="string" required="true" />
<cfreturn trim(Replace(Replace(arguments.code, "<", "<","ALL"), ">", ">" ,"ALL")) />
</cffunction>
</cfcomponent>