@@ -8,10 +8,6 @@ import hex.di.error.MissingClassDescriptionException;
88import hex .di .error .MissingMappingException ;
99import hex .di .mapping .InjectionMapping ;
1010import hex .di .provider .IDependencyProvider ;
11- import hex .di .reflect .ClassDescription ;
12- import hex .di .reflect .FastClassDescriptionProvider ;
13- import hex .di .reflect .IClassDescriptionProvider ;
14- import hex .di .reflect .InjectionUtil ;
1511import hex .error .NullPointerException ;
1612import hex .event .ITrigger ;
1713import hex .event .ITriggerOwner ;
@@ -22,7 +18,7 @@ import hex.util.ClassUtil;
2218 * ...
2319 * @author Francis Bourre
2420 */
25- class Injector
21+ class Injector
2622 #if !macro
2723 implements ITriggerOwner
2824 #end
@@ -32,33 +28,23 @@ class Injector
3228 var _processedMapping : Map <String ,Bool >;
3329 var _managedObjects : ArrayMap <Dynamic , Dynamic >;
3430 var _parentInjector : Injector ;
35- var _classDescriptor : IClassDescriptionProvider ;
36-
37- public var trigger ( default , never ) : ITrigger <IInjectorListener >;
3831
32+ public var trigger ( default , never ) : ITrigger <IInjectorListener >;
33+
3934 public function new ()
4035 {
41- this ._classDescriptor = new FastClassDescriptionProvider ();
42-
4336 this ._mapping = new Map ();
4437 this ._processedMapping = new Map ();
4538 this ._managedObjects = new ArrayMap ();
4639 }
4740
4841 public function createChildInjector () : Injector
4942 {
50- var injector = new Injector ();
51- injector .setParent ( this ) ;
43+ var injector = new Injector ();
44+ injector ._parentInjector = this ;
5245 return injector ;
5346 }
54-
55- // TODO write test
56- public function setParent ( injector : Injector ) : Void
57- {
58- this ._parentInjector = injector ;
59- }
6047
61- //
6248 public function addListener ( listener : IInjectorListener ) : Bool
6349 {
6450 return this .trigger .connect ( listener );
@@ -104,7 +90,7 @@ class Injector
10490 }
10591 }
10692
107- public function getInstanceWithClassName <T >( className : String , name : String = ' ' , targetType : Class <Dynamic > = null ) : T
93+ public function getInstanceWithClassName <T >( className : String , name : String = ' ' , targetType : Class <Dynamic > = null , shouldThrowAnError : Bool = true ) : T
10894 {
10995 var mappingID = className + ' |' + name ;
11096 var mapping = cast this ._mapping [ mappingID ];
@@ -117,11 +103,15 @@ class Injector
117103 {
118104 return this ._parentInjector .getInstanceWithClassName ( className , name , targetType );
119105 }
120- else
106+ else if ( shouldThrowAnError )
121107 {
122- throw new MissingMappingException ( " Injector is missing a mapping to get instance with type '" +
123- className + " '. Target dependency: '" + mappingID + " '" );
108+ var errorMessage = " Injector is missing a mapping to get instance with type '" + className + " '" ;
109+ errorMessage + = ( targetType != null ) ? " in class '" + Type .getClassName ( targetType ) + " '." : " ." ;
110+ errorMessage + = " Target dependency: '" + mappingID + " '" ;
111+ throw new MissingMappingException ( errorMessage );
124112 }
113+
114+ return null ;
125115 }
126116
127117 public function getProvider <T >( className : String , name : String = ' ' ) : IDependencyProvider <T >
@@ -142,27 +132,40 @@ class Injector
142132 return null ;
143133 }
144134 }
135+
136+ inline function _getClassDescription ( type : Class <Dynamic > )
137+ {
138+ return Reflect .getProperty ( type , " __INJECTION" );
139+ }
145140
146141 public function instantiateUnmapped <T >( type : Class <T > ) : T
147142 {
148- if ( type == null )
149- {
150- throw new NullPointerException ( ' class description cannot be null' );
151- }
152-
153- var classDescription = this ._classDescriptor .getClassDescription ( type );
143+ #if debug
144+ if ( type == null ) throw new NullPointerException ( ' type cant be null' );
145+ #end
154146
155- var instance : T ;
156- if ( classDescription != null && classDescription . c != null )
147+ var instance : Dynamic ;
148+ try
157149 {
158- instance = InjectionUtil .applyConstructorInjection ( type , this , classDescription .c .a , type );
159- this ._applyInjection ( instance , type , classDescription );
150+ instance = ( cast type ).__ac ( this .getInstanceWithClassName );
160151 }
161- else
152+ catch ( e : Dynamic )
162153 {
163154 instance = Type .createInstance ( type , [] );
164155 }
165156
157+ try
158+ {
159+ if ( instance .__ai != null )
160+ {
161+ this ._applyInjection ( instance , type );
162+ }
163+ }
164+ catch ( e : Dynamic )
165+ {
166+
167+ }
168+
166169 return instance ;
167170 }
168171
@@ -227,24 +230,15 @@ class Injector
227230 {
228231 var mappingID = this ._getMappingID ( type , name );
229232 var mapping = cast this ._mapping [ mappingID ];
230-
231- if ( mapping != null )
232- {
233- return mapping .provider != null ;
234- }
235- else
236- {
237- return false ;
238- }
233+ return ( mapping != null ) ? mapping .provider != null : false ;
239234 }
240235
241236 public function injectInto ( target : Dynamic ) : Void
242237 {
243- var targetType : Class <Dynamic > = Type .getClass ( target );
244- var classDescription = this ._classDescriptor .getClassDescription ( targetType );
245- if ( classDescription != null )
238+ var targetType = Type .getClass ( target );
239+ if ( target .__ai != null )
246240 {
247- this ._applyInjection ( target , targetType , classDescription );
241+ this ._applyInjection ( target , targetType );
248242 }
249243 else
250244 {
@@ -256,19 +250,23 @@ class Injector
256250
257251 public function destroyInstance ( instance : Dynamic ) : Void
258252 {
259- if ( ! Reflect .isFunction ( instance ) )
253+ // #if php
254+ if ( this ._managedObjects .containsKey ( instance ) )
260255 {
261256 this ._managedObjects .remove ( instance );
262-
263- var classDescription = this ._classDescriptor .getClassDescription ( Type .getClass ( instance ) );
264- if ( classDescription != null )
265- {
266- for ( preDestroy in classDescription .pd )
267- {
268- InjectionUtil .applyMethodInjection ( instance , this , Type .getClass ( instance ), preDestroy .a , preDestroy .m );
269- }
270- }
257+ instance .__ap ();
271258 }
259+ /* #else
260+ try
261+ {
262+ this._managedObjects.remove( instance );
263+ instance.__ap();
264+ }
265+ catch ( e : Dynamic )
266+ {
267+
268+ }
269+ #end*/
272270 }
273271
274272 public function map <T >( type : Class <T >, name : String = ' ' ) : InjectionMapping <T >
@@ -372,23 +370,32 @@ class Injector
372370 return mapping ;
373371 }
374372
375- function _applyInjection ( target : Dynamic , targetType : Class <Dynamic >, classDescription : ClassDescription ) : Void
373+ function _applyInjection ( target : Dynamic , targetType : Class <Dynamic > ) : Void
376374 {
377375 #if !macro
378376 this .trigger .onPreConstruct ( this , target , targetType );
379377 #end
380378
381- InjectionUtil .applyClassInjection ( target , this , classDescription , targetType );
382- if ( classDescription .pd .length > 0 && ! Reflect .isFunction (target ) )
379+ target .__ai ( this .getInstanceWithClassName , targetType );
380+
381+ try
383382 {
384- this ._managedObjects .put ( target , target );
383+ if ( target .__ap != null )
384+ {
385+ this ._managedObjects .put ( target , target );
386+ }
387+ }
388+ catch ( e : Dynamic )
389+ {
390+
385391 }
386392
387393 #if !macro
388394 this .trigger .onPostConstruct ( this , target , targetType );
389395 #end
390396 }
391397
398+ //
392399 inline function _getMappingID ( type : Class <Dynamic >, name : String = ' ' ) : String
393400 {
394401 #if neko
0 commit comments