fis-postpackager-json2php
Version:
a postpackager plugin for transfer json to php
130 lines (117 loc) • 4.86 kB
JavaScript
/**
* @file 读取map表的js对象转成标准的php值,写入新建的map.php文件。
* 支持传递写入的js对象、新建的php文件路径。
* @author hclscut
*/
;
module.exports = function (ret, conf, setting, opt) {
var root = fis.project.getProjectPath();
var ns = fis.config.get('namespace');
if (typeof setting.needMap === 'undefined' || setting.needMap === true) {
var map = fis.file(root, (ns ? ns + '-' : '') + 'map.php');
map.setContent('<?php \n return ' + var_export(ret.map, true) + ';');
ret.pkg[map.subpath] = map;
ret.ids[map.getId()] = map;
}
if (setting.path && setting.transferObj) {
var file = fis.file(setting.path);
file.setContent('<?php \n return ' + var_export(setting.transferObj, true) + ';');
ret.pkg[file.subpath] = file;
ret.ids[map.getId()] = file;
}
/**
* 根据传递的JS对象,返回符合php值语法的字符串
*
* @param mixed_expression JS对象
* @param bool_return 当前显示还是返回给接受对象,参考php var_export
* @returns {*}
*/
function var_export(mixed_expression, bool_return) {
var retstr = '',
iret = '',
value,
cnt = 0,
x = [],
i = 0,
funcParts = [],
// We use the last argument (not part of PHP) to pass in
// our indentation level
idtLevel = arguments[2] || 2,
innerIndent = '',
outerIndent = '',
_makeIndent = function(idtLevel) {
return (new Array(idtLevel + 1))
.join(' ');
},
__getType = function(inp) {
var i = 0,
match, types, cons, type = typeof inp;
if (type === 'function') {
return 'function';
}
if (type === 'object' && !inp) {
return 'null'; // Should this be just null?
}
if (type === 'object') {
if (!inp.constructor) {
return 'object';
}
cons = inp.constructor.toString();
match = cons.match(/(\w+)\(/);
if (match) {
cons = match[1].toLowerCase();
}
types = ['boolean', 'number', 'string', 'array'];
for (i = 0; i < types.length; i++) {
if (cons === types[i]) {
type = types[i];
break;
}
}
}
return type;
};
var type = __getType(mixed_expression);
if (type === null) {
retstr = 'NULL';
} else if (type === 'array' || type === 'object') {
outerIndent = _makeIndent(idtLevel - 2);
innerIndent = _makeIndent(idtLevel);
for (i in mixed_expression) {
value = var_export(mixed_expression[i], 1, idtLevel + 2);
/*console.log(value);
value = typeof value === 'string' ? value.replace(/</g, '<')
.
replace(/>/g, '>') : value;*/
x[cnt++] = innerIndent + '"' + i + '"' + ' => ' +
(__getType(mixed_expression[i]) === 'array' ?
'\n' : '') + value;
}
iret = x.join(',\n');
retstr = outerIndent + 'array (\n' + iret + '\n' + outerIndent + ')';
} else if (type === 'function') {
funcParts = mixed_expression.toString()
.
match(/function .*?\((.*?)\) \{([\s\S]*)\}/);
// For lambda functions, var_export() outputs such as the following:
// '\000lambda_1'. Since it will probably not be a common use to
// expect this (unhelpful) form, we'll use another PHP-exportable
// construct, create_function() (though dollar signs must be on the
// variables in JavaScript); if using instead in JavaScript and you
// are using the namespaced version, note that create_function() will
// not be available as a global
retstr = "create_function ('" + funcParts[1] + "', '" +
funcParts[2].replace(new RegExp("'", 'g'), "\\'") + "')";
} else {
retstr = typeof mixed_expression !== 'string' ? mixed_expression :
"'" + mixed_expression.replace(/(["'])/g, '\\$1')
.
replace(/\0/g, '\\0') + "'";
}
if (!bool_return) {
console.log(retstr);
return null;
}
return retstr;
}
};