[provider] add Enum support
This commit is contained in:
@@ -1,10 +1,24 @@
|
|||||||
package haxework.provider;
|
package haxework.provider;
|
||||||
|
|
||||||
|
abstract KeyType<T>(String) {
|
||||||
|
|
||||||
|
public function new(value:String) {
|
||||||
|
this = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:from public static inline function fromClass<T>(value:Class<T>):KeyType<T> {
|
||||||
|
return new KeyType<T>(Type.getClassName(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@:from public static inline function fromEnum<T>(value:Enum<T>):KeyType<T> {
|
||||||
|
return new KeyType<T>(Type.getEnumName(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Provider {
|
class Provider {
|
||||||
|
|
||||||
private static function key<T>(i:Class<T>, ?type:Dynamic):String {
|
private static function key<T>(i:KeyType<T>, ?type:Dynamic):String {
|
||||||
var result = Type.getClassName(i);
|
var result:String = Std.string(i);
|
||||||
if (type != null) result += ':${Std.string(type)}';
|
if (type != null) result += ':${Std.string(type)}';
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -13,18 +27,18 @@ class Provider {
|
|||||||
private static var args:Map<String, Array<Dynamic>> = new Map();
|
private static var args:Map<String, Array<Dynamic>> = new Map();
|
||||||
private static var instances:Map<String, Dynamic> = new Map();
|
private static var instances:Map<String, Dynamic> = new Map();
|
||||||
|
|
||||||
public static function setFactory<T>(i:Class<T>, clazz:Class<T>, ?type:Dynamic, ?args:Array<Dynamic>):Void {
|
public static function setFactory<T>(i:KeyType<T>, clazz:Class<T>, ?type:Dynamic, ?args:Array<Dynamic>):Void {
|
||||||
var key = key(i, type);
|
var key = key(i, type);
|
||||||
factories.set(key, clazz);
|
factories.set(key, clazz);
|
||||||
if (args != null) Provider.args.set(key, args);
|
if (args != null) Provider.args.set(key, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function set<T>(i:Class<T>, instance:T, ?type:Dynamic):Void {
|
public static function set<T>(i:KeyType<T>, instance:T, ?type:Dynamic):Void {
|
||||||
var key = key(i, type);
|
var key = key(i, type);
|
||||||
instances.set(key, instance);
|
instances.set(key, instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get<T>(i:Class<T>, ?type:Dynamic):T {
|
public static function get<T>(i:KeyType<T>, ?type:Dynamic):T {
|
||||||
var key = key(i, type);
|
var key = key(i, type);
|
||||||
if (instances.exists(key)) {
|
if (instances.exists(key)) {
|
||||||
return instances.get(key);
|
return instances.get(key);
|
||||||
|
|||||||
Reference in New Issue
Block a user