1
- /*
2
- InfomaniakDI
3
- Copyright (C) 2023 Infomaniak Network SA
4
-
5
- This program is free software: you can redistribute it and/or modify
6
- it under the terms of the GNU General Public License as published by
7
- the Free Software Foundation, either version 3 of the License, or
8
- (at your option) any later version.
9
-
10
- This program is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- GNU General Public License for more details.
14
-
15
- You should have received a copy of the GNU General Public License
16
- along with this program. If not, see <http://www.gnu.org/licenses/>.
17
- */
1
+ // Licensed under the Apache License, Version 2.0 (the "License");
2
+ // you may not use this file except in compliance with the License.
3
+ // You may obtain a copy of the License at
4
+ //
5
+ // http://www.apache.org/licenses/LICENSE-2.0
6
+ //
7
+ // Unless required by applicable law or agreed to in writing,
8
+ // software distributed under the License is distributed on an
9
+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10
+ // KIND, either express or implied. See the License for the
11
+ // specific language governing permissions and limitations
12
+ // under the License.
18
13
19
14
import Foundation
20
15
@@ -66,44 +61,44 @@ public final class SimpleResolver: SimpleResolvable, SimpleStorable, CustomDebug
66
61
}
67
62
return buffer
68
63
}
69
-
64
+
70
65
enum ErrorDomain : Error {
71
66
case factoryMissing( identifier: String )
72
67
case typeMissmatch( expected: String , got: String )
73
68
}
74
-
69
+
75
70
/// One singleton to rule them all
76
71
public static let sharedResolver = SimpleResolver ( )
77
-
72
+
78
73
/// Factory collection
79
74
var factories = [ String: Factoryable] ( )
80
-
75
+
81
76
/// Resolved object collection
82
77
var store = [ String: Any] ( )
83
78
84
79
/// A serial queue for thread safety
85
80
private let queue = DispatchQueue ( label: " com.infomaniakDI.resolver " )
86
-
81
+
87
82
// MARK: SimpleStorable
88
-
83
+
89
84
public func store( factory: Factoryable ,
90
85
forCustomTypeIdentifier customIdentifier: String ? = nil ) {
91
86
let type = factory. type
92
-
87
+
93
88
let identifier = buildIdentifier ( type: type, forIdentifier: customIdentifier)
94
89
queue. sync {
95
90
factories [ identifier] = factory
96
91
}
97
92
}
98
-
93
+
99
94
// MARK: SimpleResolvable
100
-
95
+
101
96
public func resolve< Service> ( type: Service . Type ,
102
97
forCustomTypeIdentifier customIdentifier: String ? ,
103
98
factoryParameters: [ String : Any ] ? = nil ,
104
99
resolver: SimpleResolvable ) throws -> Service {
105
100
let serviceIdentifier = buildIdentifier ( type: type, forIdentifier: customIdentifier)
106
-
101
+
107
102
// load form store
108
103
var fetchedService : Any ?
109
104
queue. sync {
@@ -112,7 +107,7 @@ public final class SimpleResolver: SimpleResolvable, SimpleStorable, CustomDebug
112
107
if let service = fetchedService as? Service {
113
108
return service
114
109
}
115
-
110
+
116
111
// load service from factory
117
112
var factory : Factoryable ?
118
113
queue. sync {
@@ -121,23 +116,23 @@ public final class SimpleResolver: SimpleResolvable, SimpleStorable, CustomDebug
121
116
guard let factory = factory else {
122
117
throw ErrorDomain . factoryMissing ( identifier: serviceIdentifier)
123
118
}
124
-
119
+
125
120
// Apply factory closure
126
121
let builtType = try factory. build ( factoryParameters: factoryParameters, resolver: resolver)
127
122
guard let service = builtType as? Service else {
128
123
throw ErrorDomain . typeMissmatch ( expected: " \( Service . Type. self) " , got: " \( builtType. self) " )
129
124
}
130
-
125
+
131
126
// keep in store built object for later
132
127
queue. sync {
133
128
store [ serviceIdentifier] = service
134
129
}
135
-
130
+
136
131
return service
137
132
}
138
-
133
+
139
134
// MARK: internal
140
-
135
+
141
136
func buildIdentifier( type: Any . Type ,
142
137
forIdentifier identifier: String ? = nil ) -> String {
143
138
if let identifier {
@@ -146,9 +141,9 @@ public final class SimpleResolver: SimpleResolvable, SimpleStorable, CustomDebug
146
141
return " \( type) "
147
142
}
148
143
}
149
-
144
+
150
145
// MARK: testing
151
-
146
+
152
147
func removeAll( ) {
153
148
queue. sync {
154
149
factories. removeAll ( )
0 commit comments