|
| 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>: _Injected { |
| 48 | + |
| 49 | + var _value: Any? |
| 50 | + |
| 51 | + public init(_ value: T? = nil) { |
| 52 | + self._value = value |
| 53 | + } |
| 54 | + |
| 55 | + public var value: T? { |
| 56 | + get { |
| 57 | + return _value as? T |
| 58 | + } |
| 59 | + set { |
| 60 | + _value = newValue |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + Use this wrapper to identifiy weak properties of the instance that should be injected when you call |
| 68 | + `resolveDependencies()` on this instance. Type T should be a **class** type. |
| 69 | + Otherwise it will cause runtime exception when container will try to resolve the property. |
| 70 | + Use this wrapper to define one of two circular dependencies to avoid retain cycle. |
| 71 | + |
| 72 | + - warning: |
| 73 | + Do not define this property as optional or container will not be able to inject it. |
| 74 | + Instead define it with initial value of `InjectedWeak<T>()`. |
| 75 | +If you need to nilify wrapped value, assing property to `InjectedWeak<T>()`. |
| 76 | + |
| 77 | + **Example**: |
| 78 | + |
| 79 | + ```swift |
| 80 | + class ServiceImp: Service { |
| 81 | + var client = InjectedWeak<Client>() |
| 82 | + } |
| 83 | + |
| 84 | + ``` |
| 85 | + |
| 86 | + - note: |
| 87 | + The only difference between `InjectedWeak` and `Injected` is that `InjectedWeak` uses _weak_ reference |
| 88 | + to store underlying value, when `Injected` uses _strong_ reference. |
| 89 | + For that reason if you resolve instance that holds weakly injected property |
| 90 | + this property will be released when `resolve` returns 'cause no one else holds reference to it. |
| 91 | + |
| 92 | + - seealso: `Injected`, `DependencyContainer.resolveDependencies(_:)` |
| 93 | + |
| 94 | + */ |
| 95 | +public final class InjectedWeak<T>: _InjectedWeak { |
| 96 | + |
| 97 | + //Only classes (means AnyObject) can be used as `weak` properties |
| 98 | + //but we can not make <T: AnyObject> cause that will prevent using protocol as generic type |
| 99 | + //so we just rely on user reading documentation and passing AnyObject in runtime |
| 100 | + //also we will throw fatal error if type can not be casted to AnyObject during resolution |
| 101 | + |
| 102 | + weak var _value: AnyObject? |
| 103 | + |
| 104 | + public init(_ value: T? = nil) { |
| 105 | + self._value = value as? AnyObject |
| 106 | + } |
| 107 | + |
| 108 | + public var value: T? { |
| 109 | + get { |
| 110 | + return _value as? T |
| 111 | + } |
| 112 | + set { |
| 113 | + _value = newValue as? AnyObject |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +extension DependencyContainer { |
| 120 | + |
| 121 | + /** |
| 122 | + 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. |
| 123 | + |
| 124 | + - parameter instance: object whose dependecies should be resolved |
| 125 | + |
| 126 | + - Note: |
| 127 | + Use `InjectedWeak<T>` to define one of two circular dependecies if another dependency is defined as `Injected<U>`. |
| 128 | + This will prevent retain cycle between resolved instances. |
| 129 | + |
| 130 | + **Example**: |
| 131 | + ```swift |
| 132 | + class ClientImp: Client { |
| 133 | + var service = Injected<Service>() |
| 134 | + } |
| 135 | + |
| 136 | + class ServiceImp: Service { |
| 137 | + var client = InjectedWeak<Client>() |
| 138 | + } |
| 139 | + |
| 140 | + //when resolved client will have service injected |
| 141 | + let client = container.resolve() as Client |
| 142 | + |
| 143 | + ``` |
| 144 | + |
| 145 | + */ |
| 146 | + public func resolveDependencies(instance: Any) { |
| 147 | + for child in Mirror(reflecting: instance).children |
| 148 | + where child.value is _Injected || child.value is _InjectedWeak { |
| 149 | + |
| 150 | + let tag = Tag.String("\(child.value.dynamicType)") |
| 151 | + if let value = child.value as? _Injected { |
| 152 | + value._value = resolve(tag: tag) as Any |
| 153 | + } |
| 154 | + else if let weakValue = child.value as? _InjectedWeak { |
| 155 | + weakValue._value = resolve(tag: tag) as AnyObject |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +} |
| 161 | + |
| 162 | +//MARK: - Private |
| 163 | + |
| 164 | +extension DependencyContainer { |
| 165 | + private typealias InjectedFactory = ()->Any |
| 166 | + private typealias InjectedWeakFactory = ()->AnyObject |
| 167 | + |
| 168 | + private func injectedKey(T: Any.Type) -> DefinitionKey { |
| 169 | + return DefinitionKey(protocolType: Any.self, factoryType: InjectedFactory.self, associatedTag: Tag.String(injectedTag(T.self))) |
| 170 | + } |
| 171 | + |
| 172 | + private func injectedWeakKey(T: Any.Type) -> DefinitionKey { |
| 173 | + return DefinitionKey(protocolType: AnyObject.self, factoryType: InjectedWeakFactory.self, associatedTag: Tag.String(injectedWeakTag(T.self))) |
| 174 | + } |
| 175 | + |
| 176 | + func registerInjected<T, F>(definition: DefinitionOf<T, F>) { |
| 177 | + guard let definition = definition.injectedDefinition else { return } |
| 178 | + definitions[injectedKey(T.self)] = definition |
| 179 | + } |
| 180 | + |
| 181 | + func registerInjectedWeak<T, F>(definition: DefinitionOf<T, F>) { |
| 182 | + guard let definition = definition.injectedWeakDefinition else { return } |
| 183 | + definitions[injectedWeakKey(T.self)] = definition |
| 184 | + } |
| 185 | + |
| 186 | + func removeInjected<T, F>(definition: DefinitionOf<T, F>) { |
| 187 | + guard definition.injectedDefinition != nil else { return } |
| 188 | + definitions[injectedKey(T.self)] = nil |
| 189 | + } |
| 190 | + |
| 191 | + func removeInjectedWeak<T, F>(definition: DefinitionOf<T, F>) { |
| 192 | + guard definition.injectedWeakDefinition != nil else { return } |
| 193 | + definitions[injectedWeakKey(T.self)] = nil |
| 194 | + } |
| 195 | + |
| 196 | +} |
| 197 | + |
| 198 | +func injectedTag(T: Any.Type) -> String { |
| 199 | + return "Injected<\(T.self)>" |
| 200 | +} |
| 201 | + |
| 202 | +func injectedWeakTag(T: Any.Type) -> String { |
| 203 | + return "InjectedWeak<\(T.self)>" |
| 204 | +} |
| 205 | + |
| 206 | +protocol _Injected: class { |
| 207 | + var _value: Any? { get set } |
| 208 | +} |
| 209 | + |
| 210 | +protocol _InjectedWeak: class { |
| 211 | + weak var _value: AnyObject? { get set } |
| 212 | +} |
| 213 | + |
0 commit comments