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.app.component; 23 24 private import aermicioi.aedi; 25 private import eph.args; 26 27 private import core.runtime; 28 29 private import std.experimental.logger; 30 private import std.exception; 31 32 private import dxx.util.injector; 33 private import dxx.util.config; 34 private import dxx.app; 35 36 interface Components { 37 public Logger getLogger(); 38 39 public ArgParser getArgParser(); 40 41 } 42 43 mixin template registerComponent(T : RuntimeComponents!Param,Param...) { 44 shared static this() { 45 import std.conv; 46 import std.experimental.logger; 47 import dxx.util.injector; 48 49 debug(Component) { sharedLog.info("RuntimeComponents register "~typeid(T).to!string); } 50 auto t = new T; 51 } 52 } 53 54 55 mixin template registerComponents(Param...) { 56 mixin registerComponent!(RuntimeComponents!Param); 57 } 58 59 @component 60 class RuntimeComponents(Param...) : Components { 61 // static ArgParser argParser; 62 63 static __gshared InjectionContainer _injector; 64 static bool instantiated = false; 65 66 this(this T)() { 67 debug(Component) { 68 import std.conv; 69 sharedLog.info("RuntimeComponents "~typeid(T).to!string); 70 } 71 synchronized(InjectionContainer.classinfo) { 72 if(!instantiated) { 73 if(_injector is null) { 74 _injector = newInjector!(T,Param); 75 registerAppDependencies(_injector); 76 _injector.instantiate(); 77 } 78 instantiated = true; 79 } 80 } 81 } 82 83 void registerAppDependencies(InjectionContainer injector) { 84 // 85 } 86 87 public { 88 //@component 89 //override RuntimeModule getRuntimeComponents() { 90 // return INSTANCE; 91 //} 92 @component 93 override Logger getLogger() { 94 return sharedLog; 95 } 96 @component 97 override ArgParser getArgParser() { 98 // if(argParser is null) { 99 // argParser = new ArgParser; 100 // Parameter param = new Parameter(); 101 // argParser.register(param); 102 // //string args = AppConfig.get(DXXConfig.keys.commandLine); 103 // //args.split(" "); 104 // //Argument[] arguments = resolveInjector!(Argument[])(); 105 // //arguments.each!(a => argParser.register(a)); 106 // 107 //// auto args = Runtime.args; 108 //// argParser.parse(args); 109 // } 110 // return argParser; 111 return new ArgParser; 112 } 113 114 } 115 116 //@component 117 //public ExtensionPointManager getExtensionPointManager() { 118 // if(extensionPointManager is null) { 119 // extensionPointManager = new ExtensionPointManager; 120 // } 121 // return extensionPointManager; 122 //} 123 124 }