diff --git a/source/poodinis/valueinjector/mirage.d b/source/poodinis/valueinjector/mirage.d index 531ab56..9d1c3ab 100644 --- a/source/poodinis/valueinjector/mirage.d +++ b/source/poodinis/valueinjector/mirage.d @@ -10,6 +10,9 @@ module poodinis.valueinjector.mirage; import poodinis : ValueInjector, DependencyContainer, Value, Autowire, existingInstance; import mirage : ConfigDictionary, mirageLoadConfig = loadConfig; +import mirage.json : mirageLoadJsonConfig = loadJsonConfig; +import mirage.java : mirageLoadJavaConfig = loadJavaConfig; +import mirage.ini : mirageLoadIniConfig = loadIniConfig; class MirageValueInjector(Type) : ValueInjector!Type { @@ -76,9 +79,60 @@ public void registerMirageInjectors(shared(DependencyContainer) container) * Throws: ConfigCreationException when the file's extension is unrecognized. */ public void loadConfig(shared(DependencyContainer) container, const string configPath) +{ + loadConfigWithLoader(container, configPath, &mirageLoadConfig); +} + +/** + * Load a JSON config from disk. + * registerMirageInjectors will be called by this function. The loaded ConfigDictionary will be + * registered and available for injection by itself too. + * Params: + * container = Dependency container to register config and injectors with. + * configPath = Path to the configuration file. + */ +public void loadJsonConfig(shared(DependencyContainer) container, const string configPath) +{ + loadConfigWithLoader(container, configPath, &mirageLoadJsonConfig); +} + +/** + * Load a Java properties from disk. + * registerMirageInjectors will be called by this function. The loaded ConfigDictionary will be + * registered and available for injection by itself too. + * Params: + * container = Dependency container to register config and injectors with. + * configPath = Path to the configuration file. + */ +public void loadJavaProperties(shared(DependencyContainer) container, const string configPath) +{ + loadConfigWithLoader(container, configPath, &mirageLoadJavaConfig); +} + +/// ditto +alias loadJavaConfig = loadJavaProperties; + +/** + * Load an INI config from disk. + * registerMirageInjectors will be called by this function. The loaded ConfigDictionary will be + * registered and available for injection by itself too. + * Params: + * container = Dependency container to register config and injectors with. + * configPath = Path to the configuration file. + */ +public void loadIniConfig(shared(DependencyContainer) container, const string configPath) +{ + loadConfigWithLoader(container, configPath, &mirageLoadIniConfig); +} + +private void loadConfigWithLoader( + shared(DependencyContainer) container, + const string configPath, + ConfigDictionary function(const string configPath) loaderFunc +) { container.registerMirageInjectors; - auto config = mirageLoadConfig(configPath); + auto config = loaderFunc(configPath); container.register!ConfigDictionary.existingInstance(config); } @@ -122,4 +176,38 @@ version (unittest) assert(testClass.horseName == "Breeeeeezer"); assert(testClass.horseChildCount == 4); } + + @("Load JSON config") + unittest + { + auto dependencies = new shared DependencyContainer; + dependencies.register!TestClass; + dependencies.loadJsonConfig("testfiles/horses.json"); + + auto testClass = dependencies.resolve!TestClass; + assert(testClass.horseName == "Maaarrrilll"); + } + + @("Load Java config") + unittest + { + auto dependencies = new shared DependencyContainer; + dependencies.register!TestClass; + dependencies.loadJavaConfig("testfiles/horses.properties"); + + auto testClass = dependencies.resolve!TestClass; + assert(testClass.horseName == "Beaaaaaaan"); + } + + + @("Load INI config") + unittest + { + auto dependencies = new shared DependencyContainer; + dependencies.register!TestClass; + dependencies.loadIniConfig("testfiles/horses.ini"); + + auto testClass = dependencies.resolve!TestClass; + assert(testClass.horseName == "Breeeeeezer"); + } } diff --git a/testfiles/horses.json b/testfiles/horses.json new file mode 100644 index 0000000..3b896a2 --- /dev/null +++ b/testfiles/horses.json @@ -0,0 +1,6 @@ +{ + "horse": { + "name": "Maaarrrilll", + "children": 11 + } +} diff --git a/testfiles/horses.properties b/testfiles/horses.properties new file mode 100644 index 0000000..a70875f --- /dev/null +++ b/testfiles/horses.properties @@ -0,0 +1,2 @@ +horse.name = Beaaaaaaan +horse.children = 98 \ No newline at end of file