forked from BigShow1949/BigShow1949
-
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
1 parent
3ad8d17
commit fe79333
Showing
31 changed files
with
1,700 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file modified
BIN
+4.25 KB
(110%)
BigShow1949.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
25 changes: 25 additions & 0 deletions
25
...ow1949/Classes/10 - DesignPattern(设计模式)/Router/JLRouters/Classes/JLROptionalRouteParser.h
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,25 @@ | ||
/* | ||
Copyright (c) 2017, Joel Levin | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
Neither the name of JLRoutes nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
|
||
@interface JLROptionalRouteParser : NSObject | ||
|
||
+ (NSArray <NSString *> *)expandOptionalRoutePatternsForPattern:(NSString *)routePattern; | ||
|
||
@end | ||
|
||
|
||
NS_ASSUME_NONNULL_END |
147 changes: 147 additions & 0 deletions
147
...ow1949/Classes/10 - DesignPattern(设计模式)/Router/JLRouters/Classes/JLROptionalRouteParser.m
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,147 @@ | ||
/* | ||
Copyright (c) 2017, Joel Levin | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
Neither the name of JLRoutes nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#import "JLROptionalRouteParser.h" | ||
|
||
|
||
@interface NSArray (Combinations) | ||
|
||
- (NSArray<NSArray *> *)JLRoutes_allOrderedCombinations; | ||
|
||
@end | ||
|
||
|
||
@implementation JLROptionalRouteParser | ||
|
||
+ (NSArray <NSString *> *)expandOptionalRoutePatternsForPattern:(NSString *)routePattern | ||
{ | ||
/* this method exists to take a route pattern that is known to contain optional params, such as: | ||
/path/:thing/(/a)(/b)(/c) | ||
and create the following paths: | ||
/path/:thing/a/b/c | ||
/path/:thing/a/b | ||
/path/:thing/a/c | ||
/path/:thing/b/a | ||
/path/:thing/a | ||
/path/:thing/b | ||
/path/:thing/c | ||
*/ | ||
|
||
if ([routePattern rangeOfString:@"("].location == NSNotFound) { | ||
return @[]; | ||
} | ||
|
||
NSString *baseRoute = nil; | ||
NSArray *components = [self _optionalComponentsForPattern:routePattern baseRoute:&baseRoute]; | ||
NSArray *routes = [self _routesForOptionalComponents:components baseRoute:baseRoute]; | ||
|
||
return routes; | ||
} | ||
|
||
+ (NSArray <NSString *> *)_optionalComponentsForPattern:(NSString *)routePattern baseRoute:(NSString **)outBaseRoute; | ||
{ | ||
if (routePattern.length == 0) { | ||
return @[]; | ||
} | ||
|
||
NSMutableArray *optionalComponents = [NSMutableArray array]; | ||
|
||
NSScanner *scanner = [NSScanner scannerWithString:routePattern]; | ||
NSString *nonOptionalRouteSubpath = nil; | ||
|
||
BOOL parsedBaseRoute = NO; | ||
BOOL parseError = NO; | ||
|
||
// first, we need to parse the string and find the array of optional params. | ||
// aka, take (/a)(/b)(/c) and turn it into ["/a", "/b", "/c"] | ||
while ([scanner scanUpToString:@"(" intoString:&nonOptionalRouteSubpath]) { | ||
if ([scanner isAtEnd]) { | ||
break; | ||
} | ||
|
||
if (nonOptionalRouteSubpath.length > 0 && outBaseRoute != NULL && !parsedBaseRoute) { | ||
// the first 'non optional subpath' is always the base route | ||
*outBaseRoute = nonOptionalRouteSubpath; | ||
parsedBaseRoute = YES; | ||
} | ||
|
||
scanner.scanLocation = scanner.scanLocation + 1; | ||
|
||
NSString *component = nil; | ||
if (![scanner scanUpToString:@")" intoString:&component]) { | ||
parseError = YES; | ||
break; | ||
} | ||
|
||
[optionalComponents addObject:component]; | ||
} | ||
|
||
if (parseError) { | ||
NSLog(@"[JLRoutes]: Parse error, unsupported route: %@", routePattern); | ||
return @[]; | ||
} | ||
|
||
return [optionalComponents copy]; | ||
} | ||
|
||
+ (NSArray <NSString *> *)_routesForOptionalComponents:(NSArray <NSString *> *)optionalComponents baseRoute:(NSString *)baseRoute | ||
{ | ||
if (optionalComponents.count == 0 || baseRoute.length == 0) { | ||
return @[]; | ||
} | ||
|
||
NSMutableArray *routes = [NSMutableArray array]; | ||
|
||
// generate all possible combinations of the components that could exist (taking order into account) | ||
// aka, "/path/:thing/(/a)(/b)(/c)" should never generate a route for "/path/:thing/(/b)(/a)" | ||
NSArray *combinations = [optionalComponents JLRoutes_allOrderedCombinations]; | ||
|
||
// generate the actual final route path strings | ||
for (NSArray *components in combinations) { | ||
NSString *path = [components componentsJoinedByString:@""]; | ||
[routes addObject:[baseRoute stringByAppendingString:path]]; | ||
} | ||
|
||
// sort them so that the longest routes are first (since they are the most selective) | ||
[routes sortUsingSelector:@selector(length)]; | ||
|
||
return [routes copy]; | ||
} | ||
|
||
@end | ||
|
||
|
||
@implementation NSArray (JLRoutes) | ||
|
||
- (NSArray<NSArray *> *)JLRoutes_allOrderedCombinations | ||
{ | ||
NSInteger length = self.count; | ||
if (length == 0) { | ||
return [NSArray arrayWithObject:[NSArray array]]; | ||
} | ||
|
||
id lastObject = [self lastObject]; | ||
NSArray *subarray = [self subarrayWithRange:NSMakeRange(0, length - 1)]; | ||
NSArray *subarrayCombinations = [subarray JLRoutes_allOrderedCombinations]; | ||
NSMutableArray *combinations = [NSMutableArray arrayWithArray:subarrayCombinations]; | ||
|
||
for (NSArray *subarrayCombos in subarrayCombinations) { | ||
[combinations addObject:[subarrayCombos arrayByAddingObject:lastObject]]; | ||
} | ||
|
||
return [NSArray arrayWithArray:combinations]; | ||
} | ||
|
||
@end |
35 changes: 35 additions & 0 deletions
35
BigShow1949/Classes/10 - DesignPattern(设计模式)/Router/JLRouters/Classes/JLRRouteDefinition.h
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,35 @@ | ||
/* | ||
Copyright (c) 2017, Joel Levin | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
Neither the name of JLRoutes nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "JLRRouteRequest.h" | ||
#import "JLRRouteResponse.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
|
||
@interface JLRRouteDefinition : NSObject | ||
|
||
@property (nonatomic, strong, readonly) NSString *scheme; | ||
@property (nonatomic, strong, readonly) NSString *pattern; | ||
@property (nonatomic, assign, readonly) NSUInteger priority; | ||
|
||
- (instancetype)initWithScheme:(NSString *)scheme pattern:(NSString *)pattern priority:(NSUInteger)priority handlerBlock:(BOOL (^)(NSDictionary *parameters))handlerBlock; | ||
|
||
- (JLRRouteResponse *)routeResponseForRequest:(JLRRouteRequest *)request decodePlusSymbols:(BOOL)decodePlusSymbols; | ||
|
||
- (BOOL)callHandlerBlockWithParameters:(NSDictionary *)parameters; | ||
|
||
@end | ||
|
||
|
||
NS_ASSUME_NONNULL_END |
157 changes: 157 additions & 0 deletions
157
BigShow1949/Classes/10 - DesignPattern(设计模式)/Router/JLRouters/Classes/JLRRouteDefinition.m
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,157 @@ | ||
/* | ||
Copyright (c) 2017, Joel Levin | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
Neither the name of JLRoutes nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#import "JLRRouteDefinition.h" | ||
#import "JLRoutes.h" | ||
|
||
|
||
@interface JLRRouteDefinition () | ||
|
||
@property (nonatomic, strong) NSString *pattern; | ||
@property (nonatomic, strong) NSString *scheme; | ||
@property (nonatomic, assign) NSUInteger priority; | ||
@property (nonatomic, strong) BOOL (^handlerBlock)(NSDictionary *parameters); | ||
|
||
@property (nonatomic, strong) NSArray *patternComponents; | ||
|
||
@end | ||
|
||
|
||
@implementation JLRRouteDefinition | ||
|
||
- (instancetype)initWithScheme:(NSString *)scheme pattern:(NSString *)pattern priority:(NSUInteger)priority handlerBlock:(BOOL (^)(NSDictionary *parameters))handlerBlock | ||
{ | ||
if ((self = [super init])) { | ||
self.scheme = scheme; | ||
self.pattern = pattern; | ||
self.priority = priority; | ||
self.handlerBlock = handlerBlock; | ||
|
||
if ([pattern characterAtIndex:0] == '/') { | ||
pattern = [pattern substringFromIndex:1]; | ||
} | ||
|
||
self.patternComponents = [pattern componentsSeparatedByString:@"/"]; | ||
} | ||
return self; | ||
} | ||
|
||
- (NSString *)description | ||
{ | ||
return [NSString stringWithFormat:@"<%@ %p> - %@ (priority: %@)", NSStringFromClass([self class]), self, self.pattern, @(self.priority)]; | ||
} | ||
|
||
- (JLRRouteResponse *)routeResponseForRequest:(JLRRouteRequest *)request decodePlusSymbols:(BOOL)decodePlusSymbols | ||
{ | ||
BOOL patternContainsWildcard = [self.patternComponents containsObject:@"*"]; | ||
|
||
if (request.pathComponents.count != self.patternComponents.count && !patternContainsWildcard) { | ||
// definitely not a match, nothing left to do | ||
return [JLRRouteResponse invalidMatchResponse]; | ||
} | ||
|
||
JLRRouteResponse *response = [JLRRouteResponse invalidMatchResponse]; | ||
NSMutableDictionary *routeParams = [NSMutableDictionary dictionary]; | ||
BOOL isMatch = YES; | ||
NSUInteger index = 0; | ||
|
||
for (NSString *patternComponent in self.patternComponents) { | ||
NSString *URLComponent = nil; | ||
|
||
// figure out which URLComponent it is, taking wildcards into account | ||
if (index < [request.pathComponents count]) { | ||
URLComponent = request.pathComponents[index]; | ||
} else if ([patternComponent isEqualToString:@"*"]) { | ||
// match /foo by /foo/* | ||
URLComponent = [request.pathComponents lastObject]; | ||
} | ||
|
||
if ([patternComponent hasPrefix:@":"]) { | ||
// this is a variable, set it in the params | ||
NSString *variableName = [self variableNameForValue:patternComponent]; | ||
NSString *variableValue = [self variableValueForValue:URLComponent decodePlusSymbols:decodePlusSymbols]; | ||
routeParams[variableName] = variableValue; | ||
} else if ([patternComponent isEqualToString:@"*"]) { | ||
// match wildcards | ||
NSUInteger minRequiredParams = index; | ||
if (request.pathComponents.count >= minRequiredParams) { | ||
// match: /a/b/c/* has to be matched by at least /a/b/c | ||
routeParams[JLRouteWildcardComponentsKey] = [request.pathComponents subarrayWithRange:NSMakeRange(index, request.pathComponents.count - index)]; | ||
isMatch = YES; | ||
} else { | ||
// not a match: /a/b/c/* cannot be matched by URL /a/b/ | ||
isMatch = NO; | ||
} | ||
break; | ||
} else if (![patternComponent isEqualToString:URLComponent]) { | ||
// break if this is a static component and it isn't a match | ||
isMatch = NO; | ||
break; | ||
} | ||
index++; | ||
} | ||
|
||
if (isMatch) { | ||
// if it's a match, set up the param dictionary and create a valid match response | ||
NSMutableDictionary *params = [NSMutableDictionary dictionary]; | ||
[params addEntriesFromDictionary:[request queryParamsDecodingPlusSymbols:decodePlusSymbols]]; | ||
[params addEntriesFromDictionary:routeParams]; | ||
[params addEntriesFromDictionary:[self baseMatchParametersForRequest:request]]; | ||
response = [JLRRouteResponse validMatchResponseWithParameters:[params copy]]; | ||
} | ||
|
||
return response; | ||
} | ||
|
||
- (NSString *)variableNameForValue:(NSString *)value | ||
{ | ||
NSString *name = [value substringFromIndex:1]; | ||
|
||
if (name.length > 1 && [name characterAtIndex:0] == ':') { | ||
name = [name substringFromIndex:1]; | ||
} | ||
|
||
if (name.length > 1 && [name characterAtIndex:name.length - 1] == '#') { | ||
name = [name substringToIndex:name.length - 1]; | ||
} | ||
|
||
return name; | ||
} | ||
|
||
- (NSString *)variableValueForValue:(NSString *)value decodePlusSymbols:(BOOL)decodePlusSymbols | ||
{ | ||
NSString *var = [value stringByRemovingPercentEncoding]; | ||
|
||
if (var.length > 1 && [var characterAtIndex:var.length - 1] == '#') { | ||
var = [var substringToIndex:var.length - 1]; | ||
} | ||
|
||
var = [JLRRouteRequest variableValueFrom:var decodePlusSymbols:decodePlusSymbols]; | ||
|
||
return var; | ||
} | ||
|
||
- (NSDictionary *)baseMatchParametersForRequest:(JLRRouteRequest *)request | ||
{ | ||
return @{JLRoutePatternKey: self.pattern ?: [NSNull null], JLRouteURLKey: request.URL ?: [NSNull null], JLRouteSchemeKey: self.scheme ?: [NSNull null]}; | ||
} | ||
|
||
- (BOOL)callHandlerBlockWithParameters:(NSDictionary *)parameters | ||
{ | ||
if (self.handlerBlock == nil) { | ||
return YES; | ||
} | ||
|
||
return self.handlerBlock(parameters); | ||
} | ||
|
||
@end |
Oops, something went wrong.