-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
79 lines (71 loc) · 2.03 KB
/
index.js
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
const path = require('path')
const { execSync } = require('child_process')
const settings = {}
settings.disableRegisterGlobalModel = false
function run (script) {
try {
const stdout = execSync(`php ${path.join(__dirname, script)}`)
return stdout.toString()
} catch (err) {
throw new Error(`PHP process exited with code ${err.status}`)
}
}
function runWithData (template, model) {
if (!model) model = {}
model._TEMPLATE = template
if (typeof model._REGISTER_GLOBAL_MODEL === 'undefined') {
if (settings.disableRegisterGlobalModel) {
model._REGISTER_GLOBAL_MODEL = false
} else {
model._REGISTER_GLOBAL_MODEL = true
}
}
model._REGISTER_GLOBAL_MODEL = !!model._REGISTER_GLOBAL_MODEL
model._VIEWS_PATH = model?.settings?.views || './'
const jsonModel = JSON.stringify(model, circular())
try {
const stdout = execSync(`php ${path.join(__dirname, '/loader.php')}`, {
input: jsonModel
})
return stdout.toString()
} catch (err) {
throw new Error(`PHP process exited with code ${err.status}`)
}
}
function __express (template, model, callback) {
try {
const stdout = runWithData(template, model)
callback(null, stdout)
} catch (err) {
callback(err)
}
}
function disableRegisterGlobalModel () {
settings.disableRegisterGlobalModel = true
}
function enableRegisterGlobalModel () {
settings.disableRegisterGlobalModel = false
}
function circular (ref, methods) {
ref = ref || '[Circular]'
const seen = []
return function (key, val) {
if (typeof val === 'function' && methods) {
val = val.toString()
}
if (!val || typeof (val) !== 'object') {
return val
}
if (~seen.indexOf(val)) {
if (typeof ref === 'function') return ref(val)
return ref
}
seen.push(val)
return val
}
}
module.exports.run = run
module.exports.runWithData = runWithData
module.exports.__express = __express
module.exports.disableRegisterGlobalModel = disableRegisterGlobalModel
module.exports.enableRegisterGlobalModel = enableRegisterGlobalModel