Luminate/Sources/LuminateCore/DIContainer.swift

33 lines
786 B
Swift

import Foundation
public final class DIContainer {
public static let shared = DIContainer()
public private(set) var values = InjectionValues()
private init() {}
public func register<T>(
_ keyPath: WritableKeyPath<InjectionValues, T?>,
value: T
) {
values[keyPath: keyPath] = value
}
public func resolve<T>(
_ keyPath: KeyPath<InjectionValues, T?>
) -> T {
guard let value = values[keyPath: keyPath] else {
fatalError(
"DIContainer: No value registered for \(keyPath). "
+ "Call DIContainer.shared.register(\\.key, value:) during app startup."
)
}
return value
}
public func reset() {
values = InjectionValues()
}
}