1 /** 2 Copyright 2018 Mark Fisher 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy of 5 this software and associated documentation files (the "Software"), to deal in 6 the Software without restriction, including without limitation the rights to 7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 8 of the Software, and to permit persons to whom the Software is furnished to do 9 so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in all 12 copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 SOFTWARE. 21 **/ 22 module dxx.util.injector; 23 24 private import std.algorithm : each; 25 private import std.variant; 26 private import std.experimental.logger; 27 private import std.stdio; 28 private import std.process : environment; 29 30 private import aermicioi.aedi; 31 private import aermicioi.aedi_property_reader; 32 33 private import dxx.util.ini; 34 //private import dxx.util.storage; 35 36 static Variant[string] readInjectorProperties(File* f) { 37 Variant[string] res; 38 return res; 39 } 40 // 41 static void registerInjectorProperties(Variant[string] properties) { 42 //DefaultInjector._DEFAULT_CONTAINER.each!(c=>{ 43 // with(c.configure) { 44 // properties.each!((k,v)=>{ 45 // if(v.type == typeid(string)) { 46 // } 47 // }); 48 // } 49 //}); 50 //properties.keys.each!((k)=>{ 51 // if(v.type == typeid(string)) { 52 // DefaultInjector.register!string(k); 53 // } 54 // }); 55 } 56 57 static auto resolveInjector(alias T,Arg...)(Arg arg) { 58 return DefaultInjector._DEFAULT_CONTAINER.locate!T(arg); 59 } 60 61 static auto newInjector(alias T)(AggregateContainer c = DefaultInjector._DEFAULT_CONTAINER) { 62 return new ContextInjector!T(c); 63 } 64 65 static T getInjectorProperty(T)(DefaultInjector i,string k) { 66 return i.resolve!T(k); 67 } 68 69 static void setInjectorProperty(T)(DefaultInjector i,string k,T t) { 70 i.register!T(t,k); 71 } 72 73 abstract class DefaultInjector { 74 75 static __gshared AggregateContainer _DEFAULT_CONTAINER; 76 77 @property 78 AggregateContainer _container; 79 static auto config() { 80 auto cont = container( 81 singleton, 82 prototype, 83 argument, 84 env 85 //xml("./config.xml"), 86 //xml("~/.config/aedi-example/config.xml"), 87 //xml("/etc/aedi-example/config.xml"), 88 //json("./config.json"), 89 //json("~/.config/aedi-example/config.json"), 90 //json("/etc/aedi-example/config.json"), 91 //sdlang("./config.sdlang"), 92 //sdlang("~/.config/aedi-example/config.sdlang"), 93 //sdlang("/etc/aedi-example/config.sdlang") 94 //configFiles 95 ); 96 97 return cont; 98 } 99 shared static this() { 100 debug { 101 sharedLog.info("Creating shared container."); 102 } 103 //_DEFAULT_CONTAINER = prototype(); 104 //auto c = container( 105 // argument(), 106 // env() 107 // ); 108 auto c = aggregate(config, "parameters"); 109 //c.set(services(c), "services"); 110 _DEFAULT_CONTAINER = c; 111 scope(exit) _DEFAULT_CONTAINER.terminate(); 112 } 113 this(AggregateContainer c = _DEFAULT_CONTAINER) { 114 _container = c; 115 } 116 //void registerProperties(string[string] properties) { 117 //} 118 // auto resolve(alias T)() { 119 // return _container.locate!T; 120 // } 121 auto resolve(T,Arg ...)(Arg arg) { 122 return _container.locate!T(arg); 123 } 124 // void register(T...)() { 125 // _container.register!T; 126 // } 127 void register(T,Arg...)(Arg arg) { 128 _container.register!T(arg); 129 } 130 //auto configure() { 131 // return _container.configure; 132 //} 133 auto instantiate() { 134 return _container.instantiate; 135 } 136 } 137 138 139 final class ContextInjector(alias C ) : DefaultInjector { 140 this(AggregateContainer c = _DEFAULT_CONTAINER) { 141 super(c); 142 c.scan!C; 143 //foreach (subcontainer; c) { 144 // with (subcontainer.configure) { 145 // //register!ushort("http.port"); 146 // //register!(string[])("http.listen"); 147 // //register!string("http.host"); 148 // //register!bool("http.compression"); 149 // //register!string("log.access.file"); 150 // //register!string("log.access.format"); 151 // //register!bool("log.access.console"); 152 // //register!string("route.index"); 153 // //register!string("route.about"); 154 // //register!string("route.public"); 155 // } 156 //} 157 } 158 }