Skip to content

Commit

Permalink
New object graph scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper Blues committed Jan 26, 2014
1 parent a1007d2 commit 10efb5f
Show file tree
Hide file tree
Showing 26 changed files with 3,164 additions and 3,087 deletions.
2 changes: 1 addition & 1 deletion .idea/codeStyleSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .scripts/pod-update-checksum.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c39d78cda9a83c12ebe529161fa6d974c9b0f0a3
ff202de3006b041683d6dceebbb7c2e328069855
82 changes: 30 additions & 52 deletions A-Typhoon.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ - (id)initWithClass:(Class)clazz key:(NSString*)key factoryComponent:(NSString*)
{
_type = clazz;
_key = [key copy];
_scope = TyphoonScopeObjectGraph;
_factoryReference = [factoryComponent copy];
_injectedProperties = [[NSMutableSet alloc] init];
[self validateRequiredParametersAreSet];
Expand Down
9 changes: 8 additions & 1 deletion Source/Definition/TyphoonDefinition.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import "TyphoonPropertyInjectedAsObjectInstance.h"
#import "TyphoonPropertyInjectedByFactoryReference.h"
#import "TyphoonDefinition+Infrastructure.h"
#import "OCLogTemplate.h"


@implementation TyphoonDefinition
Expand Down Expand Up @@ -67,6 +68,12 @@ + (TyphoonDefinition*)withClass:(Class)clazz key:(NSString*)key initialization:(
__unsafe_unretained TyphoonDefinition* weakDefinition = definition;
properties(weakDefinition);
}
if (definition.lazy && definition.scope != TyphoonScopeSingleton)
{
[NSException raise:NSInvalidArgumentException format:
@"The lazy attribute is only applicable to singleton scoped definitions, but is set for definition: %@ ", definition];
}

return definition;
}

Expand Down Expand Up @@ -236,7 +243,7 @@ - (void)setFactory:(TyphoonDefinition*)factory
/* ====================================================================================================================================== */
#pragma mark - Overridden Methods

