Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infrastructure components #69

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Source/Component/TyphoonDefinition+Infrastructure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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 "TyphoonDefinition.h"

@class TyphoonPropertyPlaceholderConfigurer;
@protocol TyphoonResource;

/**
Declares short-hand definition factory methods for infrastructure components.
*/
@interface TyphoonDefinition (Infrastructure)

/**
Factory method for a TyphoonPropertyPlaceholderConfigurer.
@param resource The resource to load.
@return a definition.
*/
+ (TyphoonDefinition*)propertyPlaceholderWithResource:(id<TyphoonResource>)resource;

@end
32 changes: 32 additions & 0 deletions Source/Component/TyphoonDefinition+Infrastructure.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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 "TyphoonDefinition+Infrastructure.h"
#import "TyphoonPropertyPlaceholderConfigurer.h"
#import "TyphoonResource.h"
#import "TyphoonInitializer.h"

@implementation TyphoonDefinition (Infrastructure)

+ (TyphoonDefinition *)propertyPlaceholderWithResource:(id<TyphoonResource>)resource {

TyphoonDefinition *definition = [self withClass:[TyphoonPropertyPlaceholderConfigurer class] initialization:^(TyphoonInitializer *initializer) {

initializer.selector = @selector(configurerWithResource:);
[initializer injectWithObject:resource];

}];
definition.key = [NSString stringWithFormat:@"%@-%@", NSStringFromClass(definition.class), [resource description]];
return definition;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


#import <Foundation/Foundation.h>
#import "TyphoonComponentFactoryPostProcessor.h"
#import "TyphoonComponentFactoryMutator.h"

typedef id (^ObjectCreationBlock)();
Expand All @@ -23,7 +24,10 @@ typedef id (^ObjectCreationBlock)();
*
*
*/
@interface TyphoonPatcher : NSObject <TyphoonComponentFactoryMutator>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@interface TyphoonPatcher : NSObject <TyphoonComponentFactoryPostProcessor, TyphoonComponentFactoryMutator>
#pragma clang diagnostic pop
{
NSMutableDictionary* _patches;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#import "TyphoonInitializer.h"
#import "OCLogTemplate.h"
#import "TyphoonDefinition+InstanceBuilder.h"

#import "TyphoonComponentFactory.h"

@implementation TyphoonPatcher

Expand Down Expand Up @@ -49,7 +49,20 @@ - (void)patchDefinition:(TyphoonDefinition*)definition withObject:(ObjectCreatio
/* ====================================================================================================================================== */
#pragma mark - Protocol Methods

- (NSArray*)newDefinitionsToRegister
- (void)postProcessComponentFactory:(TyphoonComponentFactory *)factory
{
for (TyphoonDefinition *newDefinition in [self newDefinitionsToRegister])
{
[factory register:newDefinition];
}

for (TyphoonDefinition* definition in [factory registry])
{
[self patchDefinitionIfNeeded:definition];
}
}

- (NSArray *)newDefinitionsToRegister
{
NSMutableArray* newDefinitions = [[NSMutableArray alloc] init];
for (NSString* key in [_patches allKeys])
Expand All @@ -64,18 +77,23 @@ - (NSArray*)newDefinitionsToRegister
return [newDefinitions copy];
}

- (void)mutateComponentDefinitionsIfRequired:(NSArray*)componentDefinitions
- (void)mutateComponentDefinitionsIfRequired:(NSArray *)componentDefinitions
{
for (TyphoonDefinition* definition in componentDefinitions)
{
[self patchDefinitionIfNeeded:definition];
}
}

- (void)patchDefinitionIfNeeded:(TyphoonDefinition*)definition
{
for (TyphoonDefinition* definition in componentDefinitions)
id patchObject = [_patches objectForKey:definition.key];
if (patchObject)
{
id patchObject = [_patches objectForKey:definition.key];
if (patchObject)
{
LogDebug(@"Patching component with key %@ with object %@", definition.key, patchObject);
[definition setFactoryReference:[self patchFactoryNameForKey:definition.key]];
[definition setInitializer:[[TyphoonInitializer alloc] initWithSelector:@selector(object)]];
[definition setInjectedProperties:nil];
}
LogDebug(@"Patching component with key %@ with object %@", definition.key, patchObject);
[definition setFactoryReference:[self patchFactoryNameForKey:definition.key]];
[definition setInitializer:[[TyphoonInitializer alloc] initWithSelector:@selector(object)]];
[definition setInjectedProperties:nil];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@


#import <Foundation/Foundation.h>
#import "TyphoonComponentFactoryPostProcessor.h"
#import "TyphoonComponentFactoryMutator.h"

@protocol TyphoonResource;


@interface TyphoonPropertyPlaceholderConfigurer : NSObject <TyphoonComponentFactoryMutator>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@interface TyphoonPropertyPlaceholderConfigurer : NSObject <TyphoonComponentFactoryPostProcessor, TyphoonComponentFactoryMutator>
#pragma clang diagnostic pop
{
NSString* _prefix;
NSString* _suffix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#import "TyphoonPropertyInjectedWithStringRepresentation.h"
#import "TyphoonDefinition+InstanceBuilder.h"
#import "OCLogTemplate.h"

#import "TyphoonComponentFactory.h"

@implementation TyphoonPropertyPlaceholderConfigurer

Expand Down Expand Up @@ -102,14 +102,9 @@ - (NSDictionary*)properties
/* ====================================================================================================================================== */
#pragma mark - Protocol Methods

- (NSArray*)newDefinitionsToRegister
{
return nil;
}

- (void)mutateComponentDefinitionsIfRequired:(NSArray*)componentDefinitions
{
for (TyphoonDefinition* definition in componentDefinitions)
-(void)postProcessComponentFactory:(TyphoonComponentFactory *)factory {

for (TyphoonDefinition* definition in [factory registry])
{
for (id <TyphoonComponentInjectedByValue> component in [definition componentsInjectedByValue])
{
Expand All @@ -118,6 +113,22 @@ - (void)mutateComponentDefinitionsIfRequired:(NSArray*)componentDefinitions
}
}

- (NSArray *)newDefinitionsToRegister
{
return nil;
}

-(void)mutateComponentDefinitionsIfRequired:(NSArray *)componentDefinitions
{
for (TyphoonDefinition* definition in componentDefinitions)
{
for (id <TyphoonComponentInjectedByValue> component in [definition componentsInjectedByValue])
{
[self mutateComponentInjectedByValue:component];
}
}
}

- (void)mutateComponentInjectedByValue:(id <TyphoonComponentInjectedByValue>)component;
{
if ([component.textValue hasPrefix:_prefix] && [component.textValue hasSuffix:_suffix])
Expand Down
32 changes: 32 additions & 0 deletions Source/Factory/Config/TyphoonComponentFactoryPostProcessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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 TyphoonComponentFactory;

/**
Allows for custom modification of a component factory's definitions.
Component factories can auto-detect TyphoonComponentFactoryPostProcessor components in their definitions and apply them before any other components get created.
@see TyphoonPropertyPlaceholderConfigurer for an example implementation.
*/
@protocol TyphoonComponentFactoryPostProcessor <NSObject>

/**
Post process a component factory after its initialization.
@param factory The component factory
*/
- (void)postProcessComponentFactory:(TyphoonComponentFactory *)factory;

@end
6 changes: 5 additions & 1 deletion Source/Factory/Mutator/TyphoonComponentFactoryMutator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

#import <Foundation/Foundation.h>

@protocol TyphoonComponentFactoryMutator
/**
@deprecated Replaced by TyphoonComponentFactoryPostProcessor
*/
__attribute__((deprecated))
@protocol TyphoonComponentFactoryMutator <NSObject>

- (NSArray*)newDefinitionsToRegister;

Expand Down
22 changes: 19 additions & 3 deletions Source/Factory/TyphoonComponentFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


#import <Foundation/Foundation.h>
#import "TyphoonComponentFactoryPostProcessor.h"
#import "TyphoonComponentFactoryMutator.h"
@class TyphoonDefinition;

Expand All @@ -25,8 +26,9 @@
NSMutableDictionary* _singletons;

NSMutableDictionary* _currentlyResolvingReferences;
NSMutableArray* _postProcessors;
NSMutableArray* _mutators;
BOOL _isLoading;
BOOL _isLoading;
}

/**
Expand All @@ -40,7 +42,12 @@
@property (nonatomic, assign, getter = isLoaded) BOOL loaded;

/**
* Mutate the component definitions with the mutators and
* The attached factory post processors.
*/
@property (nonatomic, strong, readonly) NSArray *postProcessors;

/**
* Mutate the component definitions and
* build the not-lazy singletons.
*/
- (void)load;
Expand Down Expand Up @@ -88,7 +95,16 @@

- (NSArray*)registry;

- (void)attachMutator:(id<TyphoonComponentFactoryMutator>)mutator;
/**
@deprecated replaced by -attachPostProcessor:
*/
- (void)attachMutator:(id<TyphoonComponentFactoryMutator>)mutator __attribute__((deprecated));

/**
Attach a TyphoonComponentFactoryPostProcessor to this component factory.
@param postProcessor The post-processor to attach.
*/
- (void)attachPostProcessor:(id<TyphoonComponentFactoryPostProcessor>)postProcessor;

/**
* Injects the properties of an object
Expand Down
Loading