Skip to content

Instantly share code, notes, and snippets.

@jlamoree
Last active June 24, 2017 20:50
Show Gist options
  • Save jlamoree/7daccb0744c21f3787cdd2fcd9db0086 to your computer and use it in GitHub Desktop.
Save jlamoree/7daccb0744c21f3787cdd2fcd9db0086 to your computer and use it in GitHub Desktop.
component implements="coldbox.system.ioc.dsl.IDSLBuilder" {
/**
* This custom DSL will return whatever exists at the path specified within the ColdBox settings.
* Consider the following chunk of config.Coldbox [sic]:
* ```
* variables.settings = {
* search = {
* classfied = {
* solr_host = "classified.sneakybastard.com:8993",
* solr_core = "gems"
* },
* unclassified = {
* solr_host = "unclassified.sneakybastard.com:8994",
* solr_core = "junk"
* }
* },
* auth = {
* tokens = {
* test = ["foo", "bar", "boo", "baz"],
* prod = ["secret", "monkey"]
* }
* }
* }
* ```
*
* To inject a specific search configuration structure, use this:
* ```
* property name="classifiedSearch" inject="config:search.classified";
* ```
*
* To inject a specific nested value:
* ```
* property name="prodTokens" inject="config:auth.tokens.prod";
* ```
*
* A possible future enhancement would be to support an injection like this:
* ```
* property name="prodToken" inject="config:auth.tokens.prod[1]";
* ```
*
* @injector Typically an instance of coldbox.system.ioc.Injector. Almost all the time.
*/
public any function init(required any injector) {
variables.injector = arguments.injector;
variables.coldbox = variables.injector.getColdBox();
variables.log = variables.injector.getLogBox().getLogger(this);
return this;
}
/**
* @definition If this isn't a struct, you'll have a bad time.
* @targetObject Not expected or used by this builder.
*/
public any function process(required any definition, any targetObject) {
var locator = arguments.definition.dsl.replace("config:", "");
var depth = locator.listLen(".");
var top = "";
var matches = [];
var match = "";
if (variables.keyExists("log") && variables.log.canDebug()) {
variables.log.debug("Received DSL definition '#arguments.definition.dsl#'");
}
if (locator.len() == 0 || depth == 0) {
throw(type="InvalidDSLException", message="The specified DSL ('#arguments.definition.dsl#') does not contain a locator.");
} else if (depth == 1) {
if (variables.coldbox.settingExists(locator)) {
return variables.coldbox.getSetting(locator);
} else {
throw(type="SettingNotFoundException", message="The requested ColdBox setting was not found for DSL '#arguments.definition.dsl#'.");
}
}
if (variables.coldbox.settingExists(listFirst(locator, "."))) {
top = variables.coldbox.getSetting(listFirst(locator, "."));
} else {
throw(type="SettingNotFoundException", message="The top level ColdBox setting was not found for DSL '#arguments.definition.dsl#'.");
}
matches = top.findKey(locator.listLast("."), "all");
if (matches.len() == 0) {
throw(type="SettingNotFoundException", message="A nested ColdBox setting was not found for DSL '#arguments.definition.dsl#'.");
}
locator = "." & locator.listRest(".");
for (match in matches) {
if (match.path == locator) {
return match.value;
}
}
throw(type="SettingNotFoundException", message="The requested ColdBox setting was not found for DSL '#arguments.definition.dsl#'.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment