Skip to content

Commit

Permalink
支持实例方法路由
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeyang committed Jan 11, 2020
1 parent a5305f4 commit c78dfcc
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- Expecta (1.0.6)
- Peregrine (0.6.3)
- Peregrine (0.6.4)
- Specta (1.0.7)

DEPENDENCIES:
Expand All @@ -19,7 +19,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Expecta: 3b6bd90a64b9a1dcb0b70aa0e10a7f8f631667d5
Peregrine: d339ebce32d3ec21f46b15a9c23161427b6dce12
Peregrine: e1b035ddde8115a44344dd3eb2bc96645cbd6b3b
Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66

PODFILE CHECKSUM: d76d71e8befa29d376a0d577864c2209ab26ac39
Expand Down
2 changes: 1 addition & 1 deletion Peregrine.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
Pod::Spec.new do |s|
s.name = 'Peregrine'
s.version = '0.6.3'
s.version = '0.6.4'
s.summary = 'A short description of Peregrine.'

# This description is used to generate tags and improve search results.
Expand Down
28 changes: 28 additions & 0 deletions Peregrine/NSObject+Peregrine.h
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
71 changes: 71 additions & 0 deletions Peregrine/NSObject+Peregrine.m
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
2 changes: 1 addition & 1 deletion Peregrine/PGGenerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def mapRouter(file_path)
file_content.scan(/@interface\s+(\w+)\s*[\s\S]+?\n([\s\S]+?)@end/) do |match|
class_name = match[0]
class_content = match[1]
class_content.scan(/PGMethod\((\b\w+\b),\s*\"([\s\S]+?)\"\);/) do |match1|
class_content.scan(/PG\w*Method\((\b\w+\b),\s*\"([\s\S]+?)\"\);/) do |match1|
@routers.push({ 'class' => class_name, 'selector' => match1[0] + ':', 'url' => match1[1] })
end
end
Expand Down
31 changes: 28 additions & 3 deletions Peregrine/PGRouter-Generate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,36 @@
// Copyright © 2019 BinaryParadise. All rights reserved.

/**
Generated automatic by Peregrine version 0.6.2
Generated automatic by Peregrine version 0.6.3
Don't modify manual ⚠️
*/

/// Build Time 2020-01-01 10:40:45 +0800
/// Build Time 2020-01-11 17:14:04 +0800

typedef NSString *PGRouterURLKey;

#pragma - mark xincheng

FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_address_list;
FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_darwinkit_open;
FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_item_list;
FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_itemdetail_home;
FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_itemdetail_showparam;
FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_itemdetail_showprotection;
FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_itemdetail_showsku;
FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_order_confirm;
FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_order_detail;
FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_order_list;
FOUNDATION_EXPORT PGRouterURLKey const vip8_xincheng_webview;

#pragma - mark shop

FOUNDATION_EXPORT PGRouterURLKey const vip8_shop_cart;

#pragma - mark address

FOUNDATION_EXPORT PGRouterURLKey const vip8_address_card;

#pragma - mark xincheng

FOUNDATION_EXPORT PGRouterURLKey const vip_xincheng_order_logistics;
33 changes: 20 additions & 13 deletions Peregrine/PGRouter-Generate.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@

#import "PGRouter-Generate.h"

#pragma - mark tlbb
#pragma - mark xincheng

PGRouterURLKey const ap_tlbb_most_like_wangyuyan = @"ap://tlbb/most/like/wangyuyan";
PGRouterURLKey const ap_tlbb_most_like_wangzuxian = @"ap://tlbb/most/like/wangzuxian";
PGRouterURLKey const ap_tlbb_wyy = @"ap://tlbb/wyy";
PGRouterURLKey const ap_tlbb_xlv = @"ap://tlbb/xlv";
PGRouterURLKey const ap_tlbb_xxlv = @"ap://tlbb/xxlv";
PGRouterURLKey const ap_tlbb_ym = @"ap://tlbb/ym";
PGRouterURLKey const vip8_xincheng_address_list = @"vip8://xincheng/address/list";
PGRouterURLKey const vip8_xincheng_darwinkit_open = @"vip8://xincheng/darwinkit/open";
PGRouterURLKey const vip8_xincheng_item_list = @"vip8://xincheng/item/list";
PGRouterURLKey const vip8_xincheng_itemdetail_home = @"vip8://xincheng/itemdetail/home";
PGRouterURLKey const vip8_xincheng_itemdetail_showparam = @"vip8://xincheng/itemdetail/showparam";
PGRouterURLKey const vip8_xincheng_itemdetail_showprotection = @"vip8://xincheng/itemdetail/showprotection";
PGRouterURLKey const vip8_xincheng_itemdetail_showsku = @"vip8://xincheng/itemdetail/showsku";
PGRouterURLKey const vip8_xincheng_order_confirm = @"vip8://xincheng/order/confirm";
PGRouterURLKey const vip8_xincheng_order_detail = @"vip8://xincheng/order/detail";
PGRouterURLKey const vip8_xincheng_order_list = @"vip8://xincheng/order/list";
PGRouterURLKey const vip8_xincheng_webview = @"vip8://xincheng/webview";

#pragma - mark webview
#pragma - mark shop

PGRouterURLKey const ap_webview_UIWebView = @"ap://webview/UIWebView";
PGRouterURLKey const ap_webview_WKWebView = @"ap://webview/WKWebView";
PGRouterURLKey const ap_webview_calloc = @"ap://webview/calloc";
PGRouterURLKey const vip8_shop_cart = @"vip8://shop/cart";

#pragma - mark :
#pragma - mark address

PGRouterURLKey const _invalidurl_haha = @"://invalidurl/haha";
PGRouterURLKey const vip8_address_card = @"vip8://address/card";

#pragma - mark xincheng

PGRouterURLKey const vip_xincheng_order_logistics = @"vip://xincheng/order/logistics";
5 changes: 5 additions & 0 deletions Peregrine/PGRouterManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
#import "PGRouterNode.h"
#import "PGRouter-Generate.h"

//类方法
#define PGMethod(_name, _url) \
+ (void)_name:(PGRouterContext *)context;

//实例方法
#define PGInstanceMethod(_name, _url) \
- (void)_name:(PGRouterContext *)context;

//使用__attribute__
#define PGMethodA(_name, _url) \
+ (void)_name:(PGRouterContext *)context __attribute__((pe_routed(_router)));
Expand Down
2 changes: 2 additions & 0 deletions Peregrine/Peregrine.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@

#import <Peregrine/PGRouterContext.h>
#import <Peregrine/PGRouterManager.h>
#import <Peregrine/NSObject+Peregrine.h>
#import <Peregrine/PGRouter-Generate.h>

0 comments on commit c78dfcc

Please sign in to comment.