- (TyphoonInitializer* )initializer
- (TyphoonInitializer*)initializer
{
if (!_initializer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@


#import "TyphoonKeyedStackInstanceRegister.h"

#import "TyphoonGenericStack.h"
#import "TyphoonComponentSolvingStack.h"


@implementation TyphoonKeyedStackInstanceRegister
Expand Down Expand Up @@ -44,13 +43,13 @@ - (id)init

- (void)stashInstance:(id)instance forKey:(NSString*)key
{
TyphoonGenericStack* stack = [self stackForKey:key];
TyphoonComponentSolvingStack* stack = [self stackForKey:key];
[stack push:instance];
}

- (id)unstashInstanceForKey:(NSString*)key
{
TyphoonGenericStack* stack = _registry[key];
TyphoonComponentSolvingStack* stack = _registry[key];
id instance = [stack pop];

if ([stack isEmpty])
Expand All @@ -63,7 +62,7 @@ - (id)unstashInstanceForKey:(NSString*)key

- (id)peekInstanceForKey:(NSString*)key
{
TyphoonGenericStack* stack = _registry[key];
TyphoonComponentSolvingStack* stack = _registry[key];
return [stack peek];
}

Expand All @@ -75,13 +74,13 @@ - (BOOL)hasInstanceForKey:(NSString*)key

#pragma mark Private methods

- (TyphoonGenericStack*)stackForKey:(NSString*)key
- (TyphoonComponentSolvingStack*)stackForKey:(NSString*)key
{
TyphoonGenericStack* stack = _registry[key];
TyphoonComponentSolvingStack* stack = _registry[key];

if (!stack)
{
stack = [TyphoonGenericStack stack];
stack = [TyphoonComponentSolvingStack stack];
_registry[key] = stack;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

- (id)buildInstanceWithDefinition:(TyphoonDefinition*)definition;

- (id)buildSingletonWithDefinition:(TyphoonDefinition*)definition;
- (id)buildSharedInstanceForDefinition:(TyphoonDefinition*)definition;

- (void)injectPropertyDependenciesOn:(id <TyphoonIntrospectiveNSObject>)instance withDefinition:(TyphoonDefinition*)definition;

Expand Down
40 changes: 13 additions & 27 deletions Source/Factory/Internal/TyphoonComponentFactory+InstanceBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#import "TyphoonComponentFactoryAware.h"
#import "TyphoonParameterInjectedAsCollection.h"
#import "TyphoonComponentPostProcessor.h"
#import "TyphoonComponentSolvingStack.h"
#import "TyphoonStackItem.h"

@implementation TyphoonComponentFactory (InstanceBuilder)

Expand All @@ -53,11 +55,16 @@ - (id)buildInstanceWithDefinition:(TyphoonDefinition*)definition
{
[NSException raise:NSInvalidArgumentException format:@"Attempt to instantiate abstract definition: %@", definition];
}

__autoreleasing id <TyphoonIntrospectiveNSObject> instance;
instance = [self allocateInstance:instance withDefinition:definition];

[_currentlyResolvingReferences push:[TyphoonStackItem itemWithDefinition:definition instance:instance]];
instance = [self injectInstance:instance withDefinition:definition];
instance = [self postProcessInstance:instance];

[_currentlyResolvingReferences pop];

return instance;
}

Expand Down Expand Up @@ -88,14 +95,9 @@ - (id)allocateInstance:(id)instance withDefinition:(TyphoonDefinition*)definitio

- (id)injectInstance:(id)instance withDefinition:(TyphoonDefinition*)definition
{
[self markCurrentlyResolvingDefinition:definition withInstance:instance];

instance = [self initializerInjectionOn:instance withDefinition:definition];
[self propertyInjectionOn:instance withDefinition:definition];
[self injectAssemblyOnInstanceIfTyphoonAware:instance];

[self markDoneResolvingDefinition:definition];

return instance;
}

Expand All @@ -111,13 +113,6 @@ - (id)postProcessInstance:(id)instance
return instance;
}

- (void)markCurrentlyResolvingDefinition:(TyphoonDefinition*)definition withInstance:(__autoreleasing id)instance
{
NSString* key = definition.key;
[_currentlyResolvingReferences stashInstance:instance forKey:key];
LogTrace(@"Building instance with definition: '%@' as part of definitions pending resolution: '%@'.", definition, _currentlyResolvingReferences);
}

- (id)initializerInjectionOn:(id)instance withDefinition:(TyphoonDefinition*)definition
{
if (definition.initializer)
Expand Down Expand Up @@ -158,28 +153,19 @@ - (void)injectAssemblyOnInstance:(id <TyphoonComponentFactoryAware>)instance;
[instance setFactory:self];
}

- (void)markDoneResolvingDefinition:(TyphoonDefinition*)definition;
- (id)buildSharedInstanceForDefinition:(TyphoonDefinition*)definition
{
[_currentlyResolvingReferences unstashInstanceForKey:definition.key];
}

- (id)buildSingletonWithDefinition:(TyphoonDefinition*)definition
{
if ([self alreadyResolvingDefinition:definition])
if ([self alreadyResolvingKey:definition.key])
{
return [_currentlyResolvingReferences peekInstanceForKey:definition.key];
return [_currentlyResolvingReferences itemForKey:definition.key].instance;
}
return [self buildInstanceWithDefinition:definition];
}

- (BOOL)alreadyResolvingDefinition:(TyphoonDefinition*)definition
{
return [self alreadyResolvingKey:definition.key];
}

- (BOOL)alreadyResolvingKey:(NSString*)key
{
return [_currentlyResolvingReferences hasInstanceForKey:key];
return [_currentlyResolvingReferences itemForKey:key] != nil;
}

/* ====================================================================================================================================== */
Expand Down Expand Up @@ -223,7 +209,7 @@ - (void)doBeforePropertyInjectionOn:(id <TyphoonIntrospectiveNSObject>)instance
}
}

- (void)doPropertyInjection:(id <TyphoonIntrospectiveNSObject>)instance property:(TyphoonAbstractInjectedProperty* )property
- (void)doPropertyInjection:(id <TyphoonIntrospectiveNSObject>)instance property:(TyphoonAbstractInjectedProperty*)property
typeDescriptor:(TyphoonTypeDescriptor*)typeDescriptor
{
/* FIXME: change invocation to KVC for all injects */
Expand Down Expand Up @@ -347,7 +333,7 @@ - (void)injectCircularDependenciesOn:(__autoreleasing id <TyphoonIntrospectiveNS
[invocation setTarget:instance];
[invocation setSelector:pSelector];
NSString* componentKey = [circularDependentProperties objectForKey:propertyName];
id reference = [_currentlyResolvingReferences peekInstanceForKey:componentKey];
id reference = [_currentlyResolvingReferences itemForKey:componentKey].instance;
[invocation setArgument:&reference atIndex:2];
[invocation invoke];
}
Expand Down
30 changes: 30 additions & 0 deletions Source/Factory/Internal/TyphoonComponentSolvingStack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
////////////////////////////////////////////////////////////////////////////////
//
// TYPHOON FRAMEWORK
// Copyright 2013, Jasper Blues & Contributors
// All Rights Reserved.
//
// NOTICE: The authors permit you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

#import <Foundation/Foundation.h>

@class TyphoonStackItem;

@interface TyphoonComponentSolvingStack : NSObject

+ (instancetype)stack;

- (void)push:(TyphoonStackItem*)stackItem;

- (TyphoonStackItem*)pop;

- (TyphoonStackItem*)peek;

- (TyphoonStackItem*)itemForKey:(NSString*)key;

- (BOOL)isEmpty;

@end
96 changes: 96 additions & 0 deletions Source/Factory/Internal/TyphoonComponentSolvingStack.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
////////////////////////////////////////////////////////////////////////////////
//
// TYPHOON FRAMEWORK
// Copyright 2013, Jasper Blues & Contributors
// All Rights Reserved.
//
// NOTICE: The authors permit you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////


#import "TyphoonComponentSolvingStack.h"
#import "TyphoonStackItem.h"
#import "TyphoonDefinition.h"
#import "OCLogTemplate.h"

@implementation TyphoonComponentSolvingStack
{
NSMutableArray* _storage;
}


/* ====================================================================================================================================== */
#pragma mark - Class Methods

+ (instancetype)stack
{
return [[self alloc] init];
}

/* ====================================================================================================================================== */
#pragma mark - Initialization & Destruction

- (id)init
{
self = [super init];
if (self)
{
_storage = [NSMutableArray array];
}
return self;
}


/* ====================================================================================================================================== */
#pragma mark - Interface Methods

- (void)push:(TyphoonStackItem*)stackItem
{
if (![stackItem isKindOfClass:[TyphoonStackItem class]])
{
[NSException raise:NSInvalidArgumentException format:@"Not a TyphoonStackItem: %@", stackItem];
}
if ([self itemForKey:stackItem.definition.key])
{
LogDebug(@"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ already in!!!!!!!!!!");
return;
}
[_storage addObject:stackItem];
}

- (TyphoonStackItem*)pop
{
id element = [_storage lastObject];
if ([self isEmpty] == NO)
{
[_storage removeLastObject];
}
return element;
}

- (TyphoonStackItem*)peek
{
return [_storage lastObject];
}

- (TyphoonStackItem*)itemForKey:(NSString*)key
{
for (TyphoonStackItem* item in _storage)
{
if ([item.definition.key isEqualToString:key])
{
return item;
}
}
return nil;
}


- (BOOL)isEmpty
{
return ([_storage count] == 0);
}

@end
34 changes: 34 additions & 0 deletions Source/Factory/Internal/TyphoonStackItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
////////////////////////////////////////////////////////////////////////////////
//
// TYPHOON FRAMEWORK
// Copyright 2013, Jasper Blues & Contributors
// All Rights Reserved.
//
// NOTICE: The authors permit you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////


#import <Foundation/Foundation.h>

@class TyphoonDefinition;


@interface TyphoonStackItem : NSObject

@property(nonatomic, strong, readonly) TyphoonDefinition* definition;
@property(nonatomic, strong, readonly) id instance;


+ (instancetype)itemWithDefinition:(TyphoonDefinition*)definition instance:(id)instance;

- (BOOL)isEqual:(id)other;

- (BOOL)isEqualToItem:(TyphoonStackItem*)item;

- (NSUInteger)hash;



@end
Loading

0 comments on commit 10efb5f

Please sign in to comment.