-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFramework.cfc
More file actions
649 lines (426 loc) · 13.7 KB
/
Framework.cfc
File metadata and controls
649 lines (426 loc) · 13.7 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
component accessors="true" {
property componentPrefix;
property fileSystem;
property loaded;
property mappings;
property rootPath;
property settings;
public any function init(required string rootPath, required string componentPrefix) {
setComponentPrefix(arguments.componentPrefix);
setRootPath(arguments.rootPath);
setLoaded(false);
return this;
}
public any function setRootPath(required string rootPath) {
variables.rootPath = getFileSystem().sanitizeDirectory(arguments.rootPath);
return this;
}
public any function getFileSystem() {
if (!structKeyExists(variables, "fileSystem")) {
variables.fileSystem = create("util.FileSystem");
}
return variables.fileSystem;
}
public struct function getMappings() {
if (!structKeyExists(variables, "mappings")) {
var rootPath = getRootPath();
var fileSystem = getFileSystem();
var defaults = {
"/config" = rootPath & "config/",
"/public" = rootPath & "public/",
"/app" = rootPath & "app/",
"/generated" = rootPath & ".generated/",
"/views" = rootPath & ".generated/views/",
"/layouts" = rootPath & ".generated/layouts/",
"/tags" = rootPath & ".generated/tags/"
};
if (fileSystem.directoryExists(rootPath & "plugins/")) {
defaults["/plugins"] = rootPath & "plugins/";
} else if (fileSystem.directoryExists(expandPath("/plugins"))) {
defaults["/plugins"] = fileSystem.sanitizeDirectory(expandPath("/plugins"));
}
variables.mappings = defaults;
}
return variables.mappings;
}
public struct function getSettings() {
if (!structKeyExists(variables, "settings")) {
var environment = "default";
var settings = {};
var rootPath = getRootPath();
var fileSystem = getFileSystem();
var configPath = rootPath & "config/config.ini";
if (fileSystem.fileExists(configPath)) {
var ini = create("config.Ini", {
filePath = configPath,
parentSection = "default"
});
// check to see if there's an environment file
var environmentPath = rootPath & "config/environment.txt";
if (fileSystem.fileExists(environmentPath)) {
environment = fileRead(environmentPath);
}
var section = ini.getSection(environment);
structAppend(settings, section, true);
}
var defaults = {
"controller" = "index",
"development" = false,
"environment" = environment,
"https" = "auto",
"layout" = "index",
"reloadKey" = "init",
"reloadPassword" = "",
"rootPath" = rootPath,
"sesURLs" = false
};
// override any default settings
structAppend(settings, defaults, false);
if (!structKeyExists(settings, "autoReload")) {
settings["autoReload"] = settings["development"];
}
if (!structKeyExists(settings, "debug")) {
settings["debug"] = settings["development"];
}
if (!structKeyExists(settings, "urlPath")) {
if (settings["sesURLs"]) {
settings["urlPath"] = replaceNoCase(cgi.script_name, "/index.cfm", "");
} else {
settings["urlPath"] = cgi.script_name;
}
}
if (!structKeyExists(settings, "assetPath")) {
var assetPath = replaceNoCase(settings["urlPath"], "index.cfm", "");
if (assetPath == "/") {
assetPath = "";
} else if (right(assetPath, 1) == "/") {
assetPath = left(assetPath, len(assetPath) - 1);
}
settings["assetPath"] = assetPath;
}
variables.settings = settings;
}
return variables.settings;
}
public any function getSetting(required string key) {
var settings = getSettings();
if (structKeyExists(settings, arguments.key)) {
return settings[arguments.key];
} else {
return "";
}
}
public struct function getDefaultSettings() {
var rootPath = getRootPath();
var currentDirectory = listLast(rootPath, "/");
var settings = getSettings();
var defaults = {
serverSideFormValidation = false,
sessionTimeout = createTimeSpan(0, 2, 0, 0),
mappings = {},
ormEnabled = true,
ormSettings = {}
};
if (structKeyExists(settings, "datasource")) {
if (settings.datasource != "") {
defaults.datasource = settings.datasource;
} else {
defaults.ormEnabled = false;
}
} else {
defaults.datasource = currentDirectory;
}
if (structKeyExists(settings, "ormEnabled")) {
defaults.ormEnabled = settings.ormEnabled;
}
return defaults;
}
public struct function getDefaultORMSettings() {
var rootPath = getRootPath();
var fileSystem = getFileSystem();
var defaults = {
cfclocation = [ rootPath ],
dbcreate = "update",
eventHandler = "coldmvc.orm.EventHandler",
eventHandling = true,
namingStrategy = "coldmvc.orm.NamingStrategy",
flushAtRequestEnd = false
};
// check to see if a hibernate mapping file exists
if (fileSystem.fileExists(rootPath & "/config/hibernate.hbmxml")) {
// if autoGenMap hasn't been explicitly set already, don't generate the mapping files if they have one
if (!structKeyExists(defaults, "autoGenMap")) {
defaults.autoGenMap = false;
}
// if saveMapping hasn't been set already, don't generate the mapping files if they have one
if (!structKeyExists(defaults, "saveMapping")) {
defaults.saveMapping = true;
}
} else {
// if saveMapping hasn't been set already, don't generate the mapping files if they have one
if (!structKeyExists(defaults, "saveMapping")) {
defaults.saveMapping = false;
}
}
return defaults;
}
public any function applyConfigSettings(required any context) {
var settings = getSettings();
// check to see if any application settings are specified inside the config
if (structKeyExists(settings, "this")) {
var key = "";
for (key in settings.this) {
var value = settings.this[key];
if (isSimpleValue(value)) {
// allow for simple settings like timeouts
arguments.context[key] = value;
} else if (isStruct(value)) {
// allow for handling of nested structs like ormSettings
if (!structKeyExists(arguments.context, key)) {
arguments.context[key] = {};
}
structAppend(arguments.context[key], value, true);
}
}
}
return this;
}
public any function getPluginManager() {
if (!structKeyExists(variables, "pluginManager")) {
variables.pluginManager = create("system.PluginManager", {
configPath = "/config/plugins.cfm",
fileSystem = getFileSystem()
});
}
return variables.pluginManager;
}
public any function getLibraryManager() {
if (!structKeyExists(variables, "libraryManager")) {
variables.libraryManager = create("system.LibraryManager", {}, {
pluginManager = getPluginManager(),
fileSystem = getFileSystem()
});
}
return variables.libraryManager;
}
public any function getBeanFactory() {
return variables.internalBeanFactory;
}
public boolean function containsBean(required string beanName) {
return getBeanFactory().containsBean(arguments.beanName);
}
public any function getBean(required string beanName) {
return getBeanFactory().getBean(arguments.beanName);
}
public any function getApplication() {
return variables.applicationBeanFactory;
}
public any function onApplicationStart() {
variables.internalBeanFactory = createInternalBeanFactory();
variables.applicationBeanFactory = createApplicationBeanFactory();
dispatchEvent("preApplication");
dispatchEvent("applicationStart");
setLoaded(true);
return this;
}
public any function onApplicationReload() {
// only try to reload the framework if it's already loaded in case the initial startup failed
if (getLoaded()) {
dispatchEvent("postReload");
getBean("debugManager").setReloaded();
}
return this;
}
public any function onSessionStart() {
dispatchEvent("sessionStart");
return this;
}
public any function onSessionEnd() {
dispatchEvent("sessionEnd");
return this;
}
public boolean function requiresReload() {
var autoReload = getSetting("autoReload");
var reloadKey = getSetting("reloadKey");
var reloadPassword = getSetting("reloadPassword");
if (getSetting("autoReload")) {
return true;
} else if (structKeyExists(url, reloadKey)) {
if (reloadPassword == "" || url[reloadKey] == reloadPassword) {
return true;
}
} else if (!getLoaded()) {
return true;
}
return false;
}
public any function onRequestStart() {
dispatchEvent("preRequest");
dispatchEvent("requestStart");
return this;
}
public any function onRequestEnd() {
try {
dispatchEvent("requestEnd");
dispatchEvent("postRequest");
}
catch(any e) {
}
return this;
}
private any function createInternalBeanFactory() {
var xml = findBeans("/config/framework.xml");
var beans = {
"fileSystem" = {
class = "coldmvc.util.FileSystem",
instance = getFileSystem()
},
"framework" = {
class = "coldmvc.Framework",
instance = this
},
"libraryManager" = {
class = "coldmvc.system.LibraryManager",
instance = getLibraryManager()
},
"pluginManager" = {
class = "coldmvc.system.PluginManager",
instance = getPluginManager()
}
};
var beanFactory = create("beans.BeanFactory", {
xml = xml,
config = getSettings(),
beans = beans
});
beanFactory.loadBeans();
beanFactory.getBean("config").setSettings(getSettings());
return beanFactory;
}
private any function createApplicationBeanFactory() {
var xml = findBeans("/config/beans.xml");
var factory = getBeanFactory();
var factoryPostProcessors = [
factory.getBean("beanFinder")
];
var beanPostProcessors = [
factory.getBean("frameworkBeanInjector"),
factory.getBean("modelInjector")
];
var beanFactory = create("beans.BeanFactory", {
xml = xml,
config = getSettings(),
beans = {},
factoryPostProcessors = factoryPostProcessors,
beanPostProcessors = beanPostProcessors
});
var beanInjector = create("beans.BeanInjector", {}, {
beanFactory = beanFactory
});
beanFactory.addBeanPostProcessor(beanInjector);
beanFactory.loadBeans();
return beanFactory;
}
private any function findBeans(required string xmlPath) {
var beans = xmlNew();
beans.xmlRoot = xmlElemNew(beans, "beans");
beans.xmlRoot.xmlAttributes["default-autowire"] = "byName";
addBeans(beans, arguments.xmlPath);
var plugins = getPluginManager().getPlugins();
var i = "";
for (i = 1; i <= arrayLen(plugins); i++) {
addBeans(beans, plugins[i].path & arguments.xmlPath);
}
addBeans(beans, "/coldmvc" & arguments.xmlPath);
var xml = toString(beans);
xml = replace(xml, "#chr(60)#!----#chr(62)#", "", "all");
return xml;
}
private any function addBeans(required xml beans, required string configPath) {
var fileSystem = getFileSystem();
if (!fileSystem.fileExists(arguments.configPath)) {
arguments.configPath = expandPath(arguments.configPath);
}
if (fileSystem.fileExists(arguments.configPath)) {
var content = fileRead(arguments.configPath);
if (isXML(content)) {
var xml = xmlParse(content);
var i = "";
var j = "";
var beanDefs = xmlSearch(xml, "/beans/bean");
for (i = 1; i <= arrayLen(beanDefs); i++) {
var bean = beanDefs[i];
var exists = xmlSearch(arguments.beans, "beans/bean[@id='#bean.xmlAttributes.id#']");
if (arrayLen(exists) == 0) {
var imported = xmlImport(arguments.beans, bean);
for (j = 1; j <= arrayLen(imported); j++) {
arrayAppend(arguments.beans.xmlRoot.xmlChildren, imported[j]);
}
}
}
var imports = xmlSearch(xml, "/beans/import");
for (i = 1; i <= arrayLen(imports); i++) {
addBeans(arguments.beans, imports[i].xmlAttributes.resource);
}
}
}
return this;
}
private any function xmlImport(required any destination, required any source) {
var node = xmlElemNew(arguments.destination, arguments.source.xmlName);
var i = "";
structAppend(node.xmlAttributes, arguments.source.xmlAttributes);
node.xmlText = arguments.source.xmlText;
node.xmlComment = arguments.source.xmlComment;
for (i = 1; i <= arrayLen(arguments.source.xmlChildren); i++) {
arrayAppend(node.xmlChildren, xmlImport(arguments.destination, arguments.source.xmlChildren[i]));
}
return node;
}
public void function dispatchEvent(required string event, struct data) {
if (!structKeyExists(arguments, "data")) {
arguments.data = {};
}
getBean("eventDispatcher").dispatchEvent(arguments.event, arguments.data);
getEventDispatcher().dispatchEvent(arguments.event, arguments.data);
}
public any function getEventDispatcher() {
if (!structKeyExists(variables, "eventDispatcher")) {
var eventDispatcher = create("events.EventDispatcher", {}, {
beanFactory = getApplication(),
debugManager = getBean("debugManager"),
fileSystem = getBean("fileSystem"),
flashManager = getBean("flashManager"),
requestManager = getBean("requestManager"),
logEvents = true
});
var metaDataObserver = create("metadata.MetaDataObserver", {}, {
beanFactory = getApplication(),
eventDispatcher = eventDispatcher,
metaDataFlattener = getBean("metaDataFlattener")
});
eventDispatcher.addObserver("preApplication", metaDataObserver, "findObservers");
variables.eventDispatcher = eventDispatcher;
}
return variables.eventDispatcher;
}
private any function create(required string class, struct constructorArgs, struct properties) {
var object = createObject("component", variables.componentPrefix & arguments.class);
if (!structKeyExists(arguments, "constructorArgs")) {
arguments.constructorArgs = {};
}
if (structKeyExists(object, "init")) {
object.init(argumentCollection=arguments.constructorArgs);
}
if (!structKeyExists(arguments, "properties")) {
arguments.properties = {};
}
var key = "";
for (key in arguments.properties) {
if (structKeyExists(object, "set#key#")) {
evaluate("object.set#key#(arguments.properties[key])");
}
}
return object;
}
}