@@ -15,8 +15,7 @@ import { Core, Controller, Model, Service, Repository } from '../constants/Targe
1515import { events , EventEmitter } from './api/events' ;
1616import { Log } from './log' ;
1717
18-
19- import { User } from '../api/models/User' ;
18+ const log = new Log ( 'core:IoC' ) ;
2019
2120
2221class IoC {
@@ -39,7 +38,9 @@ class IoC {
3938 public async bindModules ( ) : Promise < void > {
4039 this . bindCore ( ) ;
4140 await this . bindModels ( ) ;
42- // this.bindControllers();
41+ await this . bindRepositories ( ) ;
42+ await this . bindServices ( ) ;
43+ await this . bindControllers ( ) ;
4344
4445 this . container = this . customConfiguration ( this . container ) ;
4546 }
@@ -49,36 +50,68 @@ class IoC {
4950 this . container . bind < EventEmitter > ( Types . Core ) . toConstantValue ( events ) . whenTargetNamed ( Core . Events ) ;
5051 }
5152
52- private async bindControllers ( ) : Promise < void > {
53- this . getFiles ( '/controllers' , ( files : string [ ] ) => {
54- files . forEach ( ( file : any ) => {
55- console . log ( file ) ;
56- const fileExport = require ( `${ file . path } /${ file . fileName } ` ) ;
57- console . log ( fileExport ) ;
58- this . container
59- . bind < interfaces . Controller > ( Types . Controller )
60- . to ( fileExport [ file . name ] )
61- . whenTargetNamed ( Controller [ file . name ] ) ;
62- } ) ;
53+ private bindModels ( ) : Promise < void > {
54+ return this . bindFiles ( '/models' , Model , ( name : any , value : any ) => {
55+ this . container
56+ . bind < any > ( Types . Model )
57+ . toConstantValue ( value )
58+ . whenTargetNamed ( name ) ;
6359 } ) ;
6460 }
6561
66- private bindModels ( ) : Promise < void > {
67- return new Promise < void > ( ( resolve , reject ) => {
68- console . log ( 'Models' ) ;
69- this . getFiles ( '/models' , ( files : string [ ] ) => {
70- files . forEach ( ( file : any ) => {
71- const fileExport = require ( ` ${ file . path } / ${ file . fileName } ` ) ;
72- this . validateExport ( fileExport [ file . name ] ) ;
73- this . validateTarget ( Model , file . name ) ;
62+ private bindRepositories ( ) : Promise < void > {
63+ return this . bindFiles ( '/repositories' , Repository , ( name : any , value : any ) => {
64+ this . container
65+ . bind < any > ( Types . Repository )
66+ . to ( value )
67+ . whenTargetNamed ( name ) ;
68+ } ) ;
69+ }
7470
75- this . container
76- . bind < any > ( Types . Model )
77- . toConstantValue ( fileExport [ file . name ] )
78- . whenTargetNamed ( Model [ file . name ] ) ;
71+ private bindServices ( ) : Promise < void > {
72+ return this . bindFiles ( '/services' , Service , ( name : any , value : any ) => {
73+ this . container
74+ . bind < any > ( Types . Service )
75+ . to ( value )
76+ . whenTargetNamed ( name ) ;
77+ } ) ;
78+ }
7979
80- resolve ( ) ;
80+ private bindControllers ( ) : Promise < void > {
81+ return this . bindFiles ( '/controllers' , Controller , ( name : any , value : any ) => {
82+ this . container
83+ . bind < any > ( Types . Controller )
84+ . to ( value )
85+ . whenTargetNamed ( name ) ;
86+ } ) ;
87+ }
88+
89+ private bindFiles ( path : string , target : any , callback : ( name : any , value : any ) => void ) : Promise < void > {
90+ return new Promise < void > ( ( resolve , reject ) => {
91+ this . getFiles ( path , ( files : string [ ] ) => {
92+ files . forEach ( ( file : any ) => {
93+ let fileExport ;
94+ try {
95+ fileExport = require ( `${ file . path } /${ file . fileName } ` ) ;
96+ } catch ( e ) {
97+ log . warn ( e . message ) ;
98+ return ;
99+ }
100+ if ( fileExport === undefined ) {
101+ log . warn ( `Could not find the file ${ file . name } !` ) ;
102+ return ;
103+ }
104+ if ( fileExport [ file . name ] === undefined ) {
105+ log . warn ( `Name of the file '${ file . name } ' does not match to the class name!` ) ;
106+ return ;
107+ }
108+ if ( target && target [ file . name ] === undefined ) {
109+ log . warn ( `Please define your '${ file . name } ' class is in the target constants.` ) ;
110+ return ;
111+ }
112+ callback ( target [ file . name ] , fileExport [ file . name ] ) ;
81113 } ) ;
114+ resolve ( ) ;
82115 } ) ;
83116 } ) ;
84117 }
@@ -92,7 +125,8 @@ class IoC {
92125 private getFiles ( path : string , done : ( files : any [ ] ) => void ) : void {
93126 fs . readdir ( this . getBasePath ( ) + path , ( err : any , files : string [ ] ) : void => {
94127 if ( err ) {
95- console . error ( err ) ;
128+ log . warn ( `Could not read the folder ${ path } !` ) ;
129+ return ;
96130 }
97131 done ( files . map ( ( fileName : string ) => ( {
98132 path : this . getBasePath ( ) + path ,
@@ -106,18 +140,6 @@ class IoC {
106140 return fileName . substring ( 0 , fileName . length - 3 ) ;
107141 }
108142
109- private validateExport ( value : any ) : void {
110- if ( ! value ) {
111- throw new Error ( `${ value } is not defined in the target constants` ) ;
112- }
113- }
114-
115- private validateTarget ( target : any , value : any ) : void {
116- if ( target && target [ value ] === undefined ) {
117- throw new Error ( `${ value } is not defined in the target constants` ) ;
118- }
119- }
120-
121143}
122144
123145export const ioc = new IoC ( ) ;
0 commit comments