Skip to content

Commit ab9bdda

Browse files
committed
Fixes framework-one#52 by adding overrides.
1 parent f687d35 commit ab9bdda

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

ioc.cfc

+30-14
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ component {
5757

5858

5959
// programmatically register new beans with the factory (add an actual CFC)
60-
public any function declareBean( string beanName, string dottedPath, boolean isSingleton = true ) {
60+
public any function declareBean( string beanName, string dottedPath, boolean isSingleton = true, struct overrides = { } ) {
6161
discoverBeans( variables.folders );
6262
var singleDir = '';
6363
if ( listLen( dottedPath, '.' ) > 1 ) {
@@ -68,7 +68,8 @@ component {
6868
var cfcPath = replace( expandPath( '/' & replace( dottedPath, '.', '/', 'all' ) & '.cfc' ), chr(92), '/', 'all' );
6969
var metadata = {
7070
name = beanName, qualifier = singleDir, isSingleton = isSingleton,
71-
path = cfcPath, cfc = dottedPath, metadata = cleanMetadata( dottedPath )
71+
path = cfcPath, cfc = dottedPath, metadata = cleanMetadata( dottedPath ),
72+
overrides = overrides
7273
};
7374
variables.beanInfo[ beanName ] = metadata;
7475
return this;
@@ -485,7 +486,9 @@ component {
485486
var injection = partialBean.injection[ name ];
486487
for ( var property in injection.setters ) {
487488
var args = { };
488-
if ( structKeyExists( partialBean.injection, property ) ) {
489+
if ( structKeyExists( injection.overrides, property ) ) {
490+
args[ property ] = injection.overrides[ property ];
491+
} else if ( structKeyExists( partialBean.injection, property ) ) {
489492
args[ property ] = partialBean.injection[ property ].bean;
490493
} else if ( structKeyExists( variables, 'parent' ) && variables.parent.containsBean( property ) ) {
491494
args[ property ] = variables.parent.getBean( property );
@@ -506,6 +509,7 @@ component {
506509
var info = variables.beanInfo[ beanName ];
507510
if ( structKeyExists( info, 'cfc' ) ) {
508511
var metaBean = cachable( beanName );
512+
var overrides = structKeyExists( info, 'overrides' ) ? info.overrides : { };
509513
bean = metaBean.bean;
510514
if ( metaBean.newObject ) {
511515
if ( structKeyExists( info.metadata, 'constructor' ) ) {
@@ -515,7 +519,10 @@ component {
515519
// handle known required arguments
516520
if ( info.metadata.constructor[ arg ] ) {
517521
var beanMissing = true;
518-
if ( containsBean( arg ) ) {
522+
if ( structKeyExists( overrides, arg ) ) {
523+
args[ arg ] = overrides[ arg ];
524+
beanMissing = false;
525+
} else if ( containsBean( arg ) ) {
519526
argBean = resolveBeanCreate( arg, accumulator );
520527
if ( structKeyExists( argBean, 'bean' ) ) {
521528
args[ arg ] = argBean.bean;
@@ -525,14 +532,18 @@ component {
525532
if ( beanMissing ) {
526533
throw 'bean not found: #arg#; while resolving constructor arguments for #beanName#';
527534
}
528-
} else if ( containsBean( arg ) ) {
529-
// optional but present
530-
argBean = resolveBeanCreate( arg, accumulator );
531-
if ( structKeyExists( argBean, 'bean' ) ) {
532-
args[ arg ] = argBean.bean;
533-
}
534535
} else {
535-
// optional but not present
536+
if ( structKeyExists( overrides, arg ) ) {
537+
args[ arg ] = overrides[ arg ];
538+
} else if ( containsBean( arg ) ) {
539+
// optional but present
540+
argBean = resolveBeanCreate( arg, accumulator );
541+
if ( structKeyExists( argBean, 'bean' ) ) {
542+
args[ arg ] = argBean.bean;
543+
}
544+
} else {
545+
// optional but not present
546+
}
536547
}
537548
}
538549
var __ioc_newBean = evaluate( 'bean.init( argumentCollection = args )' );
@@ -552,11 +563,16 @@ component {
552563
}
553564
var setterMeta = {
554565
setters = variables.settersInfo[ beanName ].setters,
555-
bean = bean
566+
bean = bean,
567+
overrides = overrides
556568
};
557569
accumulator.injection[ beanName ] = setterMeta;
558570
for ( var property in setterMeta.setters ) {
559-
resolveBeanCreate( property, accumulator );
571+
if ( structKeyExists( overrides, property ) ) {
572+
// skip resolution because we'll inject override
573+
} else {
574+
resolveBeanCreate( property, accumulator );
575+
}
560576
}
561577
}
562578
accumulator.bean = bean;
@@ -608,7 +624,7 @@ component {
608624
throw 'singletonPattern and transientPattern are mutually exclusive';
609625
}
610626

611-
variables.config.version = '0.4.10';
627+
variables.config.version = '0.5.0';
612628
}
613629

614630

tests/DeclareBean.cfc

+37-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ component extends="mxunit.framework.TestCase" {
22

33
function shouldDeclareSingleton() {
44
var bf = new ioc( "" ).declareBean( "foo", "tests.extrabeans.sheep.item" );
5-
application.itemCount = 0;
5+
structDelete( application, "itemCount" );
66
var item1 = bf.getBean( "foo" );
77
assertEquals( 1, application.itemCount );
88
var item2 = bf.getBean( "foo" );
@@ -12,12 +12,47 @@ component extends="mxunit.framework.TestCase" {
1212

1313
function shouldDeclareTransient() {
1414
var bf = new ioc( "" ).declareBean( "foo", "tests.extrabeans.sheep.item", false );
15-
application.itemCount = 0;
15+
structDelete( application, "itemCount" );
1616
var item1 = bf.getBean( "foo" );
1717
assertEquals( 1, application.itemCount );
1818
var item2 = bf.getBean( "foo" );
1919
assertEquals( 2, application.itemCount );
2020
assertNotSame( item1, item2 );
2121
}
2222

23+
function shouldDeclareSingletonWithOverride() {
24+
var bf = new ioc( "" ).declareBean( "foo", "tests.extrabeans.sheep.item", true, { start = 100 } );
25+
structDelete( application, "itemCount" );
26+
var item1 = bf.getBean( "foo" );
27+
assertEquals( 101, application.itemCount );
28+
var item2 = bf.getBean( "foo" );
29+
assertEquals( 101, application.itemCount );
30+
assertSame( item1, item2 );
31+
}
32+
33+
function shouldDeclareTransientWithOverride() {
34+
var bf = new ioc( "" ).declareBean( "foo", "tests.extrabeans.sheep.item", false, { start = 100 } );
35+
structDelete( application, "itemCount" );
36+
var item1 = bf.getBean( "foo" );
37+
assertEquals( 101, application.itemCount );
38+
var item2 = bf.getBean( "foo" );
39+
assertEquals( 102, application.itemCount );
40+
assertNotSame( item1, item2 );
41+
}
42+
43+
function shouldDeclareAndAdd() {
44+
var bf = new ioc( "" ).declareBean( "foo", "tests.declared.things.myconfig" ).addBean( "name", "test" ).addBean( "config", "some" );
45+
var item = bf.getBean( "foo" );
46+
assertEquals( "test", item.getName() );
47+
assertEquals( "some", item.getConfig() );
48+
}
49+
50+
function shouldDeclareWithOverride() {
51+
var bf = new ioc( "" ).declareBean( "foo", "tests.declared.things.myconfig", true, { name = "test", config = "some" } )
52+
.addBean( "name", "not-test" ).addBean( "config", "config" );
53+
var item = bf.getBean( "foo" );
54+
assertEquals( "test", item.getName() );
55+
assertEquals( "some", item.getConfig() );
56+
}
57+
2358
}

tests/extrabeans/sheep/item.cfc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
component accessors="true" {
22
property beanfactory;
33

4-
function init() {
5-
param name="application.itemCount" default="0";
4+
function init( numeric start = 0 ) {
5+
param name="application.itemCount" default="#start#";
66
this.itemNumber = ++application.itemCount;
77
}
88

0 commit comments

Comments
 (0)