-
Notifications
You must be signed in to change notification settings - Fork 6
/
Application.cfc
174 lines (109 loc) · 3.66 KB
/
Application.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
component {
createMapping();
setupApplication();
public any function onApplicationStart() {
structDelete(application, "coldmvc");
var framework = getFramework();
structAppend(this.mappings, framework.getMappings(), false);
structAppend(this.mappings, framework.getPluginManager().getMappings(), false);
structAppend(this.mappings, framework.getLibraryManager().getMappings(), false);
application.coldmvc = {
framework = framework,
mappings = this.mappings
};
application.coldmvc.framework.onApplicationStart();
}
private void function createMapping() {
var rootPath = getRootPath();
if (_directoryExists(rootPath & "coldmvc/")) {
// check if coldmvc is nested inside the current project
this.mappings["/coldmvc"] = rootPath & "coldmvc/";
} else if (_directoryExists(expandPath("/coldmvc"))) {
// if coldmvc can be found either in the webroot or by a server mapping, create an app mapping for it
this.mappings["/coldmvc"] = sanitizePath(expandPath("/coldmvc"));
}
}
private any function getFramework() {
if (isDefined("application") && structKeyExists(application, "coldmvc") && structKeyExists(application.coldmvc, "framework")) {
return application.coldmvc.framework;
}
if (!structKeyExists(request, "framework")) {
var rootPath = getRootPath();
// make sure the mapping works as expected, otherwise default to executing as if inside the coldmvc directory
if (_fileExists(expandPath("/coldmvc/Framework.cfc"))) {
request.framework = new coldmvc.Framework(rootPath, "coldmvc.");
} else {
request.framework = new Framework(rootPath, "");
}
}
return request.framework;
}
private string function getRootPath() {
var rootPath = getDirectoryFromPath(getBaseTemplatePath());
var directory = listlast(rootPath, "/\");
if (directory == "public") {
rootPath = left(rootPath, len(rootPath) - 7);
}
return rootPath;
}
public any function onSessionStart() {
getFramework().onSessionStart();
}
public any function onSessionEnd() {
getFramework().onSessionEnd();
}
public any function onRequestStart() {
if (getFramework().requiresReload()) {
reload();
}
structAppend(this.mappings, application.coldmvc.mappings, false);
getFramework().onRequestStart();
}
private void function reload() {
ormReload();
structDelete(application, "coldmvc");
structDelete(request, "framework");
onApplicationStart();
getFramework().onApplicationReload();
}
public any function onRequestEnd() {
getFramework().onRequestEnd();
}
private void function setupApplication() {
var framework = getFramework();
var rootPath = framework.getRootPath();
var currentDirectory = listLast(rootPath, "/");
if (!structKeyExists(this, "name")) {
this.name = currentDirectory & "_" & hash(rootPath);
}
this.sessionManagement = true;
structAppend(this, framework.getDefaultSettings(), false);
structAppend(this.mappings, framework.getMappings(), false);
structAppend(this.ormSettings, framework.getDefaultORMSettings(), false);
framework.applyConfigSettings(this);
}
private boolean function _fileExists(required string path) {
var result = false;
try {
result = fileExists(arguments.path);
}
catch (any e) {
}
return result;
}
private boolean function _directoryExists(required string path) {
var result = false;
try {
result = directoryExists(arguments.path);
}
catch (any e) {}
return result;
}
private string function sanitizePath(required string path) {
arguments.path = replace(arguments.path, "\", "/", "all");
if (right(arguments.path, 1) != "/") {
arguments.path = arguments.path & "/";
}
return arguments.path;
}
}