|
| 1 | +// |
| 2 | +// Dip |
| 3 | +// |
| 4 | +// Copyright (c) 2015 Olivier Halligon < [email protected]> |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +// |
| 24 | + |
| 25 | +//MARK: Public |
| 26 | + |
| 27 | +/** |
| 28 | +Use this wrapper to identifiy strong properties of the instance that should be injected when you call |
| 29 | +`resolveDependencies()` on this instance. Type T can be any type. |
| 30 | + |
| 31 | +- warning: |
| 32 | +Do not define this property as optional or container will not be able to inject it. |
| 33 | +Instead define it with initial value of `Injected<T>()`. |
| 34 | +If you need to nilify wrapped value, assing property to `Injected<T>()`. |
| 35 | + |
| 36 | +**Example**: |
| 37 | + |
| 38 | +```swift |
| 39 | +class ClientImp: Client { |
| 40 | + var service = Injected<Service>() |
| 41 | +} |
| 42 | + |
| 43 | +``` |
| 44 | +- seealso: `InjectedWeak`, `DependencyContainer.resolveDependencies(_:)` |
| 45 | + |
| 46 | +*/ |
| 47 | +public final class Injected<T>: _InjectedPropertyBox { |
| 48 | + |
| 49 | + static var tag: DependencyContainer.Tag { |
| 50 | + return .String("\(Injected<T>.self)") |
| 51 | + } |
| 52 | + |
| 53 | + var _value: Any? |
| 54 | + |
| 55 | + public var value: T? { |
| 56 | + get { |
| 57 | + return _value as? T |
| 58 | + } |
| 59 | + set { |
| 60 | + _value = newValue |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public init() {} |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + Use this wrapper to identifiy weak properties of the instance that should be injected when you call |
| 70 | + `resolveDependencies()` on this instance. Type T should be a **class** type. |
| 71 | + Otherwise it will cause runtime exception when container will try to resolve the property. |
| 72 | + Use this wrapper to define one of two circular dependencies to avoid retain cycle. |
| 73 | + |
| 74 | + - warning: |
| 75 | + Do not define this property as optional or container will not be able to inject it. |
| 76 | + Instead define it with initial value of `InjectedWeak<T>()`. |
| 77 | +If you need to nilify wrapped value, assing property to `InjectedWeak<T>()`. |
| 78 | + |
| 79 | + **Example**: |
| 80 | + |
| 81 | + ```swift |
| 82 | + class ServiceImp: Service { |
| 83 | + var client = InjectedWeak<Client>() |
| 84 | + } |
| 85 | + |
| 86 | + ``` |
| 87 | + |
| 88 | + - note: |
| 89 | + The only difference between `InjectedWeak` and `Injected` is that `InjectedWeak` uses _weak_ reference |
| 90 | + to store underlying value, when `Injected` uses _strong_ reference. |
| 91 | + For that reason if you resolve instance that holds weakly injected property |
| 92 | + this property will be released when `resolve` returns 'cause no one else holds reference to it. |
| 93 | + |
| 94 | + - seealso: `Injected`, `DependencyContainer.resolveDependencies(_:)` |
| 95 | + |
| 96 | + */ |
| 97 | +public final class InjectedWeak<T>: _InjectedWeakPropertyBox { |
| 98 | + |
| 99 | + //Only classes (means AnyObject) can be used as `weak` properties |
| 100 | + //but we can not make <T: AnyObject> cause that will prevent using protocol as generic type |
| 101 | + //so we just rely on user reading documentation and passing AnyObject in runtime |
| 102 | + //also we will throw fatal error if type can not be casted to AnyObject during resolution |
| 103 | + |
| 104 | + static var tag: DependencyContainer.Tag { |
| 105 | + return .String("\(InjectedWeak<T>.self)") |
| 106 | + } |
| 107 | + |
| 108 | + weak var _value: AnyObject? |
| 109 | + |
| 110 | + public var value: T? { |
| 111 | + get { |
| 112 | + return _value as? T |
| 113 | + } |
| 114 | + set { |
| 115 | + _value = newValue as? AnyObject |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public init() {} |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +extension DependencyContainer { |
| 124 | + |
| 125 | + /** |
| 126 | + Resolves dependencies of passed object. Properties that should be injected must be of type `Injected<T>` or `InjectedWeak<T>`. This method will also recursively resolve their dependencies, building full object graph. |
| 127 | + |
| 128 | + - parameter instance: object whose dependecies should be resolved |
| 129 | + |
| 130 | + - Note: |
| 131 | + Use `InjectedWeak<T>` to define one of two circular dependecies if another dependency is defined as `Injected<U>`. |
| 132 | + This will prevent retain cycle between resolved instances. |
| 133 | + |
| 134 | + **Example**: |
| 135 | + ```swift |
| 136 | + class ClientImp: Client { |
| 137 | + var service = Injected<Service>() |
| 138 | + } |
| 139 | + |
| 140 | + class ServiceImp: Service { |
| 141 | + var client = InjectedWeak<Client>() |
| 142 | + } |
| 143 | + |
| 144 | + //when resolved client will have service injected |
| 145 | + let client = container.resolve() as Client |
| 146 | + |
| 147 | + ``` |
| 148 | + |
| 149 | + */ |
| 150 | + public func resolveDependencies(instance: Any) { |
| 151 | + for child in Mirror(reflecting: instance).children { |
| 152 | + do { |
| 153 | + try (child.value as? _AutoInjectedPropertyBox)?.resolve(self) |
| 154 | + } catch { |
| 155 | + print(error) |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +} |
| 161 | + |
| 162 | +//MARK: - Private |
| 163 | + |
| 164 | +typealias InjectedFactory = ()->Any |
| 165 | +typealias InjectedWeakFactory = ()->AnyObject |
| 166 | + |
| 167 | +extension DependencyContainer { |
| 168 | + |
| 169 | + func registerInjected(definition: AutoInjectedDefinition) { |
| 170 | + guard let key = definition.injectedKey, |
| 171 | + definition = definition.injectedDefinition else { return } |
| 172 | + definitions[key] = definition |
| 173 | + } |
| 174 | + |
| 175 | + func registerInjectedWeak(definition: AutoInjectedDefinition) { |
| 176 | + guard let key = definition.injectedWeakKey, |
| 177 | + definition = definition.injectedWeakDefinition else { return } |
| 178 | + definitions[key] = definition |
| 179 | + } |
| 180 | + |
| 181 | + func removeInjected(definition: AutoInjectedDefinition) { |
| 182 | + guard definition.injectedDefinition != nil else { return } |
| 183 | + definitions[definition.injectedKey] = nil |
| 184 | + } |
| 185 | + |
| 186 | + func removeInjectedWeak(definition: AutoInjectedDefinition) { |
| 187 | + guard definition.injectedWeakDefinition != nil else { return } |
| 188 | + definitions[definition.injectedWeakKey] = nil |
| 189 | + } |
| 190 | + |
| 191 | +} |
| 192 | + |
| 193 | +protocol _AutoInjectedPropertyBox { |
| 194 | + func resolve(container: DependencyContainer) throws |
| 195 | + static var tag: DependencyContainer.Tag { get } |
| 196 | +} |
| 197 | + |
| 198 | +protocol _InjectedPropertyBox: class, _AutoInjectedPropertyBox { |
| 199 | + var _value: Any? { get set } |
| 200 | +} |
| 201 | + |
| 202 | +extension _InjectedPropertyBox { |
| 203 | + func resolve(container: DependencyContainer) throws { |
| 204 | + self._value = try container.resolve(tag: self.dynamicType.tag) as Any |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +protocol _InjectedWeakPropertyBox: class, _AutoInjectedPropertyBox { |
| 209 | + weak var _value: AnyObject? { get set } |
| 210 | +} |
| 211 | + |
| 212 | +extension _InjectedWeakPropertyBox { |
| 213 | + func resolve(container: DependencyContainer) throws { |
| 214 | + self._value = try container.resolve(tag: self.dynamicType.tag) as AnyObject |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +func isInjectedTag(tag: DependencyContainer.Tag?) -> String? { |
| 219 | + guard let tag = tag else { return nil } |
| 220 | + guard case let .String(stringTag) = tag else { return nil } |
| 221 | + |
| 222 | + return try! stringTag.match("^Injected(?:Weak){0,1}<\\((.+)\\)>$")?.first |
| 223 | +} |
| 224 | + |
| 225 | +extension String { |
| 226 | + private func match(pattern: String) throws -> [String]? { |
| 227 | + let expr = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions()) |
| 228 | + let result = expr.firstMatchInString(self, options: NSMatchingOptions(), range: NSMakeRange(0, characters.count)) |
| 229 | + if let result = result { |
| 230 | + let groups = (1..<result.numberOfRanges).map { |
| 231 | + (self as NSString).substringWithRange(result.rangeAtIndex($0)) |
| 232 | + } |
| 233 | + return groups |
| 234 | + } |
| 235 | + else { |
| 236 | + return nil |
| 237 | + } |
| 238 | + } |
| 239 | +} |
| 240 | + |
| 241 | + |
0 commit comments