-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
158 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// NSObject+Peregrine.h | ||
// Peregrine | ||
// | ||
// Created by Rake Yang on 2020/1/11. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
/// 示例的路由实现 | ||
@interface NSObject (Peregrine) | ||
|
||
/// 打开路由 | ||
/// @param URLString 路由地址 | ||
/// @param completion 回调 | ||
- (void)pg_openURL:(NSString *)URLString completion:(void (^)(BOOL ret, id object))completion; | ||
|
||
/// 打开路由 | ||
/// @param URLString 路由地址 | ||
/// @param object 参数对象 | ||
/// @param completion 回调 | ||
- (void)pg_openURL:(NSString *)URLString object:(id)object completion:(void (^)(BOOL ret, id object))completion; | ||
|
||
/// 校验路由是否生效 | ||
/// @param URLString 路由地址 | ||
- (BOOL)pg_dryRun:(NSString *)URLString; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// NSObject+Peregrine.m | ||
// Peregrine | ||
// | ||
// Created by Rake Yang on 2020/1/11. | ||
// | ||
|
||
#import "NSObject+Peregrine.h" | ||
#import "PGRouterManager.h" | ||
#import "PGRouterContext.h" | ||
|
||
@implementation NSObject (Peregrine) | ||
|
||
- (void)pg_openURL:(NSString *)URLString completion:(void (^)(BOOL, id))completion { | ||
[self pg_openURL:URLString object:nil completion:completion]; | ||
} | ||
|
||
- (void)pg_openURL:(NSString *)URLString object:(id)object completion:(void (^)(BOOL, id))completion { | ||
NSURL *patternURL = [NSURL pg_SafeURLWithString:URLString]; | ||
PGRouterNode *node = [PGRouterManager routerMap][patternURL.host]; | ||
NSMutableArray *componets = [patternURL.pathComponents mutableCopy]; | ||
if (componets.firstObject) { | ||
[componets removeObject:componets.firstObject]; | ||
} | ||
__block PGRouterNode *curNode = node; | ||
[componets enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { | ||
curNode = [curNode nodeForName:obj]; | ||
if (idx == componets.count - 1) { | ||
PGRouterConfig *config = curNode.config; | ||
if (config) { | ||
[self pg_openWithRouter:config context:[PGRouterContext contextWithURL:patternURL object:object callback:completion]]; | ||
} else { | ||
if (completion) { | ||
completion(NO, [NSString stringWithFormat:@"router: %@ no match.", URLString]); | ||
} | ||
} | ||
} | ||
}]; | ||
} | ||
|
||
- (void)pg_openWithRouter:(PGRouterConfig *)router context:(PGRouterContext *)context { | ||
if ([self respondsToSelector:router.selector]) { | ||
context.config = router; | ||
IMP imp = [router.targetClass instanceMethodForSelector:router.selector]; | ||
void (*targetMethod)(id, SEL, PGRouterContext *) = (void *)imp; | ||
targetMethod(self, router.selector, context); | ||
} | ||
} | ||
|
||
- (BOOL)pg_dryRun:(NSString *)URLString { | ||
NSURL *patternURL = [NSURL pg_SafeURLWithString:URLString]; | ||
PGRouterNode *node = [PGRouterManager routerMap][patternURL.host]; | ||
NSMutableArray *componets = [patternURL.pathComponents mutableCopy]; | ||
if (componets.firstObject) { | ||
[componets removeObject:componets.firstObject]; | ||
} | ||
__block BOOL valid = NO; | ||
__block PGRouterNode *context = node; | ||
[componets enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { | ||
context = [context nodeForName:obj]; | ||
if (idx == componets.count - 1) { | ||
PGRouterConfig *config = context.config; | ||
if (config) { | ||
valid = YES; | ||
} | ||
} | ||
}]; | ||
return valid; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters