Skip to content
Browse files

Fix #6 by adding config.constants.

config.constants is a map of bean names and their values. This is injected into the beanInfo at startup (but will be overwritten by any actual beans that match that name). Updated the example to show usage.
  • Loading branch information...
1 parent 5757220 commit 7478b41e6aa45ec56b52eabfaabd55ecc9c1f1c0 @seancorfield committed Aug 14, 2011
Showing with 42 additions and 17 deletions.
  1. +1 −1 Application.cfc
  2. +4 −0 extrabeans/beans/item.cfc
  3. +37 −16 ioc.cfc
View
2 Application.cfc
@@ -1,6 +1,6 @@
component extends="org.corfield.framework" {
function setupApplication() {
- var xbf = new ioc( '/extrabeans' );
+ var xbf = new ioc( '/extrabeans', { constants = { magicValue = 42 } } );
var bf = new ioc( '/model' );
bf.setParent( xbf );
setBeanFactory( bf );
View
4 extrabeans/beans/item.cfc
@@ -1,4 +1,8 @@
component {
param name="application.itemCount" default="0";
this.itemNumber = ++application.itemCount;
+ function init( any magicValue ) {
+ this.magicValue = magicValue;
+ return this;
+ }
}
View
53 ioc.cfc
@@ -248,23 +248,38 @@ component {
var bean = 0;
if ( structKeyExists( variables.beanInfo, beanName ) ) {
var info = variables.beanInfo[ beanName ];
- // use createObject so we have control over initialization:
- bean = createObject( 'component', info.cfc );
- if ( structKeyExists( info.metadata, 'constructor' ) ) {
- var args = { };
- for ( var arg in info.metadata.constructor ) {
- var argBean = resolveBeanCreate( arg, accumulator );
- args[ arg ] = argBean.bean;
+ if ( structKeyExists( info, 'cfc' ) ) {
+ // use createObject so we have control over initialization:
+ bean = createObject( 'component', info.cfc );
+ if ( structKeyExists( info.metadata, 'constructor' ) ) {
+ var args = { };
+ for ( var arg in info.metadata.constructor ) {
+ var argBean = resolveBeanCreate( arg, accumulator );
+ // this throws a non-intuitive exception unless we step in...
+ if ( !structKeyExists( argBean, 'bean' ) ) {
+ throw 'bean not found: #arg#; while resolving constructor arguments for #beanName#';
+ }
+ args[ arg ] = argBean.bean;
+ }
+ var __ioc_newBean = evaluate( 'bean.init( argumentCollection = args )' );
+ // if the constructor returns anything, it becomes the bean
+ // this allows for smart constructors that return things other
+ // than the CFC being created, such as implicit factory beans
+ // and automatic singletons etc (rare practices in CFML but...)
+ if ( isDefined( '__ioc_newBean' ) ) bean = __ioc_newBean;
}
- evaluate( 'bean.init( argumentCollection = args )' );
- }
- var setterMeta = findSetters( bean, info.metadata );
- setterMeta.bean = bean;
- accumulator.injection[ beanName ] = setterMeta;
- for ( var property in setterMeta.setters ) {
- resolveBeanCreate( property, accumulator );
+ var setterMeta = findSetters( bean, info.metadata );
+ setterMeta.bean = bean;
+ accumulator.injection[ beanName ] = setterMeta;
+ for ( var property in setterMeta.setters ) {
+ resolveBeanCreate( property, accumulator );
+ }
+ accumulator.bean = bean;
+ } else if ( structKeyExists( info, 'value' ) ) {
+ accumulator.bean = info.value;
+ } else {
+ throw 'internal error: invalid metadata for #beanName#';
}
- accumulator.bean = bean;
} else if ( structKeyExists( variables, 'parent' ) && variables.parent.containsBean( beanName ) ) {
bean = variables.parent.getBean( beanName );
accumulator.injection[ beanName ] = { bean = bean, setters = { } };
@@ -287,7 +302,13 @@ component {
arrayAppend( variables.config.exclude, elem );
}
- variables.config.version = "0.0.4";
+ if ( structKeyExists( variables.config, 'constants' ) ) {
+ for ( var beanName in variables.config.constants ) {
+ variables.beanInfo[ beanName ] = { value = variables.config.constants[ beanName ] };
+ }
+ }
+
+ variables.config.version = '0.0.5';
}

0 comments on commit 7478b41

Please sign in to comment.
Something went wrong with that request. Please try again.