@@ -13,4 +13,58 @@ public protocol Resolver: AnyObject {
13
13
14
14
func unsafeResolver( file: StaticString , function: StaticString , line: UInt ) -> SwinjectResolver
15
15
16
+ init ( _swinjectContainer: SwinjectContainer )
17
+
18
+ /// Resolvers require a manual implementation that matches the inheritance structure of the Resolver
19
+ /// If ResolverB inherits from ResolverA then the ResolverB inherits function should match this
20
+ /// Example:
21
+ /// public func ResolverB: ResolverA {
22
+ /// static func inherits(from resolverType: Resolver.Type) -> Bool {
23
+ /// return self == resolverType || resolverType == ResolverA.self
24
+ /// }
25
+ /// }
26
+ static func inherits( from resolverType: Resolver . Type ) -> Bool
27
+
28
+ }
29
+
30
+ /// Default Resolver implementation. Designed to be inherited from
31
+ open class BaseResolver : Resolver {
32
+
33
+ private weak var _swinjectContainer : SwinjectContainer ?
34
+
35
+ /// Returns `true` if the backing container is still available in memory, otherwise `false`.
36
+ public var isAvailable : Bool {
37
+ _swinjectContainer != nil
38
+ }
39
+
40
+ // MARK: - SwinjectResolver
41
+
42
+ public func unsafeResolver( file: StaticString , function: StaticString , line: UInt ) -> SwinjectResolver {
43
+ _unwrappedSwinjectContainer ( file: file, function: function, line: line)
44
+ }
45
+
46
+ public required init ( _swinjectContainer: SwinjectContainer ) {
47
+ self . _swinjectContainer = _swinjectContainer
48
+ }
49
+
50
+ /// Default implementation uses pure equality
51
+ open class func inherits( from resolverType: Resolver . Type ) -> Bool {
52
+ return self == resolverType
53
+ }
54
+
55
+ // Force unwraps the weak Container
56
+ func _unwrappedSwinjectContainer(
57
+ file: StaticString = #fileID,
58
+ function: StaticString = #function,
59
+ line: UInt = #line
60
+ ) -> SwinjectContainer {
61
+ guard let _swinjectContainer else {
62
+ fatalError (
63
+ " \( function) incorrectly accessed the container for \( self ) which has already been released " ,
64
+ file: file,
65
+ line: line
66
+ )
67
+ }
68
+ return _swinjectContainer
69
+ }
16
70
}
0 commit comments