tsreflect-ioc
Version:
Inversion of control and Dependency injection framework for typescript based on the tsreflect-compiler package.
112 lines (102 loc) • 4.72 kB
text/typescript
/// <reference path="_references.ts" />
import tsreflect = require('tsreflect');
var fs = require('fs');
var log4js = require('log4js');
export module ioc {
export interface IReflectorConfig {
/**
* Defines the source files for compilation. If not set, the compiler searches for .ts files in the current root directory.
**/
sourceFiles: string[];
/**
* Directory where the definitions files located. If this property is set, the compiler automatically adds all .d.ts files which are in this directory or below.
**/
searchForDefinitionFilesIn?: string;
}
/**
* tsreflect wrapper.
* @registerType name:"ioc.Reflector"
**/
export class Reflector {
private symbols: tsreflect.Symbol[];
private ctx: tsreflect.ReflectContext;
logger: any;
constructor() {
this.logger = log4js.getLogger();
}
public initSymbols(config: IReflectorConfig) {
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(file => targetReflectionFiles.push(file));
reflectFiles.forEach(file => {
if (fs.existsSync(file)) {
var reflectionContent = fs.readFileSync(file, 'utf8').trim();
var reflected = JSON.parse(reflectionContent);
if (reflected.references) {
reflected.references.forEach(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');
}
/**
* Returns the symbol for the class with the given name. If the class was not found, null is returned.
* @param {String} className The class name of the class, where you want to get the symbol.
**/
getClassByName(className: string): tsreflect.Symbol {
return this.classes().firstOrDefault(s => s.getFullName()==className);
}
/**
* Returns all classes which are found in the initialized symbol sources.
* @returns {tsreflect.Symbol[]} A list of reflected symbols which are treaded as classes.
*/
public classes(): tsreflect.Symbol[] {
var returnSet = [];
var handleSymbol = (symbol: tsreflect.Symbol) => {
if (symbol.isClass() && !symbol.isMethod() && !symbol.isModule() && !symbol.isProperty()) {
returnSet.push(symbol);
}
symbol.getExports().forEach(symbol => handleSymbol(symbol));
};
this.symbols.forEach(symbol => handleSymbol(symbol));
return returnSet;
}
/**
* Returns all interfaces which are found in the initialized symbol sources.
* @returns {tsreflect.Symbol[]} A list of reflected symbols which are treaded as interfaces.
*/
public interfaces(): tsreflect.Symbol[] {
var returnSet = [];
var handleSymbol = (symbol: tsreflect.Symbol) => {
if (symbol.isInterface()) {
returnSet.push(symbol);
}
symbol.getExports().forEach(symbol => handleSymbol(symbol));
};
this.symbols.forEach(symbol => handleSymbol(symbol));
return returnSet;
}
/**
* Returns all modules which are found in the initialized symbol sources.
* @returns {tsreflect.Symbol[]} A list of reflected symbols which are treaded as modules.
*/
public modules(): tsreflect.Symbol[] {
var returnSet = [];
var handleSymbol = (symbol: tsreflect.Symbol) => {
if (symbol.isModule()) {
returnSet.push(symbol);
}
symbol.getExports().forEach(symbol => handleSymbol(symbol));
};
this.symbols.forEach(symbol => handleSymbol(symbol));
return returnSet;
}
}
}