tsreflect-ioc
Version:
Inversion of control and Dependency injection framework for typescript based on the tsreflect-compiler package.
94 lines (88 loc) • 4.08 kB
JavaScript
define(["require", "exports", 'tsreflect'], function(require, exports, tsreflect) {
var fs = require('fs');
var log4js = require('log4js');
(function (ioc) {
var Reflector = (function () {
function Reflector() {
this.logger = log4js.getLogger();
}
Reflector.prototype.initSymbols = function (config) {
this.logger.info('Loading symbols from files: ' + JSON.stringify(config.sourceFiles) + '...');
this.ctx = this.ctx || tsreflect.createContext();
var reflectFiles = config.sourceFiles, targetReflectionFiles = [];
config.sourceFiles.forEach(function (file) {
return targetReflectionFiles.push(file);
});
reflectFiles.forEach(function (file) {
if (fs.existsSync(file)) {
var reflectionContent = fs.readFileSync(file, 'utf8').trim();
var reflected = JSON.parse(reflectionContent);
if (reflected.references) {
reflected.references.forEach(function (ref) {
if (!targetReflectionFiles.any(ref)) {
targetReflectionFiles.push(ref);
}
});
}
}
});
reflectFiles = targetReflectionFiles;
this.symbols = (this.symbols || []).concat(this.ctx.loadSync(reflectFiles));
this.logger.info('...done Loading symbols from files, ' + this.symbols.length + ' symbols loaded');
};
Reflector.prototype.getClassByName = function (className) {
return this.classes().firstOrDefault(function (s) {
return s.getFullName() == className;
});
};
Reflector.prototype.classes = function () {
var returnSet = [];
var handleSymbol = function (symbol) {
if (symbol.isClass() && !symbol.isMethod() && !symbol.isModule() && !symbol.isProperty()) {
returnSet.push(symbol);
}
symbol.getExports().forEach(function (symbol) {
return handleSymbol(symbol);
});
};
this.symbols.forEach(function (symbol) {
return handleSymbol(symbol);
});
return returnSet;
};
Reflector.prototype.interfaces = function () {
var returnSet = [];
var handleSymbol = function (symbol) {
if (symbol.isInterface()) {
returnSet.push(symbol);
}
symbol.getExports().forEach(function (symbol) {
return handleSymbol(symbol);
});
};
this.symbols.forEach(function (symbol) {
return handleSymbol(symbol);
});
return returnSet;
};
Reflector.prototype.modules = function () {
var returnSet = [];
var handleSymbol = function (symbol) {
if (symbol.isModule()) {
returnSet.push(symbol);
}
symbol.getExports().forEach(function (symbol) {
return handleSymbol(symbol);
});
};
this.symbols.forEach(function (symbol) {
return handleSymbol(symbol);
});
return returnSet;
};
return Reflector;
})();
ioc.Reflector = Reflector;
})(exports.ioc || (exports.ioc = {}));
var ioc = exports.ioc;
});