1- import { Definition , FactoryMethod , DefinitionObjectType } from './definition' ;
2- import { ContainerResolver } from "./resolver" ;
3- import { ContainerFactoryInterface } from "./containerfactoryinterface" ;
4- import { ContainerFactory } from "./containerfactory" ;
1+ import { Definition , FactoryMethod , DefinitionObjectType } from './definition' ;
2+ import { ContainerResolver } from "./resolver" ;
3+ import { ContainerFactoryInterface } from "./containerfactoryinterface" ;
4+ import { ContainerFactory } from "./containerfactory" ;
55
66/**
77 * DI Container class
88 */
99export class Container {
1010
11- /**
12- * Resolver
13- */
14- private resolver : ContainerResolver ;
11+ /**
12+ * Resolver
13+ */
14+ private resolver : ContainerResolver ;
1515
1616
17- /**
18- * If true allows to resolve unregistered definitions
19- * @type {boolean }
20- */
21- private allowUnregisteredResolve : Boolean = false ;
17+ /**
18+ * If true allows to resolve unregistered definitions
19+ * @type {boolean }
20+ */
21+ private allowUnregisteredResolve : Boolean = false ;
2222
23- /**
24- * @constructor
25- */
26- public constructor ( ) {
27- this . resolver = new ContainerResolver ( ) ;
28- let containerFactory = new ContainerFactory ( this . resolver ) ;
23+ /**
24+ * @constructor
25+ */
26+ public constructor ( ) {
27+ this . resolver = new ContainerResolver ( ) ;
28+ let containerFactory = new ContainerFactory ( this . resolver ) ;
2929
30- this . register ( ContainerFactoryInterface , containerFactory ) ;
31- }
30+ this . register ( ContainerFactoryInterface , containerFactory ) ;
31+ }
3232
33- /**
34- * Sets if unregistered definitions are allowed to be resolved
35- * This is useful to avoid many stub container.register(Class) class, but you need to be careful to not create
36- * interface instead of implementation
37- * @param allow true to allow, false to disallow
38- */
39- public setAllowUnregisteredResolving ( allow : Boolean ) {
40- this . allowUnregisteredResolve = allow ;
41- }
33+ /**
34+ * Sets if unregistered definitions are allowed to be resolved
35+ * This is useful to avoid many stub container.register(Class) class, but you need to be careful to not create
36+ * interface instead of implementation
37+ * @param allow true to allow, false to disallow
38+ */
39+ public setAllowUnregisteredResolving ( allow : Boolean ) {
40+ this . allowUnregisteredResolve = allow ;
41+ }
4242
43- /**
44- * Register implementation to interface object
45- * @param definition
46- * @param implementationOrConstructorArgs
47- * @param constructorArgs
48- */
49- public register ( definition : string | Function , implementationOrConstructorArgs ?: Object | Function | Array < any > , constructorArgs ?: Array < any > ) : Definition {
50- let def : Definition ;
51- if ( ! implementationOrConstructorArgs ) {
52- // specific case: register(Class)
53- if ( typeof definition === "string" ) {
54- throw new Error ( "Can't register just symbol" ) ;
55- }
56- def = new Definition ( definition , definition ) ;
43+ /**
44+ * Register implementation to interface object
45+ * @param definition
46+ * @param implementationOrConstructorArgs
47+ * @param constructorArgs
48+ */
49+ public register ( definition : string | Function , implementationOrConstructorArgs ?: Object | Function | Array < any > , constructorArgs ?: Array < any > ) : Definition {
50+ let def : Definition ;
51+ if ( ! implementationOrConstructorArgs ) {
52+ // specific case: register(Class)
53+ if ( typeof definition === "string" ) {
54+ throw new Error ( "Can't register just symbol" ) ;
55+ }
56+ def = new Definition ( definition , definition ) ;
57+ } else {
58+ if ( typeof definition === "function" ) {
59+ // Cases:
60+ // 1. register(Class, Class)
61+ // 2. register(Class, [constructorArgs])
62+ // 3. register(Class, Class, [constructorArgs])
63+ // 4. register(Class, object)
64+ if ( implementationOrConstructorArgs instanceof Array ) {
65+ // Case 2.
66+ def = new Definition ( definition , definition , implementationOrConstructorArgs ) ;
5767 } else {
58- if ( typeof definition === "function" ) {
59- // Cases:
60- // 1. register(Class, Class)
61- // 2. register(Class, [constructorArgs])
62- // 3. register(Class, Class, [constructorArgs])
63- // 4. register(Class, object)
64- if ( implementationOrConstructorArgs instanceof Array ) {
65- // Case 2.
66- def = new Definition ( definition , definition , implementationOrConstructorArgs ) ;
67- } else {
68- if ( typeof implementationOrConstructorArgs == "object" ) {
69- // Case 4
70- def = new Definition ( definition , implementationOrConstructorArgs , null , FactoryMethod . OBJECT ) ;
71- } else {
72- // Cases 1, 3
73- def = new Definition ( definition , implementationOrConstructorArgs , constructorArgs ) ;
74- }
75- }
76- } else {
77- // Cases:
78- // 1. register('string', Class)
79- // 2. register('string', Class, [constructorArgs])
80- // 3. register('string', 'number|string|object|boolean')
81- if ( typeof implementationOrConstructorArgs === "function" ) {
82- // Case 1,2
83- def = new Definition ( definition , implementationOrConstructorArgs , constructorArgs ) ;
84- } else {
85- // Case 3
86- def = new Definition ( definition , implementationOrConstructorArgs , null , FactoryMethod . OBJECT ) ;
87- }
88-
89- }
68+ if ( typeof implementationOrConstructorArgs == "object" ) {
69+ // Case 4
70+ def = new Definition ( definition , implementationOrConstructorArgs , undefined , FactoryMethod . OBJECT ) ;
71+ } else {
72+ // Cases 1, 3
73+ def = new Definition ( definition , implementationOrConstructorArgs , constructorArgs ) ;
74+ }
75+ }
76+ } else {
77+ // Cases:
78+ // 1. register('string', Class)
79+ // 2. register('string', Class, [constructorArgs])
80+ // 3. register('string', 'number|string|object|boolean')
81+ if ( typeof implementationOrConstructorArgs === "function" ) {
82+ // Case 1,2
83+ def = new Definition ( definition , implementationOrConstructorArgs , constructorArgs ) ;
84+ } else {
85+ // Case 3
86+ def = new Definition ( definition , implementationOrConstructorArgs , undefined , FactoryMethod . OBJECT ) ;
9087 }
91- this . resolver . addDefinition ( definition , def ) ;
92- return def ;
93- }
9488
95- /**
96- * Register definition as callable. The callable will be invoked instead calling via new()
97- */
98- public registerCallable ( definition : string | Function , callable : ( ) => any ) : Definition {
99- let def : Definition ;
100- def = new Definition ( definition , callable , null , FactoryMethod . SINGLETON , DefinitionObjectType . CALLABLE ) ;
101- this . resolver . addDefinition ( definition , def ) ;
102- return def ;
89+ }
10390 }
91+ this . resolver . addDefinition ( definition , def ) ;
92+ return def ;
93+ }
10494
105- /**
106- * Resolves definition
107- * @param definition
108- * @param method
109- */
110- public resolve ( definition : string | Function , method ?: FactoryMethod ) : any {
111- return this . resolver . resolve ( definition , method , undefined , ! this . allowUnregisteredResolve ) ;
112- }
95+ /**
96+ * Register definition as callable. The callable will be invoked instead calling via new()
97+ */
98+ public registerCallable ( definition : string | Function , callable : ( ) => any ) : Definition {
99+ let def : Definition ;
100+ def = new Definition ( definition , callable , undefined , FactoryMethod . SINGLETON , DefinitionObjectType . CALLABLE ) ;
101+ this . resolver . addDefinition ( definition , def ) ;
102+ return def ;
103+ }
104+
105+ /**
106+ * Resolves definition
107+ * @param definition
108+ * @param method
109+ */
110+ public resolve ( definition : string | Function , method ?: FactoryMethod ) : any {
111+ return this . resolver . resolve ( definition , method , undefined , ! this . allowUnregisteredResolve ) ;
112+ }
113113}
0 commit comments