The DSL Builder Interface

IDSLBuilder

The scope interface can be found here: coldbox.system.ioc.dsl.IDSLBuilder.
Please note that you DO NOT need to add the implements to your code. We actually highly suggest you don't. There are many issues with interfaces yet in multiple CFML engines. So we do runtime checks for it, instead at compile time.
1
/**
2
* Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
3
* www.ortussolutions.com
4
* ---
5
* The main interface to produce WireBox namespace DSL Builders
6
**/
7
interface {
8
9
/**
10
* Configure the DSL Builder for operation and returns itself
11
*
12
* @injector The linked WireBox Injector
13
* @injector.doc_generic coldbox.system.ioc.Injector
14
*
15
* @return coldbox.system.ioc.dsl.IDSLBuilder
16
*/
17
function init( required injector );
18
19
/**
20
* Process an incoming DSL definition and produce an object with it
21
*
22
* @definition The injection dsl definition structure to process. Keys: name, dsl
23
* @targetObject The target object we are building the DSL dependency for. If empty, means we are just requesting building
24
* @targetID The target ID we are building this dependency for
25
*
26
* @return coldbox.system.ioc.dsl.IDSLBuilder
27
*/
28
function process( required definition, targetObject, targetID );
29
30
}
31
Copied!

Your DSL Builder

Here is a sample DSL builder:
1
/**
2
* Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
3
* www.ortussolutions.com
4
* ---
5
* Process DSL functions via LogBox
6
**/
7
component accessors="true" {
8
9
/**
10
* Injector Reference
11
*/
12
property name="injector";
13
14
/**
15
* LogBox Reference
16
*/
17
property name="logBox";
18
19
/**
20
* Log Reference
21
*/
22
property name="log";
23
24
/**
25
* Configure the DSL Builder for operation and returns itself
26
*
27
* @injector The linked WireBox Injector
28
* @injector.doc_generic coldbox.system.ioc.Injector
29
*
30
* @return coldbox.system.ioc.dsl.IDSLBuilder
31
*/
32
function init( required injector ){
33
variables.injector = arguments.injector;
34
variables.logBox = variables.injector.getLogBox();
35
variables.log = variables.injector.getLogBox().getLogger( this );
36
37
return this;
38
}
39
40
/**
41
* Process an incoming DSL definition and produce an object with it
42
*
43
* @definition The injection dsl definition structure to process. Keys: name, dsl
44
* @targetObject The target object we are building the DSL dependency for. If empty, means we are just requesting building
45
* @targetID The target ID we are building this dependency for
46
*
47
* @return coldbox.system.ioc.dsl.IDSLBuilder
48
*/
49
function process( required definition, targetObject, targetID ){
50
var thisType = arguments.definition.dsl;
51
var thisTypeLen = listLen( thisType, ":" );
52
53
// DSL stages
54
switch ( thisTypeLen ) {
55
// logbox
56
case 1: {
57
return variables.logBox;
58
}
59
60
// logbox:root and logbox:logger
61
case 2: {
62
var thisLocationKey = getToken( thisType, 2, ":" );
63
switch ( thisLocationKey ) {
64
case "root": {
65
return variables.logbox.getRootLogger();
66
}
67
case "logger": {
68
return variables.logbox.getLogger( arguments.definition.name );
69
}
70
}
71
break;
72
}
73
74
// Named Loggers
75
case 3: {
76
var thisLocationType = getToken( thisType, 2, ":" );
77
var thisLocationKey = getToken( thisType, 3, ":" );
78
// DSL Level 2 Stage Types
79
switch ( thisLocationType ) {
80
// Get a named Logger
81
case "logger": {
82
// Check for {this} and targetobject exists
83
if ( thisLocationKey eq "{this}" AND structKeyExists( arguments, "targetObject" ) ) {
84
return variables.logBox.getLogger( arguments.targetObject );
85
}
86
// Normal Logger injection
87
return variables.logBox.getLogger( thisLocationKey );
88
break;
89
}
90
}
91
break;
92
}
93
// end level 3 main DSL
94
}
95
}
96
97
}
98
Copied!
Here is another one that you can find in the ColdBox ORM module: https://github.com/coldbox-modules/cborm/tree/development/dsl
1
/**
2
* Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
3
* www.ortussolutions.com
4
* ---
5
* The ORM WireBox DSL
6
*/
7
component accessors="true" {
8
9
property name="injector";
10
property name="log";
11
12
13
/**
14
* Constructor as per interface
15
*/
16
public any function init( required any injector ){
17
variables.injector = arguments.injector;
18
variables.log = arguments.injector.getLogBox().getLogger( this );
19
20
return this;
21
}
22
23
/**
24
* Process an incoming DSL definition and produce an object with it
25
*
26
* @definition The injection dsl definition structure to process. Keys: name, dsl
27
* @targetObject The target object we are building the DSL dependency for. If empty, means we are just requesting building
28
* @targetID The target ID we are building this dependency for
29
*
30
* @return coldbox.system.ioc.dsl.IDSLBuilder
31
*/
32
function process( required definition, targetObject, targetID ){
33
var DSLNamespace = listFirst( arguments.definition.dsl, ":" );
34
35
switch ( DSLNamespace ) {
36
case "entityService": {
37
return getEntityServiceDSL( argumentCollection = arguments );
38
}
39
}
40
}
41
42
/**
43
* Get an EntityService Dependency
44
*/
45
function getEntityServiceDSL( required definition, targetObject ){
46
var entityName = getToken( arguments.definition.dsl, 2, ":" );
47
48
// Do we have an entity name? If we do create virtual entity service
49
if ( len( entityName ) ) {
50
return new cborm.models.VirtualEntityService( entityName );
51
}
52
53
// else return Base ORM Service
54
return new cborm.models.BaseORMService();
55
}
56
57
}
58
Copied!

Registration

In your configuration binder you can then register the DSL component you created
1
customDSL = {
2
ortus = "path.model.dsl.OrtusBuilder"
3
};
4
5
or
6
mapDSL("ortus","path.model.dsl.OrtusBuilder");
Copied!
This will register a new injection DSL namespace called ortus that maps to that instantiation component path.model.dsl.OrtusBuilder.
As you can see from the sample, creating your own DSL builder is fairly easy. The benefits of a custom DSL builder is that you can very easily create and extend the injection DSL language to your own benefit and if you are funky enough, override the behavior of the internal DSL Namespaces.
Copy link
Edit on GitHub